CSS Lessons Learned the Hard Way (Beyond Tellerand)

Post on 29-Jun-2015

1.077 views 0 download

Tags:

description

A collection of mistakes I've made in my CSS, how I fixed them, and how trying and failing has made me better.

Transcript of CSS Lessons Learned the Hard Way (Beyond Tellerand)

CSS Lessons Learned the Hard Way

Zoe Mickley Gillenwater @zomigi

Beyond Tellerand

Berlin 4 November 2014

I make things…

…some of which come out nicely…

Web sites

Books

Stunning CSS3: A Project-based Guide

to the Latest in CSS

www.stunningcss3.com

Flexible Web Design: Creating Liquid and Elastic

Layouts with CSS

www.flexiblewebbook.com

Kids

Cakes

…but sometimes I make mistakes…

Gardening

Gardening

https://www.flickr.com/photos/coachjeff/3600883487/

“I can’t start until I know enough to do it perfectly.”

You don’t need everything

http://www.flickr.com/photos/montage_man/4713541238/

Start using Sass in four easy steps.

Install Prepros from http://alphapixels.com/prepros/

Step 1

Drag your web site’s folder into Prepros.

Step 2

In this folder, create a file named styles.scss.

Step 3

Write in it this:

Step 4

$green: #4F9F1A; $blue: #1D6783; $lightgray: #D6D6D6; body { background: $lightgray; color: $green; } a { color: $blue; } button { background: $blue; color: $lightgray; }

Never compare your inside with somebody else’s outside.

If you walk around with the idea that there are some people

who are so gifted—they have these wonderful things in their head,

but you’re not one of them, you’re just sort of a normal person,

you could never do anything like that— then you live a different kind of life.

Brian Eno

Innovation requires a mindset that rejects the

fear of failure and replaces that fear of failure with the

joy of exploration and experimental learning.

Dr. Edward D. Hess

We also need to accept and embrace the concept of failure, not because failure is a good thing but

because it’s a natural part of the path of progress.

If you’re failing, at least that means you’re trying — not remaining on the outside of the arena, looking in.

Helen Walters

Creative people experiment a lot more,

therefore succeed a lot more, therefore fail a lot more.

Some of my recent CSS mistakes

Flexbox demo www.smoresday.us Use Chrome, Opera, Safari 7, Firefox 28+, or IE 10+ for full effect

.action

.component

HTML without flexbox <form class="builder">

<div class="wrap">

<section class="component">

<section class="component">

<section class="component">

<section class="component">

</div>

<section class="action">

</form>

HTML for flexbox version

<form class="builder">

<section class="component">

<section class="component">

<section class="component">

<section class="component">

<section class="action">

</form>

Allow boxes to wrap

.builder {

display: flex;

flex-wrap: wrap;

justify-content: space-between;

margin: 0 0 40px -20px;

}

Using flex to control width/height

.flex-item {

flex: 1 0 100px;

}

flex-grow flex-shrink flex-basis

Defining the flex property

flex-grow

how much flex item will grow relative to other items if extra space is available (proportion of extra space that it gets)

flex-shrink

how much item will shrink relative to others if there is not enough space (proportion of overflow that gets shaved off)

flex-basis

the initial starting size before free space is distributed (any standard width/height value, including auto)

My first attempt

.component {

flex: 1;

}

.action {

flex: 1 1 100%;

}

Zoe’s Brain Said: “Since .action starts out at 100%, it won’t have space to sit on the first line with the content preceding it, and will wrap to a second line.”

Flexbox fail

This fixed it

.component {

flex: 1;

margin-right: 1px;

}

/* this is needed to

make .action wrap to

second line. why??? */

My comment on the 1px margin

The hidden flex-basis value

.component {

flex: 1 1 0px;

}

.action {

flex: 1 1 100%;

}

Reality: Since it’s fine for each .component to shrink to only 0px wide, a 100% wide element can and will sit on the same line as all the components.

That’s why margin “fixed” it

.component {

flex: 1;

margin-right: 1px;

}

.action {

flex: 1 1 100%;

}

What’s happening: Now each .component starts out taking up 1px of space, so a 100% wide element can’t and won’t sit on the same line with any of the components.

Fixing flex-basis to force the wrap

.component {

flex: 1 1 200px;

}

.action {

flex: 1 1 100%;

}

Fixed: .action will always wrap to new line, and .component boxes will wrap to additional lines when there’s less space than their combined flex-basis values (plus margin, etc.).

This was not just a case of succeeding despite a mistake.

It was a case of succeeding because of a mistake.

flex-basis mistake round two

flex can be proportional

Setting flex-grow/flex-shrink to different values can make flex items size

themselves relative to each other

flex: 1; flex: 1; flex: 2;

Trying to make one twice as wide

.gallery-item {

flex: 1 0 200px;

}

.feature {

flex: 2 0 200px;

}

Expected rendering

Actual rendering

What I figured out

Having widths be in multiples of each other only works if flex-basis is 0

flex: 1 0 0px; flex: 1 0 0px; flex: 2 0 0px;

If flex-basis isn’t 0px…

…the widths may not end up as you expect

The third box gets twice as much of the extra, but that doesn’t make it twice as

wide overall

flex: 1 0 10px; flex: 1 0 10px; flex: 2 0 10px;

10px + 5px extra = 15px 10px + 5px extra = 15px 10px + 10px extra = 20px

if 50px available

It’s because flex-basis = 200px

I really get flex-basis now

Takeaway: don’t use CSS shorthand without understanding

all the pieces

Let’s talk about another mistake

