@rachelandrew
What’s it like being in the middle of a launch of a big new CSS feature?
Slide 6
@rachelandrew
It involved a lot of email.
Slide 7
@rachelandrew
Grid is: •
Exciting - a real game changer
•
Confusing - it doesn’t seem to do what I thought it would
•
Scary - there are so many new properties to learn!
Slide 8
@rachelandrew
“Q. How do you feel when you see a new CSS feature announced?” –Me, in a survey question
Slide 9
@rachelandrew
“Excited but also worried about falling behind.”
–Survey response
Slide 10
@rachelandrew
“Excited, until I share it with colleagues and they pick it apart” –Survey response
Slide 11
@rachelandrew
“Oh no, a new way to have inconsistencies between web browsers.” –Survey response
Slide 12
@rachelandrew
“Tired.”
–Survey response
Slide 13
@rachelandrew
I can’t tell you what to do.
Slide 14
@rachelandrew
I can help you develop the skills to make those decisions yourself.
Slide 15
@rachelandrew
I want you to be the amazing CSS layout person on your team.
Slide 16
@rachelandrew
You need to understand CSS.
Slide 17
@rachelandrew
Understanding CSS is not about learning every property and value by heart. (my main skill is “can use a search engine”)
Slide 18
@rachelandrew
Core ideas that help CSS layout make sense.
Slide 19
@rachelandrew
Cascading Style Sheets
Slide 20
@rachelandrew
Inheritance - which properties will inherit values from their parent.
Slide 21
@rachelandrew
Specificity - which rule wins when two things could apply to the same element.
https://codepen.io/rachelandrew/pen/ZXMwdB
@rachelandrew
.col { margin-bottom: 1em; margin-left: 2.093333%; width: 6.20%; float: left; } .row::after { content: ""; display: block; clear: both; }
! s e g a t n e c r e P
! g n i r a Cle ! h t a m n u F
.col.span2 { width: calc((6.20%*2) + 2.093333%); } .col.span3 { width: calc((6.20%*3) + (2.093333%*2)); } .col.span4 { width: calc((6.20%*4) + (2.093333%*3)); }
Slide 32
@rachelandrew
Percentages •
Ugly
•
Easy to understand
•
If they total more than 100% bad things happen.
•
Can be converted from an ideal pixel size using a straightforward calculation.
https://codepen.io/rachelandrew/pen/eGPPaZ
@rachelandrew
.wrapper .row { display: flex; flex-wrap: wrap; } .col { ! s e g padding: 10px; a t n e c r margin-bottom: 1em; Pe margin-left: 2.093333%; ! s m e t i width: 6.20%; x e fl e l b i flex: 0 0 auto; x e fl n I }
! h t a m n u F
.col.span2 { width: calc((6.20%*2) + 2.093333%); } .col.span3 { width: calc((6.20%*3) + (2.093333%*2)); } .col.span4 { width: calc((6.20%*4) + (2.093333%*3)); }
Slide 36
@rachelandrew
Past layout methods create the appearance of a grid, by lining things up.
Slide 37
@rachelandrew
CSS Intrinsic and Extrinsic Sizing https://drafts.csswg.org/css-sizing-3/
Slide 38
https://codepen.io/rachelandrew/pen/rGqQNM/
@rachelandrew
/* html */
<div class="box"> I am a string of text. </div> /* css */
.box { padding: 10px; border: 5px dotted rgba(255,255,255,.7); margin-bottom: 2em; }
Slide 39
https://codepen.io/rachelandrew/pen/rGqQNM/
@rachelandrew
available width
@rachelandrew
Items start by trying to display at max-content size.
Space is reduced according to the flexbasis. In this case the size of the content.
https://codepen.io/rachelandrew/pen/KXGbQo/
Slide 52
https://codepen.io/rachelandrew/pen/KXGbQo/
@rachelandrew
flex: 1 1 auto; Items can grow and shrink so stretch to fill the container.
With no extra space, items shrink as before.
Slide 53
https://codepen.io/rachelandrew/pen/KXGbQo/
@rachelandrew
flex: 1 1 0; Items can grow and shrink so stretch to fill the container.
With a flex-basis of 0 space is distributed from 0, making equal columns.
Slide 54
https://codepen.io/rachelandrew/pen/wrYONK
@rachelandrew
Flex items at min-content size
Grid items at max-content size
Slide 55
@rachelandrew
Flexbox is starting from max-content and taking space away. Grid starting at min-content and adding space.
https://slack.engineering/rebuilding-slack-com-b124c405c193
@rachelandrew
“In the end, we discovered that a column-based grid wasn’t actually needed. Since Grid allows you to create a custom grid to match whatever layout you have, we didn’t need to force it into 12 columns. Instead, we created CSS Grid objects for some of the common layout patterns in the designs.” –Rebuilding Slack.com
https://codepen.io/rachelandrew/pen/QqJbyB/
@rachelandrew
grid-area: content / content / content ; grid-row-start
grid-column-start
grid-row-end
• grid-column-end is set to the value used for grid-column-start, which is ‘content’.
Slide 91
https://codepen.io/rachelandrew/pen/QqJbyB/
@rachelandrew
grid-area: content / content ; grid-row-start
grid-column-start
• grid-row-end is set to the value used for grid-column-start, which is ‘content’. • grid-column-end is set to the value used for grid-column-start, which is ‘content’.
Slide 92
https://codepen.io/rachelandrew/pen/QqJbyB/
@rachelandrew
grid-area: content ; grid-row-start
• The other three values are set to the same as grid-row-start, so all are set to ‘content’
@rachelandrew
https://codepen.io/rachelandrew/pen/ZXNYob/
Floating the image means the text wraps round.
Defining a grid on the container means we don’t get the wrapping behaviour.
Slide 113
https://codepen.io/rachelandrew/pen/RLmNvY/
@rachelandrew
Multi-column layout splits content into equal width columns.
Using column-width of 200px means we get more columns if there is room, fewer with less available width.
Slide 114
https://codepen.io/rachelandrew/pen/OxYVmL
@rachelandrew
Flex items with the value of justify-content set to space-between. I also use flexbox to centre the word in the circle.
Slide 115
@rachelandrew
You don’t need a grid-shaped hammer for every layout task.
Slide 116
@rachelandrew
Off-the-shelf frameworks are designed to solve generic problems.
Slide 117
@rachelandrew
Do you want your project to inherit the CSS issues of the rest of the world?
Slide 118
@rachelandrew
Build your own framework* * framework doesn’t mean “all-encompassing behemoth”
Slide 119
@rachelandrew
Solving your specific problems only will result in lighter, easier to understand code
@rachelandrew
Building confidence in your CSS skills will help you to make your case to use newer methods. (Although quite often asking permission is optional)
Slide 127
@rachelandrew
Old browser versions
39%
of survey respondents cited IE11 as oldest IE supported.
Slide 128
@rachelandrew
Old browser versions
63%
of survey respondents support IE10+
Slide 129
@rachelandrew
https://rachelandrew.co.uk/archives/2016/11/26/should-i-try-to-use-the-ie-implementation-of-css-grid-layout/
IE10 & 11 have the -ms prefixed older version of grid layout.
Slide 130
@rachelandrew
For other desktop browsers supporting the last 2 versions is common.
@rachelandrew
Many browsers without support for Grid and other new CSS at this point are mobile browsers.
Slide 134
@rachelandrew
Many of the browsers without support are most popular in areas where data is expensive or devices less powerful.
Slide 135
@rachelandrew
“Grid too young and would need a ton of polyfills.” “Lack of a good css grid polyfill that works with postcss and supports not so old browsers” –Survey responses
Slide 136
@rachelandrew
Stop looking for polyfills and shims. They will make the experience worse for less capable browsers and devices.
Slide 137
@rachelandrew
Using Grid rather than loading a big framework can help create a better experience even for browsers that don’t support Grid.
Slide 138
@rachelandrew
Feature Queries - use CSS to ask if the browser supports a feature before using it.