SlideShare a Scribd company logo
START USING GRID LAYOUT TODAY
@rachelandrew @ RuhrJS
Rachel Andrew
▸ CSS Working Group Invited Expert
▸ Google Developer Expert
▸ co-founder Perch CMS
▸ Old Nerd.
▸ You can find me in most places as @rachelandrew you can email
me@rachelandrew.co.uk or check out my site at https://rachelandrew.co.uk
March 2017 March 2017 March 2017 March 2017 March 2017 17 Oct 2017
Start using Grid Layout today
▸ What is grid & why is it different to flexbox?
▸ How do I get started using grid in production?
▸ What about old browsers?
▸ How can we help encourage browsers to give us cool new stuff?
Why not use
flexbox?
CSS Grid Layout
Flexbox is for one-dimensional layout
Start Using CSS Grid Layout Today - RuhrJS
Grid is for two-dimensional layout
Start Using CSS Grid Layout Today - RuhrJS
.grid {
display: grid;
grid-gap: 20px;
grid-template-columns: repeat(auto-
fill, minmax(200px, 1fr));
}
Grid minmax() and auto-fill
Creating a flexible number of flexible
tracks, with a little bit of grid spec
magic.
http://codepen.io/rachelandrew/pen/evjdLM
If you are adding widths to all your
flex items, you probably need grid.
.example {
display: flex;
justify-content: space-between;
margin: 30px;
}
Flexbox
Using space-between
https://codepen.io/rachelandrew/pen/rzXXJY
Start Using CSS Grid Layout Today - RuhrJS
.example {
display: flex;
flex-wrap: wrap;
margin: 30px;
}
.example > div {
flex: 1 1 auto;
}
Flexbox
Allowing items to grow and shrink
from a flex-basis of auto.
https://codepen.io/rachelandrew/pen/MvNNaj
Start Using CSS Grid Layout Today - RuhrJS
Grid works from the container in
.example {
display: grid;
grid-gap: 20px;
grid-template-columns: 1fr 1fr 1fr 1fr;
margin: 20px;
}
Grid
Define column tracks. Items are
constrained by those tracks.
https://codepen.io/rachelandrew/pen/prMMLe
1fr 1fr 1fr 1fr
.example {
display: grid;
grid-gap: 20px;
grid-template-columns: 2fr 1fr 2fr 1fr;
margin: 20px;
}
Grid
To make some tracks larger than
others, we do that when defining the
tracks on the container not on the
item itself.
https://codepen.io/rachelandrew/pen/LjwwgM
2fr 1fr 2fr 1fr
.example {
display: grid;
grid-gap: 20px;
grid-template-columns: min-content max-
content fit-content(200px);
margin: 20px;
}
Grid
Key words based on content size,
change the size of the entire track.
- min-content
- max-content
- fit-content
https://codepen.io/rachelandrew/pen/NaLExq
Start Using CSS Grid Layout Today - RuhrJS
Other layout methods start with 

the item.
.box {
float: left;
width: 33.3333%;
}
A float grid
The float property and widths are
added to the items.
.box {
display: inline-block;
width: 33.3333%;
}
inline-block grid
The display property is set to inline-
block and width is added to the item.
.container {

display: flex;

}

.box {
flex: 0 0 33.3333%;
}
Flex grid
We add display: flex to the container
however to make a grid out of flex
items we need to use the flex
properties in the items.
.container {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
}
Grid Layout
With CSS Grid Layout we create the
grid on the parent element. We don’t
need to add properties to the items.
Grid is all about the container
Using grid in
production
CSS Grid Layout
Start Using CSS Grid Layout Today - RuhrJS
Start Using CSS Grid Layout Today - RuhrJS
Start Using CSS Grid Layout Today - RuhrJS
<div class="box-feature">
<img class="box-image" src="http://placehold.it/
900x450" alt="placeholder">
<h2 class="box-feature-title">Featured Item</h2>
<div class="box-content">…</div>
</div>
Feature box
The feature has an image with a
heading and body text overlaid.
.box-feature {
display: grid;
grid-gap: 20px;
grid-template-columns: repeat(6, 1fr);
}
Feature box
display: grid turns on grid layout
grid-gap defines gutters between
grid items
grid-template-columns creates
column tracks. In this case creating a
grid with 6 columns.
The fr unit defines a fraction of the
available space in the grid container
Start Using CSS Grid Layout Today - RuhrJS
.box-feature .box-image {
align-self: stretch;
justify-self: stretch;
grid-column: 1 / -1;
grid-row: 1 / 4;
}
Feature box
The image starts at grid column 

