106
CSS Lessons Learned the Hard Way Zoe Mickley Gillenwater @zomigi Generate Conference London 26 September 2014

CSS Lessons Learned the Hard Way (Generate Conf)

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: CSS Lessons Learned the Hard Way (Generate Conf)

CSS Lessons Learned the Hard Way

Zoe Mickley Gillenwater @zomigi

Generate Conference

London 26 September 2014

Page 2: CSS Lessons Learned the Hard Way (Generate Conf)

I make things…

Page 3: CSS Lessons Learned the Hard Way (Generate Conf)

…some of which come out nicely…

Page 4: CSS Lessons Learned the Hard Way (Generate Conf)

Web sites

Page 5: CSS Lessons Learned the Hard Way (Generate Conf)

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

Page 6: CSS Lessons Learned the Hard Way (Generate Conf)

Kids

Page 7: CSS Lessons Learned the Hard Way (Generate Conf)

Cakes

Page 8: CSS Lessons Learned the Hard Way (Generate Conf)

…but sometimes I make mistakes…

Page 9: CSS Lessons Learned the Hard Way (Generate Conf)

Gardening

Page 10: CSS Lessons Learned the Hard Way (Generate Conf)

Gardening

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

Page 11: CSS Lessons Learned the Hard Way (Generate Conf)
Page 12: CSS Lessons Learned the Hard Way (Generate Conf)

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

Page 13: CSS Lessons Learned the Hard Way (Generate Conf)

You don’t need everything

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

Page 14: CSS Lessons Learned the Hard Way (Generate Conf)

Start using Sass in four easy steps.

Page 15: CSS Lessons Learned the Hard Way (Generate Conf)

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

Step 1

Page 16: CSS Lessons Learned the Hard Way (Generate Conf)

Drag your web site’s folder into Prepros.

Step 2

Page 17: CSS Lessons Learned the Hard Way (Generate Conf)

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

Step 3

Page 18: CSS Lessons Learned the Hard Way (Generate Conf)

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; }

Page 19: CSS Lessons Learned the Hard Way (Generate Conf)

Never compare your inside with somebody else’s outside.

Page 20: CSS Lessons Learned the Hard Way (Generate Conf)

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

Page 21: CSS Lessons Learned the Hard Way (Generate Conf)

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

Page 22: CSS Lessons Learned the Hard Way (Generate Conf)

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

Page 23: CSS Lessons Learned the Hard Way (Generate Conf)

Creative people experiment a lot more,

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

Page 24: CSS Lessons Learned the Hard Way (Generate Conf)

Some of my recent CSS mistakes

Page 25: CSS Lessons Learned the Hard Way (Generate Conf)

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

Page 26: CSS Lessons Learned the Hard Way (Generate Conf)

.action

.component

Page 27: CSS Lessons Learned the Hard Way (Generate Conf)

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>

Page 28: CSS Lessons Learned the Hard Way (Generate Conf)

HTML for flexbox version

<form class="builder">

<section class="component">

<section class="component">

<section class="component">

<section class="component">

<section class="action">

</form>

Page 29: CSS Lessons Learned the Hard Way (Generate Conf)

Allow boxes to wrap

.builder {

display: flex;

flex-wrap: wrap;

justify-content: space-between;

margin: 0 0 40px -20px;

}

Page 30: CSS Lessons Learned the Hard Way (Generate Conf)

Using flex to control width/height

.flex-item {

flex: 1 0 100px;

}

flex-grow flex-shrink flex-basis

Page 31: CSS Lessons Learned the Hard Way (Generate Conf)

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)

Page 32: CSS Lessons Learned the Hard Way (Generate Conf)

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.”

Page 33: CSS Lessons Learned the Hard Way (Generate Conf)

Flexbox fail

Page 34: CSS Lessons Learned the Hard Way (Generate Conf)

This fixed it

.component {

flex: 1;

margin-right: 1px;

}

Page 35: CSS Lessons Learned the Hard Way (Generate Conf)

/* this is needed to

make .action wrap to

second line. why??? */

My comment on the 1px margin

Page 36: CSS Lessons Learned the Hard Way (Generate Conf)

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.

Page 37: CSS Lessons Learned the Hard Way (Generate Conf)

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.

Page 38: CSS Lessons Learned the Hard Way (Generate Conf)

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.).

Page 39: CSS Lessons Learned the Hard Way (Generate Conf)

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

It was a case of succeeding because of a mistake.

Page 40: CSS Lessons Learned the Hard Way (Generate Conf)

flex-basis mistake round two

Page 41: CSS Lessons Learned the Hard Way (Generate Conf)

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;

Page 42: CSS Lessons Learned the Hard Way (Generate Conf)

Trying to make one twice as wide

.gallery-item {

flex: 1 0 200px;

}

.feature {

flex: 2 0 200px;

}

Page 43: CSS Lessons Learned the Hard Way (Generate Conf)

Expected rendering

Page 44: CSS Lessons Learned the Hard Way (Generate Conf)

Actual rendering

Page 45: CSS Lessons Learned the Hard Way (Generate Conf)

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;

Page 46: CSS Lessons Learned the Hard Way (Generate Conf)

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

Page 47: CSS Lessons Learned the Hard Way (Generate Conf)

It’s because flex-basis = 200px

Page 48: CSS Lessons Learned the Hard Way (Generate Conf)

I really get flex-basis now

Page 49: CSS Lessons Learned the Hard Way (Generate Conf)

Takeaway: don’t use CSS shorthand without understanding

all the pieces

Page 50: CSS Lessons Learned the Hard Way (Generate Conf)

Let’s talk about another mistake

Page 51: CSS Lessons Learned the Hard Way (Generate Conf)

Shadow style inspiration

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

Page 52: CSS Lessons Learned the Hard Way (Generate Conf)

