Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

CSS Lessons Learned the Hard Way (Beyond Tellerand)


Published on

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

Published in: Design
  • Be the first to comment

CSS Lessons Learned the Hard Way (Beyond Tellerand)

  1. 1. CSS Lessons Learned the Hard Way Zoe Mickley Gillenwater @zomigi Beyond Tellerand Berlin 4 November 2014
  2. 2. I make things…
  3. 3. …some of which come out nicely…
  4. 4. Web sites
  5. 5. Books Stunning CSS3: A Project-based Guide to the Latest in CSS Flexible Web Design: Creating Liquid and Elastic Layouts with CSS
  6. 6. Kids
  7. 7. Cakes
  8. 8. …but sometimes I make mistakes…
  9. 9. Gardening
  10. 10. Gardening
  11. 11. “I can’t start until I know enough to do it perfectly.”
  12. 12. You don’t need everything
  13. 13. Start using Sass in four easy steps.
  14. 14. Install Prepros from Step 1
  15. 15. Drag your web site’s folder into Prepros. Step 2
  16. 16. In this folder, create a file named styles.scss. Step 3
  17. 17. 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; }
  18. 18. Never compare your inside with somebody else’s outside.
  19. 19. 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
  20. 20. 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
  21. 21. 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
  22. 22. Creative people experiment a lot more, therefore succeed a lot more, therefore fail a lot more.
  23. 23. Some of my recent CSS mistakes
  24. 24. Flexbox demo Use Chrome, Opera, Safari 7, Firefox 28+, or IE 10+ for full effect
  25. 25. .action .component
  26. 26. 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>
  27. 27. HTML for flexbox version <form class="builder"> <section class="component"> <section class="component"> <section class="component"> <section class="component"> <section class="action"> </form>
  28. 28. Allow boxes to wrap .builder { display: flex; flex-wrap: wrap; justify-content: space-between; margin: 0 0 40px -20px; }
  29. 29. Using flex to control width/height .flex-item { flex: 1 0 100px; } flex-grow flex-shrink flex-basis
  30. 30. 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)
  31. 31. 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.”
  32. 32. Flexbox fail
  33. 33. This fixed it .component { flex: 1; margin-right: 1px; }
  34. 34. /* this is needed to make .action wrap to second line. why??? */ My comment on the 1px margin
  35. 35. 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.
  36. 36. 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.
  37. 37. 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.).
  38. 38. This was not just a case of succeeding despite a mistake. It was a case of succeeding because of a mistake.
  39. 39. flex-basis mistake round two
  40. 40. 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;
  41. 41. Trying to make one twice as wide .gallery-item { flex: 1 0 200px; } .feature { flex: 2 0 200px; }
  42. 42. Expected rendering
  43. 43. Actual rendering
  44. 44. 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;
  45. 45. 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
  46. 46. It’s because flex-basis = 200px
  47. 47. I really get flex-basis now
  48. 48. Takeaway: don’t use CSS shorthand without understanding all the pieces
  49. 49. Let’s talk about another mistake
  50. 50. Shadow style inspiration
  51. 51. The plan: create shadow with generated content, skew it with CSS perspective
  52. 52. 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); }
  53. 53. Perspective fail
  54. 54. What does rotateX actually do?
  55. 55. The 3 axes X horizontal, left-right Y vertical, up-down Z away-towards you A helpful diagram: your hand. Photo:
  56. 56. Or, if your hand is effed up:
  57. 57. Rotate around the axis not in the direction of the axis As explained well by Peter Gasston in in-the-third-dimension-css-3-d-transforms/
  58. 58. My quick sketch
  59. 59. 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); }
  60. 60. Perspective working
  61. 61. Takeaway: sometimes pen and paper can make a new concept real to you
  62. 62. A two-dimensional problem
  63. 63. Absolute positioning
  64. 64. Web layout is horizontally biased
  65. 65. Flexbox is not row-centric .container { display: flex; flex-direction: column-reverse; align-items: flex-start; min-height: 200px; }
  66. 66. Correct IE 11 min-height bug
  67. 67. Fixed with another flex wrapper <div class="outer"> <div class="container"> <div class="bottom">...</div> </div> </div> .outer { display: flex; }
  68. 68. Takeaway: thinking about web layout in rows can be limiting
  69. 69. Is using flexbox another one of my mistakes?
  70. 70. Flexbox fallback #1: Do nothing. (Seriously.)
  71. 71. Flexbox fallback #2: display: table-cell
  72. 72. Flexbox fallback #3: float (but be careful)
  73. 73. Flexbox with float .container { display: -webkit-box; display: flex; } .sidebar { float: left; position: relative; width: 300px; } .no-flexbox .main-content { margin-left: 300px; }
  74. 74. Flexbox fallback example Without flexbox (IE 9) With flexbox (Chrome)
  75. 75. 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; }
  76. 76. A more public mistake
  77. 77. Sometimes you need to add special content for screen reader users…
  78. 78. …and occasionally you need to hide content from screen reader users.
  79. 79. 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
  80. 80. Hide content visually and aurally .hidden-silent { display: none; visibility: hidden; }
  81. 81. Hide text only but keep it spoken .hidden-text-spoken { text-indent: -999em; overflow: hidden; display: inline-block; }
  82. 82. 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
  83. 83. Problem: NVDA in Firefox wouldn’t read <label> with this class
  84. 84. Delete half the code, see if bug goes away, repeat
  85. 85. I narrowed it down to overflow: hidden Removing this part caused labels to be read correctly in Firefox by NVDA
  86. 86. But removing it still kept the content hidden. So why was it there to begin with?
  87. 87. This scrollbar is what overflow fixes
  88. 88. Now that I understood what overflow did, I could decide if I needed it.
  89. 89. 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
  90. 90. (By the way, this FF/NVDA bug seems to be gone now.)
  91. 91. Takeaway: one-size-fits-all isn’t always best for your needs
  92. 92. Takeaway: you can get help when you share your confusion publicly
  93. 93. Be willing to fail…
  94. 94. …and then tell us about it.
  95. 95. 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
  96. 96. Sharing mistakes has benefits
  97. 97.
  98. 98. We all do imperfect work
  99. 99. /* this is needed to make .action wrap to second line. why??? */
  100. 100. 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.
  101. 101. Revisiting comments // TODO: Fix this. Fix what? // somedev1 - 6/7/02 Adding temporary tracking of Login screen // somedev2 - 5/22/07 Temporary my ass
  102. 102. 99% of the population of the world doesn’t know CSS. Zoe’s made-up statistic
  103. 103. You are awesome, but and you make mistakes.
  104. 104. Let’s use our confidence in our skills to build others up and bravely admit our own shortcomings.
  105. 105. Thank you Zoe Mickley Gillenwater @zomigi Beyond Tellerand Berlin 4 November 2014