line 1 and ends at -1, which is the end
line.
It starts at grid row 1, ending at grid
row 4.
Using box alignment properties to
stretch the image over that area.
Grid lines respect writing mode.
Column line 1 is on the left and -1 on
the right in a LTR language.
Explicit vs. Implicit Grid
▸ The Explicit Grid is created when you define tracks with grid-template-
columns and grid-template-rows
▸ If you place an item outside of that grid, or auto-placed content requires
further row or column tracks these are added by grid as the Implicit Grid.
Start Using CSS Grid Layout Today - RuhrJS
.box-feature .box-feature-title {
grid-column: 3 / -1;
grid-row: 1;
background-color: rgba(0,0,0,0.7);
color: #fff;
align-self: start;
padding: 20px;
}
.box-feature .box-content {
grid-column: 2 / -1;
grid-row: 2;
background-color: rgba(0,0,0,0.7);
color: #fff;
padding: 20px;
}
Feature box
Positioning the content inside the
area that the image is stretched over.
http://codepen.io/rachelandrew/pen/evQjMx
Layering items on the grid
▸ You can position items into the same grid cells
▸ Items further down in the source appear on top of earlier items
▸ Control the stack using z-index
Start Using CSS Grid Layout Today - RuhrJS
Start Using CSS Grid Layout Today - RuhrJS
.listing {
display: grid;
grid-template-columns: repeat(12,1fr);
grid-gap: 20px;
}
The listing
The container for our boxes has 12
equal columns.
Start Using CSS Grid Layout Today - RuhrJS
Start Using CSS Grid Layout Today - RuhrJS
.box-title {
grid-column: 1 / 4;
grid-row: 1 / 2;
}
.box-feature {
grid-column: 4 / -1;
grid-row: 1 / 2;
}
The listing
Positioning the title top left and the
feature top right
Start Using CSS Grid Layout Today - RuhrJS
.box-newer {
grid-column: auto / span 4;
}
.box-newer.box-media {
grid-row-end: span 2;
}
Larger boxes
Newer items span 4 column tracks. If
they also have a class of box-media
they span 2 row tracks.
.box-older {
grid-column: auto / span 3;
}
Smaller boxes
The boxes for older items span 3
tracks.
http://codepen.io/rachelandrew/pen/Opaopw
Going
responsive
CSS Grid
.box-title {
grid-column: 1 / -1;
grid-row: 1;
}
@media all and (min-width: 53.125em) {
.box-title {
grid-column: 1 / 6;
grid-row: 1 / 3;
}
}
@media all and (min-width: 75em) {
.box-title {
grid-column: 1 / 4;
grid-row: 1 / 2;
}
}
Going responsive
Inside media queries we can redefine
where items sit on the grid.
.box-newer {
grid-column: 1 / -1;
}
@media all and (min-width: 28.125em) {
.box-newer {
grid-column: auto / span 6;
}
}
@media all and (min-width: 53.125em) {
.box-newer {
grid-column: auto / span 4;
}
}
Going responsive
Or redefine how many columns they
span.
http://codepen.io/rachelandrew/pen/gmQdgz
What about
old browsers?
CSS Grid Layout
What about old browsers?
If using display: grid on a container, child items:
‣ Using float, lose their float behaviour
‣ The vertical-align property has no effect
‣ Flex items become grid items
‣ Items set to display: block or inline-block become grid
items
‣ Items set to display: table-cell stop creating anonymous
boxes
You do not need to build “two
layouts”
Start Using CSS Grid Layout Today - RuhrJS
.listing {
display: flex;
flex-wrap: wrap;
margin: 0 20px;
display: grid;
grid-template-columns: repeat(12,1fr);
grid-gap: 20px;
}
.listing > * {
flex: 1 1 30%;
margin: 0 20px 20px 20px;
}
Adding a flex fallback
Browsers that support display: flex
and not grid will turn the children into
flex, not grid, items.
The flex properties applied to those
items will be ignored by grid layout.
Feature Queries are your new best
friend
Start Using CSS Grid Layout Today - RuhrJS
.listing > * {
flex: 1 1 30%;
margin: 0 20px 20px 20px;
}
@supports(display: grid) {
.listing > * {
margin: 0;
}
}
Using feature queries
Add a margin for flex layout, remove it
if we are using grid layout.
Start Using CSS Grid Layout Today - RuhrJS
.listing .box-feature {
flex: 1 1 60%;
}
Flex layout
Give the feature box a larger flex-
basis percentage.
http://codepen.io/rachelandrew/pen/jBQpXv
.grid > div {
float: left;
}
.grid {
display: grid;
grid-gap: 10px;
grid-template-columns: repeat(3, auto);
width: 500px;
}
Float and Clear
The float and clear properties have
no effect on a grid item.