The plan: create shadow with generated content,

skew it with CSS perspective

Page 53: CSS Lessons Learned the Hard Way (Generate Conf)
Page 54: CSS Lessons Learned the Hard Way (Generate Conf)
Page 55: CSS Lessons Learned the Hard Way (Generate Conf)

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);

}

Page 56: CSS Lessons Learned the Hard Way (Generate Conf)

Perspective fail

Page 57: CSS Lessons Learned the Hard Way (Generate Conf)
Page 58: CSS Lessons Learned the Hard Way (Generate Conf)

What does rotateX actually do?

Page 59: CSS Lessons Learned the Hard Way (Generate Conf)

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/

Page 60: CSS Lessons Learned the Hard Way (Generate Conf)

Or, if your hand is effed up:

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

Page 61: CSS Lessons Learned the Hard Way (Generate Conf)

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/

Page 62: CSS Lessons Learned the Hard Way (Generate Conf)

My quick sketch

Page 63: CSS Lessons Learned the Hard Way (Generate Conf)

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);

}

Page 64: CSS Lessons Learned the Hard Way (Generate Conf)

Perspective working

Page 65: CSS Lessons Learned the Hard Way (Generate Conf)

Takeaway: sometimes pen and paper can make a new concept

real to you

Page 66: CSS Lessons Learned the Hard Way (Generate Conf)

A more public mistake

Page 67: CSS Lessons Learned the Hard Way (Generate Conf)

Sometimes you need to add special content for

screen reader users…

Page 68: CSS Lessons Learned the Hard Way (Generate Conf)
Page 69: CSS Lessons Learned the Hard Way (Generate Conf)

…and occasionally you need to hide content from

screen reader users.

Page 70: CSS Lessons Learned the Hard Way (Generate Conf)
Page 71: CSS Lessons Learned the Hard Way (Generate Conf)

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

Page 72: CSS Lessons Learned the Hard Way (Generate Conf)

Hide content visually and aurally

.hidden-silent {

display: none;

visibility: hidden;

}

Page 73: CSS Lessons Learned the Hard Way (Generate Conf)

Hide text only but keep it spoken

.hidden-text-spoken {

text-indent: -999em;

overflow: hidden;

display: inline-block;

}

Page 74: CSS Lessons Learned the Hard Way (Generate Conf)

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

Page 75: CSS Lessons Learned the Hard Way (Generate Conf)

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

Page 76: CSS Lessons Learned the Hard Way (Generate Conf)

Delete half the code, see if bug goes away,

repeat

Page 77: CSS Lessons Learned the Hard Way (Generate Conf)

I narrowed it down to overflow: hidden

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

Page 78: CSS Lessons Learned the Hard Way (Generate Conf)

But removing it still kept the content hidden.

So why was it there to begin with?

Page 79: CSS Lessons Learned the Hard Way (Generate Conf)
Page 80: CSS Lessons Learned the Hard Way (Generate Conf)
Page 81: CSS Lessons Learned the Hard Way (Generate Conf)
Page 82: CSS Lessons Learned the Hard Way (Generate Conf)

This scrollbar is what overflow fixes

Page 83: CSS Lessons Learned the Hard Way (Generate Conf)

Now that I understood what overflow did, I could

decide if I needed it.

Page 84: CSS Lessons Learned the Hard Way (Generate Conf)

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

Page 85: CSS Lessons Learned the Hard Way (Generate Conf)

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

Page 86: CSS Lessons Learned the Hard Way (Generate Conf)

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

best for your needs

Page 87: CSS Lessons Learned the Hard Way (Generate Conf)

Takeaway: you can get help when you

share your confusion publicly

Page 88: CSS Lessons Learned the Hard Way (Generate Conf)

Be willing to fail…

Page 89: CSS Lessons Learned the Hard Way (Generate Conf)

…and then tell us about it.

Page 90: CSS Lessons Learned the Hard Way (Generate Conf)

Vulnerability is not weakness. And that myth is profoundly dangerous.

Dr. Brené Brown

Page 91: CSS Lessons Learned the Hard Way (Generate Conf)

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

Page 92: CSS Lessons Learned the Hard Way (Generate Conf)

We all do imperfect work

Page 93: CSS Lessons Learned the Hard Way (Generate Conf)

/* this is needed to

make .action wrap to

second line. why??? */

Page 94: CSS Lessons Learned the Hard Way (Generate Conf)

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.

Page 95: CSS Lessons Learned the Hard Way (Generate Conf)

Revisiting comments

// TODO: Fix this. Fix what?

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

Page 96: CSS Lessons Learned the Hard Way (Generate Conf)
Page 97: CSS Lessons Learned the Hard Way (Generate Conf)

YAY! Mediocrity!

Page 98: CSS Lessons Learned the Hard Way (Generate Conf)

YAY! Being human!

Page 99: CSS Lessons Learned the Hard Way (Generate Conf)

Hiding mistakes seems to be human nature

Page 100: CSS Lessons Learned the Hard Way (Generate Conf)

But sharing mistakes has benefits

Page 101: CSS Lessons Learned the Hard Way (Generate Conf)

Try to shift “Who can I blame?”

to “Who can I teach?”

Page 102: CSS Lessons Learned the Hard Way (Generate Conf)

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

Page 103: CSS Lessons Learned the Hard Way (Generate Conf)

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

Zoe’s made-up statistic

Page 104: CSS Lessons Learned the Hard Way (Generate Conf)

You are awesome, but and you make mistakes.

Page 105: CSS Lessons Learned the Hard Way (Generate Conf)

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

shortcomings.

Page 106: CSS Lessons Learned the Hard Way (Generate Conf)

Thank you

Zoe Mickley Gillenwater @zomigi

Generate Conference

London 26 September 2014