Shadow style inspiration

http://sliderpro.net/examples/minimal-slider/

The plan: create shadow with generated content,

skew it with CSS perspective

My first attempt .lightbox:before {

content: "";

position: absolute;

z-index: -2;

left: 2%;

bottom: 0;

width: 96%;

height: 1px;

box-shadow: 0 25px 30px 15px hsla(0,0%,0%,.4);

transform: perspective(20em);

}

Perspective fail

What does rotateX actually do?

The 3 axes

X horizontal, left-right

Y vertical, up-down

Z away-towards you

A helpful diagram: your hand. Photo: http://www.smashingmagazine.com/2012/01/06/adventures-in-the-third-dimension-css-3-d-transforms/

Or, if your hand is effed up:

http://architecture.gtu.ge/MPL/Multimodal%20Explorer/Acad_11/14control_workplane0/control_workplane.htm

Rotate around the axis not in the direction of the axis

As explained well by Peter Gasston in http://www.smashingmagazine.com/2012/01/06/adventures-

in-the-third-dimension-css-3-d-transforms/

My quick sketch

Adding rotateX with perspective .lightbox:before {

content: "";

position: absolute;

z-index: -2;

left: 6%;

bottom: 0;

width: 88%;

height: 1px;

box-shadow: 0 25px 30px 15px hsla(0,0%,0%,.4);

transform: perspective(20em) rotateX(50deg);

}

Perspective working

Takeaway: sometimes pen and paper can make a new concept

real to you

A two-dimensional problem

Absolute positioning

https://www.flickr.com/photos/40325561@N04/8176648959/

Web layout is horizontally biased

Flexbox is not row-centric

.container {

display: flex;

flex-direction: column-reverse;

align-items: flex-start;

min-height: 200px;

}

Correct

IE 11 min-height bug

Fixed with another flex wrapper

<div class="outer">

<div class="container">

<div class="bottom">...</div>

</div>

</div>

.outer {

display: flex;

}

Takeaway: thinking about web layout in

rows can be limiting

Is using flexbox another one of my mistakes?

Flexbox fallback #1: Do nothing. (Seriously.)

Flexbox fallback #2: display: table-cell

Flexbox fallback #3: float

(but be careful)

Flexbox with float .container { display: -webkit-box; display: flex; } .sidebar { float: left; position: relative; width: 300px; } .no-flexbox .main-content { margin-left: 300px; }

Flexbox fallback example

Without flexbox (IE 9) With flexbox (Chrome)

Flexbox with float fallback .iw_mini_details_wrapper { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: baseline; } .iw_mini_review_score_wrapper { float: left; } .iw_mini_price_wrapper { float: right; }

A more public mistake

Sometimes you need to add special content for

screen reader users…

…and occasionally you need to hide content from

screen reader users.

I needed CSS classes to:

1. Hide content visually and aurally 2. Hide just the text of an element, not

whole element, but keep text spoken 3. Hide whole element visually but keep

its text spoken by screen readers

Hide content visually and aurally

.hidden-silent {

display: none;

visibility: hidden;

}

Hide text only but keep it spoken

.hidden-text-spoken {

text-indent: -999em;

overflow: hidden;

display: inline-block;

}

Hide element but keep it spoken

Yahoo! Accessibility blog said to use:

.hidden-spoken {

position: absolute !important;

clip: rect(1px 1px 1px 1px); /* IE 6-7 */

clip: rect(1px, 1px, 1px, 1px);

padding: 0 !important;

border: 0 !important;

height: 1px !important;

width: 1px !important;

overflow: hidden;

}

Article now online at https://developer.yahoo.com/blogs/ydn/clip-hidden-content-better-accessibility-53456.html

Problem: NVDA in Firefox wouldn’t read <label> with this class

https://www.flickr.com/photos/87255087@N00/6261885005/

Delete half the code, see if bug goes away,

repeat

I narrowed it down to overflow: hidden

Removing this part caused labels to be read correctly in Firefox by NVDA

But removing it still kept the content hidden.

So why was it there to begin with?

This scrollbar is what overflow fixes

Now that I understood what overflow did, I could

decide if I needed it.

How I fixed my mistake

• Removed overflow:hidden from new instances going forward (but sadly not fixed in existing style sheets)

• Updated documentation to advise adding it on as-needed basis

(By the way, this FF/NVDA bug seems to be gone now.)

Takeaway: one-size-fits-all isn’t always

best for your needs

Takeaway: you can get help when you

share your confusion publicly

Be willing to fail…

…and then tell us about it.

Vulnerability is the birthplace of innovation, creativity, and change.

To create is to make something that has never existed before.

There's nothing more vulnerable than that.

Dr. Brené Brown

Sharing mistakes has benefits

http://www.flickr.com/photos/stilleben/49644790/

We all do imperfect work

/* this is needed to

make .action wrap to

second line. why??? */

The evidence in the comments

// Dear future me. Please forgive me. // I can't even begin to express how sorry I am.

// I am not sure if we need this, but too scared to delete.

// Magic. Do not touch.

Revisiting comments

// TODO: Fix this. Fix what?

// somedev1 - 6/7/02 Adding temporary tracking of Login screen // somedev2 - 5/22/07 Temporary my ass

99% of the population of the world doesn’t know CSS.

Zoe’s made-up statistic

You are awesome, but and you make mistakes.

Let’s use our confidence in our skills to build others up and bravely admit our own

shortcomings.

Thank you

Zoe Mickley Gillenwater @zomigi

Beyond Tellerand

Berlin 4 November 2014