https://codepen.io/rachelandrew/pen/YZeqZv
.grid > div {
display: inline-block;
}
.grid {
display: grid;
grid-gap: 10px;
grid-template-columns: repeat(3, auto);
width: 500px;
}
display: inline-block
The properties associated with
something being inline-block cease
to apply.



https://codepen.io/rachelandrew/pen/vxdGjQ
.grid > div {
display: table-cell;
vertical-align: top;
}
.grid {
border-spacing: 10px;
}
.grid {
display: grid;
grid-gap: 10px;
grid-template-columns: repeat(3, auto);
width: 500px;
}
display: table
Anonymous boxes will not be
generated and the item will become a
grid item.



https://codepen.io/rachelandrew/pen/bqLpQN
.grid > div {
display: inline-block;
vertical-align: top;
}
.grid {
display: grid;
grid-gap: 10px;
grid-template-columns: repeat(3, auto);
width: 500px;
}
The vertical-align property
Can be used as a fallback for box
alignment and ceases to apply on grid
items.



https://codepen.io/rachelandrew/pen/vxdGaQ
.grid {
column-count: 3;
width: 500px;
}
.grid {
display: grid;
grid-gap: 10px;
grid-template-columns: repeat(3, auto);
}
Multiple-column layout
Multiple-column layout properties
cease to apply in grid layout.



https://codepen.io/rachelandrew/pen/JWpXxv
.grid {
display: flex;
align-items: center;
width: 500px;
height: 200px;
border: 1px dotted #694486;
}
.grid > div {
flex: 1;
}
.grid {
display: grid;
grid-gap: 10px;
grid-template-columns: repeat(3, auto);
}
Flex layout
Grid will override flex layout and
shares box alignment properties.



https://codepen.io/rachelandrew/pen/YZeqMB
Overrides inside @supports are
mostly widths & margins
* { box-sizing: border-box; }
.grid > div {
float: left;
width: 33.333%;
}
@supports (display: grid) {
.grid > div {
width: auto;
}
}
.grid {
display: grid;
grid-gap: 10px;
grid-template-columns: repeat(3, 1fr);
width: 500px;
}
Override widths in feature queries
Watch out for widths in your fallback
layouts.



https://codepen.io/rachelandrew/pen/JWpXNr
https://rachelandrew.co.uk/css/cheatsheets/grid-fallbacks
IE/Edge Grid implementation
▸ Updated grid implementation ships in Edge next week - 17th October
▸ Edge 15 and below, IE10 and IE11 have an older version of Grid
▸ Prefixed with -ms
▸ No auto-placement or grid-template-areas layout
▸ For simple line-based positioning it works
▸ More at https://rachelandrew.co.uk/archives/2017/04/04/edge-starts-work-
on-their-grid-implementation-update/
Autoprefixer can add -ms-grid
prefixes. This is rarely helpful.
March 2017 March 2017 March 2017 March 2017 March 2017 17 Oct 2017
Let browser vendors know which
features you want.
https://wpdev.uservoice.com/forums/257854-microsoft-edge-developer/
https://developer.microsoft.com/en-us/microsoft-edge/platform/usage/
http://codepen.io/rachelandrew/pen/YqpRdq/
.exclusion {
-ms-wrap-flow: both;
wrap-flow: both;
}
Exclusions
Defines the wrap-flow property,
which enables wrapping content
round all sides of an element.
https://www.chromestatus.com/features/6296903092273152
You can get involved in the future of
CSS.
https://github.com/w3c/csswg-drafts/issues
https://github.com/w3c/csswg-drafts/issues/499
Get involved with CSS
▸ Comment on or raise new issues against CSS specifications
▸ Raise bugs against browsers
▸ Vote on features where browsers have a platform to do so
▸ Write about new features - it demonstrates we want them
▸ Be nice while doing it. Browser engineers and spec editors work within
constraints just as you do in your projects.
is here!
CSS Grid
Find out more
I made you some resources
Visit Grid by Example for worked examples, and a free video
tutorial:

http://gridbyexample.com
I created a huge set of guides for MDN: 

https://developer.mozilla.org/en-US/docs/Web/CSS/
CSS_Grid_Layout
Over 5 years of grid thoughts on my site at:

https://rachelandrew.co.uk/archives/tag/cssgrid
GridBugs! I’m collecting and trying to get fixed interop issues:

https://github.com/rachelandrew/gridbugs 

The New 

CSS Layout
Out now!
THANK YOU!
@rachelandrew



https://rachelandrew.co.uk/speaking/event/ruhrjs-2017

More Related Content

PDF
DevFest Nantes - Start Using CSS Grid Layout today
PDF
Laying out the future with grid & flexbox - Smashing Conf Freiburg
PDF
Graduating to Grid
PDF
Solving Layout Problems with CSS Grid & Friends - DevFest17
PDF
View Source London: Solving Layout Problems with CSS Grid & Friends
PDF
Solving Layout Problems with CSS Grid & Friends - NordicJS
PDF
Evergreen websites for Evergreen browsers
PDF
What I discovered about layout vis CSS Grid
DevFest Nantes - Start Using CSS Grid Layout today
Laying out the future with grid & flexbox - Smashing Conf Freiburg
Graduating to Grid
Solving Layout Problems with CSS Grid & Friends - DevFest17
View Source London: Solving Layout Problems with CSS Grid & Friends
Solving Layout Problems with CSS Grid & Friends - NordicJS
Evergreen websites for Evergreen browsers
What I discovered about layout vis CSS Grid

What's hot (20)

PDF
Confoo: You can use CSS for that!
PDF
The Creative New World of CSS
PDF
An Event Apart Seattle - New CSS Layout Meets the Real World
PDF
SmashingConf SF: Unlocking the Power of CSS Grid Layout
PDF
Unlocking the Power of CSS Grid Layout
PDF
Laracon Online: Grid and Flexbox
PDF
Flexbox and Grid Layout
PDF
New CSS Meets the Real World
PDF
GOTO Berlin - You can use CSS for that
PDF
All Day Hey! Unlocking The Power of CSS Grid Layout
PDF
Into the Weeds of CSS Layout
PDF
The Future of Frontend - what is new in CSS?
PDF
AEA Chicago CSS Grid Layout
PDF
Talk Web Design: Get Ready For CSS Grid Layout
PDF
Confoo: The New CSS Layout
PDF
CSS Day: CSS Grid Layout
PDF
Render Conf: Start using CSS Grid Layout Today
PDF
CSS Conf Budapest - New CSS Layout
PDF
CSS Grid Layout - All Things Open
PDF
Solving Layout Problems with CSS Grid & Friends - WEBU17
Confoo: You can use CSS for that!
The Creative New World of CSS
An Event Apart Seattle - New CSS Layout Meets the Real World
SmashingConf SF: Unlocking the Power of CSS Grid Layout
Unlocking the Power of CSS Grid Layout
Laracon Online: Grid and Flexbox
Flexbox and Grid Layout
New CSS Meets the Real World
GOTO Berlin - You can use CSS for that
All Day Hey! Unlocking The Power of CSS Grid Layout
Into the Weeds of CSS Layout
The Future of Frontend - what is new in CSS?
AEA Chicago CSS Grid Layout
Talk Web Design: Get Ready For CSS Grid Layout
Confoo: The New CSS Layout
CSS Day: CSS Grid Layout
Render Conf: Start using CSS Grid Layout Today
CSS Conf Budapest - New CSS Layout
CSS Grid Layout - All Things Open
Solving Layout Problems with CSS Grid & Friends - WEBU17
Ad

Similar to Start Using CSS Grid Layout Today - RuhrJS (20)

PDF
Grid and Flexbox - Smashing Conf SF
PDF
Frontend United: Start using CSS Grid Layout today!
PDF
Google Developers Experts Summit 2017 - CSS Layout
PDF
Solving Layout Problems With CSS Grid and Friends
PDF
404.ie: Solving Layout Problems with CSS Grid & Friends
PDF
Making the most of New CSS Layout
PDF
Laying out the future
PDF
CSSConf.asia - Laying out the future
PDF
An Event Apart Nashville: CSS Grid Layout
PDF
CSS Grid Layout
PDF
Devoxx Belgium: CSS Grid Layout
PDF
An Event Apart SF: CSS Grid Layout
PDF
CSS Grid Layout for Topconf, Linz
PDF
Introduction to CSS Grid Layout
PDF
World of CSS Grid
PDF
But what about old browsers?
PDF
The New CSS Layout - dotCSS
PDF
Fluent: Making Sense of the New CSS Layout
PDF
Flexbox and Grid Layout
PDF
Introducing CSS Grid Layout
Grid and Flexbox - Smashing Conf SF
Frontend United: Start using CSS Grid Layout today!
Google Developers Experts Summit 2017 - CSS Layout
Solving Layout Problems With CSS Grid and Friends
404.ie: Solving Layout Problems with CSS Grid & Friends
Making the most of New CSS Layout
Laying out the future
CSSConf.asia - Laying out the future
An Event Apart Nashville: CSS Grid Layout
CSS Grid Layout
Devoxx Belgium: CSS Grid Layout
An Event Apart SF: CSS Grid Layout
CSS Grid Layout for Topconf, Linz
Introduction to CSS Grid Layout
World of CSS Grid
But what about old browsers?
The New CSS Layout - dotCSS
Fluent: Making Sense of the New CSS Layout
Flexbox and Grid Layout
Introducing CSS Grid Layout
Ad

More from Rachel Andrew (7)

PDF
Web Summer Camp Keynote
PDF
New CSS Layout Meets the Real World
PDF
An Event Apart DC - New CSS Layout meets the Real World
PDF
Perch, Patterns and Old Browsers
PDF
Where does CSS come from?
PDF
CSS Grid for html5j
PDF
CSS Grid Layout for Frontend NE
Web Summer Camp Keynote
New CSS Layout Meets the Real World
An Event Apart DC - New CSS Layout meets the Real World
Perch, Patterns and Old Browsers
Where does CSS come from?
CSS Grid for html5j
CSS Grid Layout for Frontend NE

Recently uploaded (20)

PPTX
A Presentation on Artificial Intelligence
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Electronic commerce courselecture one. Pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Encapsulation theory and applications.pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
NewMind AI Monthly Chronicles - July 2025
A Presentation on Artificial Intelligence
MYSQL Presentation for SQL database connectivity
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Electronic commerce courselecture one. Pdf
Machine learning based COVID-19 study performance prediction
Per capita expenditure prediction using model stacking based on satellite ima...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
20250228 LYD VKU AI Blended-Learning.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
Dropbox Q2 2025 Financial Results & Investor Presentation
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
The AUB Centre for AI in Media Proposal.docx
Network Security Unit 5.pdf for BCA BBA.
Encapsulation theory and applications.pdf
Big Data Technologies - Introduction.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Review of recent advances in non-invasive hemoglobin estimation
NewMind AI Monthly Chronicles - July 2025

Start Using CSS Grid Layout Today - RuhrJS

  • 1. START USING GRID LAYOUT TODAY @rachelandrew @ RuhrJS
  • 2. Rachel Andrew ▸ CSS Working Group Invited Expert ▸ Google Developer Expert ▸ co-founder Perch CMS ▸ Old Nerd. ▸ You can find me in most places as @rachelandrew you can email [email protected] or check out my site at https://rachelandrew.co.uk
  • 3. March 2017 March 2017 March 2017 March 2017 March 2017 17 Oct 2017
  • 4. Start using Grid Layout today ▸ What is grid & why is it different to flexbox? ▸ How do I get started using grid in production? ▸ What about old browsers? ▸ How can we help encourage browsers to give us cool new stuff?
  • 6. Flexbox is for one-dimensional layout
  • 8. Grid is for two-dimensional layout
  • 10. .grid { display: grid; grid-gap: 20px; grid-template-columns: repeat(auto- fill, minmax(200px, 1fr)); } Grid minmax() and auto-fill Creating a flexible number of flexible tracks, with a little bit of grid spec magic. http://codepen.io/rachelandrew/pen/evjdLM
  • 11. If you are adding widths to all your flex items, you probably need grid.
  • 12. .example { display: flex; justify-content: space-between; margin: 30px; } Flexbox Using space-between https://codepen.io/rachelandrew/pen/rzXXJY
  • 14. .example { display: flex; flex-wrap: wrap; margin: 30px; } .example > div { flex: 1 1 auto; } Flexbox Allowing items to grow and shrink from a flex-basis of auto. https://codepen.io/rachelandrew/pen/MvNNaj
  • 16. Grid works from the container in
  • 17. .example { display: grid; grid-gap: 20px; grid-template-columns: 1fr 1fr 1fr 1fr; margin: 20px; } Grid Define column tracks. Items are constrained by those tracks. https://codepen.io/rachelandrew/pen/prMMLe
  • 18. 1fr 1fr 1fr 1fr
  • 19. .example { display: grid; grid-gap: 20px; grid-template-columns: 2fr 1fr 2fr 1fr; margin: 20px; } Grid To make some tracks larger than others, we do that when defining the tracks on the container not on the item itself. https://codepen.io/rachelandrew/pen/LjwwgM
  • 20. 2fr 1fr 2fr 1fr
  • 21. .example { display: grid; grid-gap: 20px; grid-template-columns: min-content max- content fit-content(200px); margin: 20px; } Grid Key words based on content size, change the size of the entire track. - min-content - max-content - fit-content https://codepen.io/rachelandrew/pen/NaLExq
  • 23. Other layout methods start with 
 the item.
  • 24. .box { float: left; width: 33.3333%; } A float grid The float property and widths are added to the items.
  • 25. .box { display: inline-block; width: 33.3333%; } inline-block grid The display property is set to inline- block and width is added to the item.
  • 26. .container {
 display: flex;
 }
 .box { flex: 0 0 33.3333%; } Flex grid We add display: flex to the container however to make a grid out of flex items we need to use the flex properties in the items.
  • 27. .container { display: grid; grid-template-columns: 1fr 1fr 1fr; } Grid Layout With CSS Grid Layout we create the grid on the parent element. We don’t need to add properties to the items.
  • 28. Grid is all about the container
  • 33. <div class="box-feature"> <img class="box-image" src="http://placehold.it/ 900x450" alt="placeholder"> <h2 class="box-feature-title">Featured Item</h2> <div class="box-content">…</div> </div> Feature box The feature has an image with a heading and body text overlaid.
  • 34. .box-feature { display: grid; grid-gap: 20px; grid-template-columns: repeat(6, 1fr); } Feature box display: grid turns on grid layout grid-gap defines gutters between grid items grid-template-columns creates column tracks. In this case creating a grid with 6 columns.
  • 35. The fr unit defines a fraction of the available space in the grid container
  • 37. .box-feature .box-image { align-self: stretch; justify-self: stretch; grid-column: 1 / -1; grid-row: 1 / 4; } Feature box The image starts at grid column 
 line 1 and ends at -1, which is the end line. It starts at grid row 1, ending at grid row 4. Using box alignment properties to stretch the image over that area.
  • 38. Grid lines respect writing mode. Column line 1 is on the left and -1 on the right in a LTR language.
  • 39. Explicit vs. Implicit Grid ▸ The Explicit Grid is created when you define tracks with grid-template- columns and grid-template-rows ▸ If you place an item outside of that grid, or auto-placed content requires further row or column tracks these are added by grid as the Implicit Grid.
  • 41. .box-feature .box-feature-title { grid-column: 3 / -1; grid-row: 1; background-color: rgba(0,0,0,0.7); color: #fff; align-self: start; padding: 20px; } .box-feature .box-content { grid-column: 2 / -1; grid-row: 2; background-color: rgba(0,0,0,0.7); color: #fff; padding: 20px; } Feature box Positioning the content inside the area that the image is stretched over.
  • 43. Layering items on the grid ▸ You can position items into the same grid cells ▸ Items further down in the source appear on top of earlier items ▸ Control the stack using z-index
  • 46. .listing { display: grid; grid-template-columns: repeat(12,1fr); grid-gap: 20px; } The listing The container for our boxes has 12 equal columns.
  • 49. .box-title { grid-column: 1 / 4; grid-row: 1 / 2; } .box-feature { grid-column: 4 / -1; grid-row: 1 / 2; } The listing Positioning the title top left and the feature top right
  • 51. .box-newer { grid-column: auto / span 4; } .box-newer.box-media { grid-row-end: span 2; } Larger boxes Newer items span 4 column tracks. If they also have a class of box-media they span 2 row tracks.
  • 52. .box-older { grid-column: auto / span 3; } Smaller boxes The boxes for older items span 3 tracks.
  • 55. .box-title { grid-column: 1 / -1; grid-row: 1; } @media all and (min-width: 53.125em) { .box-title { grid-column: 1 / 6; grid-row: 1 / 3; } } @media all and (min-width: 75em) { .box-title { grid-column: 1 / 4; grid-row: 1 / 2; } } Going responsive Inside media queries we can redefine where items sit on the grid.
  • 56. .box-newer { grid-column: 1 / -1; } @media all and (min-width: 28.125em) { .box-newer { grid-column: auto / span 6; } } @media all and (min-width: 53.125em) { .box-newer { grid-column: auto / span 4; } } Going responsive Or redefine how many columns they span.
  • 59. What about old browsers? If using display: grid on a container, child items: ‣ Using float, lose their float behaviour ‣ The vertical-align property has no effect ‣ Flex items become grid items ‣ Items set to display: block or inline-block become grid items ‣ Items set to display: table-cell stop creating anonymous boxes
  • 60. You do not need to build “two layouts”
  • 62. .listing { display: flex; flex-wrap: wrap; margin: 0 20px; display: grid; grid-template-columns: repeat(12,1fr); grid-gap: 20px; } .listing > * { flex: 1 1 30%; margin: 0 20px 20px 20px; } Adding a flex fallback Browsers that support display: flex and not grid will turn the children into flex, not grid, items. The flex properties applied to those items will be ignored by grid layout.
  • 63. Feature Queries are your new best friend
  • 65. .listing > * { flex: 1 1 30%; margin: 0 20px 20px 20px; } @supports(display: grid) { .listing > * { margin: 0; } } Using feature queries Add a margin for flex layout, remove it if we are using grid layout.
  • 67. .listing .box-feature { flex: 1 1 60%; } Flex layout Give the feature box a larger flex- basis percentage.
  • 69. .grid > div { float: left; } .grid { display: grid; grid-gap: 10px; grid-template-columns: repeat(3, auto); width: 500px; } Float and Clear The float and clear properties have no effect on a grid item.
 
 https://codepen.io/rachelandrew/pen/YZeqZv
  • 70. .grid > div { display: inline-block; } .grid { display: grid; grid-gap: 10px; grid-template-columns: repeat(3, auto); width: 500px; } display: inline-block The properties associated with something being inline-block cease to apply.
 
 https://codepen.io/rachelandrew/pen/vxdGjQ
  • 71. .grid > div { display: table-cell; vertical-align: top; } .grid { border-spacing: 10px; } .grid { display: grid; grid-gap: 10px; grid-template-columns: repeat(3, auto); width: 500px; } display: table Anonymous boxes will not be generated and the item will become a grid item.
 
 https://codepen.io/rachelandrew/pen/bqLpQN
  • 72. .grid > div { display: inline-block; vertical-align: top; } .grid { display: grid; grid-gap: 10px; grid-template-columns: repeat(3, auto); width: 500px; } The vertical-align property Can be used as a fallback for box alignment and ceases to apply on grid items.
 
 https://codepen.io/rachelandrew/pen/vxdGaQ
  • 73. .grid { column-count: 3; width: 500px; } .grid { display: grid; grid-gap: 10px; grid-template-columns: repeat(3, auto); } Multiple-column layout Multiple-column layout properties cease to apply in grid layout.
 
 https://codepen.io/rachelandrew/pen/JWpXxv
  • 74. .grid { display: flex; align-items: center; width: 500px; height: 200px; border: 1px dotted #694486; } .grid > div { flex: 1; } .grid { display: grid; grid-gap: 10px; grid-template-columns: repeat(3, auto); } Flex layout Grid will override flex layout and shares box alignment properties.
 
 https://codepen.io/rachelandrew/pen/YZeqMB
  • 75. Overrides inside @supports are mostly widths & margins
  • 76. * { box-sizing: border-box; } .grid > div { float: left; width: 33.333%; } @supports (display: grid) { .grid > div { width: auto; } } .grid { display: grid; grid-gap: 10px; grid-template-columns: repeat(3, 1fr); width: 500px; } Override widths in feature queries Watch out for widths in your fallback layouts.
 
 https://codepen.io/rachelandrew/pen/JWpXNr
  • 78. IE/Edge Grid implementation ▸ Updated grid implementation ships in Edge next week - 17th October ▸ Edge 15 and below, IE10 and IE11 have an older version of Grid ▸ Prefixed with -ms ▸ No auto-placement or grid-template-areas layout ▸ For simple line-based positioning it works ▸ More at https://rachelandrew.co.uk/archives/2017/04/04/edge-starts-work- on-their-grid-implementation-update/
  • 79. Autoprefixer can add -ms-grid prefixes. This is rarely helpful.
  • 80. March 2017 March 2017 March 2017 March 2017 March 2017 17 Oct 2017
  • 81. Let browser vendors know which features you want.
  • 85. .exclusion { -ms-wrap-flow: both; wrap-flow: both; } Exclusions Defines the wrap-flow property, which enables wrapping content round all sides of an element.
  • 87. You can get involved in the future of CSS.
  • 90. Get involved with CSS ▸ Comment on or raise new issues against CSS specifications ▸ Raise bugs against browsers ▸ Vote on features where browsers have a platform to do so ▸ Write about new features - it demonstrates we want them ▸ Be nice while doing it. Browser engineers and spec editors work within constraints just as you do in your projects.
  • 92. Find out more I made you some resources Visit Grid by Example for worked examples, and a free video tutorial:
 http://gridbyexample.com I created a huge set of guides for MDN: 
 https://developer.mozilla.org/en-US/docs/Web/CSS/ CSS_Grid_Layout Over 5 years of grid thoughts on my site at:
 https://rachelandrew.co.uk/archives/tag/cssgrid GridBugs! I’m collecting and trying to get fixed interop issues:
 https://github.com/rachelandrew/gridbugs 

  • 93. The New 
 CSS Layout Out now!