Grid and Flexbox - Smashing Conf SF

Rachel Andrew
Rachel AndrewWriter, speaker, co-founder of Perch CMS. Google Developer Expert for Web Technologies at A List Apart
LAYING OUT THE FUTURE WITH GRID &
FLEXBOX
@rachelandrew @ Smashing Conf SF
YOU CAN START USING CSS GRID
LAYOUT TODAY!
@rachelandrew @ Smashing Conf SF
March 2017 March 2017 March 2017 March 2017 March 2017 Soooooon!
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
Start using CSS 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
Grid and Flexbox - Smashing Conf SF
Grid is for two-dimensional layout
Grid and Flexbox - Smashing Conf SF
.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.
.example1 {
display: flex;
justify-content: space-between;
flex-wrap: wrap;
margin: 30px;
}
Flexbox
Using space-between
http://codepen.io/rachelandrew/pen/MpBbwX
Grid and Flexbox - Smashing Conf SF
.example2 {
display: flex;
flex-wrap: wrap;
}
.example2 > div {
flex: 1 1 0;
}
.example2 > div.bigger {
flex: 4 1 0;
}
Flexbox
Some things grow larger than other
things.
This is defined using flex properties on
the item.
http://codepen.io/rachelandrew/pen/MpBbwX
Grid and Flexbox - Smashing Conf SF
Grid works from the container in
.example1 {
display: grid;
grid-gap: 20px;
grid-template-columns: 1fr 1fr 1fr;
margin: 20px;
}
Grid
Define column tracks. Items are
constrained by those tracks.
http://codepen.io/rachelandrew/pen/JWBbJP
Grid and Flexbox - Smashing Conf SF
.example2 {
display: grid;
grid-gap: 20px;
grid-template-columns: 2fr 1fr 2fr;
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.
http://codepen.io/rachelandrew/pen/JWBbJP
Grid and Flexbox - Smashing Conf SF
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
Grid or Flexbox
… and that’s just the start
‣ Grid allows you to layer items, or for two items to occupy
the same space
‣ Grid allows full control of negative space in your designs
‣ Grid has methods such as the dense packing mode to
backfill gaps in a tight-packed grid
Flexbox or Grid?
Use Flexbox when …
‣ Your content is a row OR a column
‣ You want the size of items to dictate their layout
‣ You want to distribute space
Flexbox or Grid?
Consider grid when …
‣ You need to control rows and columns
‣ You are adding widths to a flex item in order to make it
line up with rows above.
‣ You want control of the layout from the parent
‣ Items need to occupy the same space or overlap
Using grid in
production
CSS Grid Layout
Grid and Flexbox - Smashing Conf SF
Grid and Flexbox - Smashing Conf SF
<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 equal columns.
The fr unit defines a fraction of the
available space in the grid container
Grid and Flexbox - Smashing Conf SF
.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.
Grid and Flexbox - Smashing Conf SF
.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
Grid and Flexbox - Smashing Conf SF
Grid and Flexbox - Smashing Conf SF
.listing {
display: grid;
grid-template-columns: repeat(12,1fr);
grid-gap: 20px;
}
The listing
The container for our boxes has 12
equal columns.
Grid and Flexbox - Smashing Conf SF
Grid and Flexbox - Smashing Conf SF
.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
Grid and Flexbox - Smashing Conf SF
.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”
Grid and Flexbox - Smashing Conf SF
.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
Grid and Flexbox - Smashing Conf SF
.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.
Grid and Flexbox - Smashing Conf SF
.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
Edge Grid implementation
▸ Currently tied to the IE10 implementation
▸ 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/
Beware autoprefixer!
March 2017 March 2017 March 2017 March 2017 March 2017 Soooooon!
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 4 years of grid thoughts on my site at:

https://rachelandrew.co.uk/archives/tag/cssgrid 

THANK YOU!
@rachelandrew



Talk resources & code: https://rachelandrew.co.uk/speaking/event/smashingsf-2017
1 of 94

Recommended

CSS Grid for html5j by
CSS Grid for html5jCSS Grid for html5j
CSS Grid for html5jRachel Andrew
1.1K views71 slides
Frontend United: Start using CSS Grid Layout today! by
Frontend United: Start using CSS Grid Layout today!Frontend United: Start using CSS Grid Layout today!
Frontend United: Start using CSS Grid Layout today!Rachel Andrew
2.2K views115 slides
Laying out the future by
Laying out the futureLaying out the future
Laying out the futureRachel Andrew
1.8K views79 slides
AEA Chicago CSS Grid Layout by
AEA Chicago CSS Grid LayoutAEA Chicago CSS Grid Layout
AEA Chicago CSS Grid LayoutRachel Andrew
1.6K views143 slides
CSS Grid Layout - All Things Open by
CSS Grid Layout - All Things OpenCSS Grid Layout - All Things Open
CSS Grid Layout - All Things OpenRachel Andrew
1.9K views137 slides
Render Conf: Start using CSS Grid Layout Today by
Render Conf: Start using CSS Grid Layout TodayRender Conf: Start using CSS Grid Layout Today
Render Conf: Start using CSS Grid Layout TodayRachel Andrew
3.6K views80 slides

More Related Content

What's hot

Introducing CSS Grid Layout by
Introducing CSS Grid LayoutIntroducing CSS Grid Layout
Introducing CSS Grid LayoutRachel Andrew
4.8K views143 slides
Talk Web Design: Get Ready For CSS Grid Layout by
Talk Web Design: Get Ready For CSS Grid LayoutTalk Web Design: Get Ready For CSS Grid Layout
Talk Web Design: Get Ready For CSS Grid LayoutRachel Andrew
4.9K views119 slides
Next-level CSS by
Next-level CSSNext-level CSS
Next-level CSSRachel Andrew
2.7K views109 slides
Laracon Online: Grid and Flexbox by
Laracon Online: Grid and FlexboxLaracon Online: Grid and Flexbox
Laracon Online: Grid and FlexboxRachel Andrew
1.9K views86 slides
CSS Grid Layout by
CSS Grid LayoutCSS Grid Layout
CSS Grid LayoutRachel Andrew
2.3K views132 slides
CSS Grid Layout - An Event Apart Orlando by
CSS Grid Layout - An Event Apart OrlandoCSS Grid Layout - An Event Apart Orlando
CSS Grid Layout - An Event Apart OrlandoRachel Andrew
958 views150 slides

What's hot(20)

Introducing CSS Grid Layout by Rachel Andrew
Introducing CSS Grid LayoutIntroducing CSS Grid Layout
Introducing CSS Grid Layout
Rachel Andrew4.8K views
Talk Web Design: Get Ready For CSS Grid Layout by Rachel Andrew
Talk Web Design: Get Ready For CSS Grid LayoutTalk Web Design: Get Ready For CSS Grid Layout
Talk Web Design: Get Ready For CSS Grid Layout
Rachel Andrew4.9K views
Laracon Online: Grid and Flexbox by Rachel Andrew
Laracon Online: Grid and FlexboxLaracon Online: Grid and Flexbox
Laracon Online: Grid and Flexbox
Rachel Andrew1.9K views
CSS Grid Layout - An Event Apart Orlando by Rachel Andrew
CSS Grid Layout - An Event Apart OrlandoCSS Grid Layout - An Event Apart Orlando
CSS Grid Layout - An Event Apart Orlando
Rachel Andrew958 views
An Event Apart Seattle - New CSS Layout Meets the Real World by Rachel Andrew
An Event Apart Seattle - New CSS Layout Meets the Real WorldAn Event Apart Seattle - New CSS Layout Meets the Real World
An Event Apart Seattle - New CSS Layout Meets the Real World
Rachel Andrew566 views
What I discovered about layout vis CSS Grid by Rachel Andrew
What I discovered about layout vis CSS GridWhat I discovered about layout vis CSS Grid
What I discovered about layout vis CSS Grid
Rachel Andrew9.5K views
Introduction to CSS Grid Layout by Rachel Andrew
Introduction to CSS Grid LayoutIntroduction to CSS Grid Layout
Introduction to CSS Grid Layout
Rachel Andrew3.6K views
Confoo: The New CSS Layout by Rachel Andrew
Confoo: The New CSS LayoutConfoo: The New CSS Layout
Confoo: The New CSS Layout
Rachel Andrew1.2K views
New CSS Meets the Real World by Rachel Andrew
New CSS Meets the Real WorldNew CSS Meets the Real World
New CSS Meets the Real World
Rachel Andrew1.4K views
CSS Day: CSS Grid Layout by Rachel Andrew
CSS Day: CSS Grid Layout CSS Day: CSS Grid Layout
CSS Day: CSS Grid Layout
Rachel Andrew9.6K views
An Event Apart Nashville: CSS Grid Layout by Rachel Andrew
An Event Apart Nashville: CSS Grid LayoutAn Event Apart Nashville: CSS Grid Layout
An Event Apart Nashville: CSS Grid Layout
Rachel Andrew1.7K views
CSS Grid Layout. Implementation status and roadmap (Webkit Contributors Meeti... by Igalia
CSS Grid Layout. Implementation status and roadmap (Webkit Contributors Meeti...CSS Grid Layout. Implementation status and roadmap (Webkit Contributors Meeti...
CSS Grid Layout. Implementation status and roadmap (Webkit Contributors Meeti...
Igalia1.5K views
CSS Grid Layout: An Event Apart Boston 2016 by Rachel Andrew
CSS Grid Layout: An Event Apart Boston 2016CSS Grid Layout: An Event Apart Boston 2016
CSS Grid Layout: An Event Apart Boston 2016
Rachel Andrew2.9K views
Making the most of New CSS Layout by Rachel Andrew
Making the most of New CSS LayoutMaking the most of New CSS Layout
Making the most of New CSS Layout
Rachel Andrew1.3K views
The New CSS Layout - dotCSS by Rachel Andrew
The New CSS Layout - dotCSSThe New CSS Layout - dotCSS
The New CSS Layout - dotCSS
Rachel Andrew38.7K views
The Right Layout Tool for the Job by Rachel Andrew
The Right Layout Tool for the JobThe Right Layout Tool for the Job
The Right Layout Tool for the Job
Rachel Andrew3K views

Similar to Grid and Flexbox - Smashing Conf SF

Start Using CSS Grid Layout Today - RuhrJS by
Start Using CSS Grid Layout Today - RuhrJSStart Using CSS Grid Layout Today - RuhrJS
Start Using CSS Grid Layout Today - RuhrJSRachel Andrew
990 views94 slides
Laying out the future with grid & flexbox - Smashing Conf Freiburg by
Laying out the future with grid & flexbox - Smashing Conf FreiburgLaying out the future with grid & flexbox - Smashing Conf Freiburg
Laying out the future with grid & flexbox - Smashing Conf FreiburgRachel Andrew
2.4K views112 slides
Evergreen websites for Evergreen browsers by
Evergreen websites for Evergreen browsersEvergreen websites for Evergreen browsers
Evergreen websites for Evergreen browsersRachel Andrew
1.5K views124 slides
DevFest Nantes - Start Using CSS Grid Layout today by
DevFest Nantes - Start Using CSS Grid Layout todayDevFest Nantes - Start Using CSS Grid Layout today
DevFest Nantes - Start Using CSS Grid Layout todayRachel Andrew
1.1K views114 slides
View Source London: Solving Layout Problems with CSS Grid & Friends by
View Source London: Solving Layout Problems with CSS Grid & FriendsView Source London: Solving Layout Problems with CSS Grid & Friends
View Source London: Solving Layout Problems with CSS Grid & FriendsRachel Andrew
1K views85 slides
Solving Layout Problems with CSS Grid & Friends - DevFest17 by
Solving Layout Problems with CSS Grid & Friends - DevFest17Solving Layout Problems with CSS Grid & Friends - DevFest17
Solving Layout Problems with CSS Grid & Friends - DevFest17Rachel Andrew
2.1K views96 slides

Similar to Grid and Flexbox - Smashing Conf SF(20)

Start Using CSS Grid Layout Today - RuhrJS by Rachel Andrew
Start Using CSS Grid Layout Today - RuhrJSStart Using CSS Grid Layout Today - RuhrJS
Start Using CSS Grid Layout Today - RuhrJS
Rachel Andrew990 views
Laying out the future with grid & flexbox - Smashing Conf Freiburg by Rachel Andrew
Laying out the future with grid & flexbox - Smashing Conf FreiburgLaying out the future with grid & flexbox - Smashing Conf Freiburg
Laying out the future with grid & flexbox - Smashing Conf Freiburg
Rachel Andrew2.4K views
Evergreen websites for Evergreen browsers by Rachel Andrew
Evergreen websites for Evergreen browsersEvergreen websites for Evergreen browsers
Evergreen websites for Evergreen browsers
Rachel Andrew1.5K views
DevFest Nantes - Start Using CSS Grid Layout today by Rachel Andrew
DevFest Nantes - Start Using CSS Grid Layout todayDevFest Nantes - Start Using CSS Grid Layout today
DevFest Nantes - Start Using CSS Grid Layout today
Rachel Andrew1.1K views
View Source London: Solving Layout Problems with CSS Grid & Friends by Rachel Andrew
View Source London: Solving Layout Problems with CSS Grid & FriendsView Source London: Solving Layout Problems with CSS Grid & Friends
View Source London: Solving Layout Problems with CSS Grid & Friends
Rachel Andrew1K views
Solving Layout Problems with CSS Grid & Friends - DevFest17 by Rachel Andrew
Solving Layout Problems with CSS Grid & Friends - DevFest17Solving Layout Problems with CSS Grid & Friends - DevFest17
Solving Layout Problems with CSS Grid & Friends - DevFest17
Rachel Andrew2.1K views
Google Developers Experts Summit 2017 - CSS Layout by Rachel Andrew
Google Developers Experts Summit 2017 - CSS Layout Google Developers Experts Summit 2017 - CSS Layout
Google Developers Experts Summit 2017 - CSS Layout
Rachel Andrew1.4K views
Devoxx Belgium: CSS Grid Layout by Rachel Andrew
Devoxx Belgium: CSS Grid LayoutDevoxx Belgium: CSS Grid Layout
Devoxx Belgium: CSS Grid Layout
Rachel Andrew1.2K views
An Event Apart SF: CSS Grid Layout by Rachel Andrew
An Event Apart SF: CSS Grid LayoutAn Event Apart SF: CSS Grid Layout
An Event Apart SF: CSS Grid Layout
Rachel Andrew1.6K views
CSS Grid Layout for Topconf, Linz by Rachel Andrew
CSS Grid Layout for Topconf, LinzCSS Grid Layout for Topconf, Linz
CSS Grid Layout for Topconf, Linz
Rachel Andrew185K views
Solving Layout Problems with CSS Grid & Friends - NordicJS by Rachel Andrew
Solving Layout Problems with CSS Grid & Friends - NordicJSSolving Layout Problems with CSS Grid & Friends - NordicJS
Solving Layout Problems with CSS Grid & Friends - NordicJS
Rachel Andrew2.7K views
Solving Layout Problems with CSS Grid & Friends - WEBU17 by Rachel Andrew
Solving Layout Problems with CSS Grid & Friends - WEBU17Solving Layout Problems with CSS Grid & Friends - WEBU17
Solving Layout Problems with CSS Grid & Friends - WEBU17
Rachel Andrew902 views
Solving Layout Problems With CSS Grid and Friends by FITC
Solving Layout Problems With CSS Grid and FriendsSolving Layout Problems With CSS Grid and Friends
Solving Layout Problems With CSS Grid and Friends
FITC586 views
404.ie: Solving Layout Problems with CSS Grid & Friends by Rachel Andrew
404.ie: Solving Layout Problems with CSS Grid & Friends404.ie: Solving Layout Problems with CSS Grid & Friends
404.ie: Solving Layout Problems with CSS Grid & Friends
Rachel Andrew909 views
CSS Grid Layout by Rachel Andrew
CSS Grid LayoutCSS Grid Layout
CSS Grid Layout
Rachel Andrew128.2K views
But what about old browsers? by Rachel Andrew
But what about old browsers?But what about old browsers?
But what about old browsers?
Rachel Andrew851 views
Flexbox and Grid Layout by Rachel Andrew
Flexbox and Grid LayoutFlexbox and Grid Layout
Flexbox and Grid Layout
Rachel Andrew2.6K views
Fluent: Making Sense of the New CSS Layout by Rachel Andrew
Fluent: Making Sense of the New CSS LayoutFluent: Making Sense of the New CSS Layout
Fluent: Making Sense of the New CSS Layout
Rachel Andrew7.7K views
Future Layout & Performance by Rachel Andrew
Future Layout & PerformanceFuture Layout & Performance
Future Layout & Performance
Rachel Andrew1.8K views

More from Rachel Andrew

All Day Hey! Unlocking The Power of CSS Grid Layout by
All Day Hey! Unlocking The Power of CSS Grid LayoutAll Day Hey! Unlocking The Power of CSS Grid Layout
All Day Hey! Unlocking The Power of CSS Grid LayoutRachel Andrew
2.2K views113 slides
SmashingConf SF: Unlocking the Power of CSS Grid Layout by
SmashingConf SF: Unlocking the Power of CSS Grid LayoutSmashingConf SF: Unlocking the Power of CSS Grid Layout
SmashingConf SF: Unlocking the Power of CSS Grid LayoutRachel Andrew
2.3K views113 slides
Unlocking the Power of CSS Grid Layout by
Unlocking the Power of CSS Grid LayoutUnlocking the Power of CSS Grid Layout
Unlocking the Power of CSS Grid LayoutRachel Andrew
2K views113 slides
The Creative New World of CSS by
The Creative New World of CSSThe Creative New World of CSS
The Creative New World of CSSRachel Andrew
2K views144 slides
Into the Weeds of CSS Layout by
Into the Weeds of CSS LayoutInto the Weeds of CSS Layout
Into the Weeds of CSS LayoutRachel Andrew
1.5K views93 slides
Graduating to Grid by
Graduating to GridGraduating to Grid
Graduating to GridRachel Andrew
1.6K views143 slides

More from Rachel Andrew(12)

All Day Hey! Unlocking The Power of CSS Grid Layout by Rachel Andrew
All Day Hey! Unlocking The Power of CSS Grid LayoutAll Day Hey! Unlocking The Power of CSS Grid Layout
All Day Hey! Unlocking The Power of CSS Grid Layout
Rachel Andrew2.2K views
SmashingConf SF: Unlocking the Power of CSS Grid Layout by Rachel Andrew
SmashingConf SF: Unlocking the Power of CSS Grid LayoutSmashingConf SF: Unlocking the Power of CSS Grid Layout
SmashingConf SF: Unlocking the Power of CSS Grid Layout
Rachel Andrew2.3K views
Unlocking the Power of CSS Grid Layout by Rachel Andrew
Unlocking the Power of CSS Grid LayoutUnlocking the Power of CSS Grid Layout
Unlocking the Power of CSS Grid Layout
Rachel Andrew2K views
The Creative New World of CSS by Rachel Andrew
The Creative New World of CSSThe Creative New World of CSS
The Creative New World of CSS
Rachel Andrew2K views
Into the Weeds of CSS Layout by Rachel Andrew
Into the Weeds of CSS LayoutInto the Weeds of CSS Layout
Into the Weeds of CSS Layout
Rachel Andrew1.5K views
Web Summer Camp Keynote by Rachel Andrew
Web Summer Camp KeynoteWeb Summer Camp Keynote
Web Summer Camp Keynote
Rachel Andrew1.9K views
New CSS Layout Meets the Real World by Rachel Andrew
New CSS Layout Meets the Real WorldNew CSS Layout Meets the Real World
New CSS Layout Meets the Real World
Rachel Andrew864 views
An Event Apart DC - New CSS Layout meets the Real World by Rachel Andrew
An Event Apart DC - New CSS Layout meets the Real WorldAn Event Apart DC - New CSS Layout meets the Real World
An Event Apart DC - New CSS Layout meets the Real World
Rachel Andrew408 views
Perch, Patterns and Old Browsers by Rachel Andrew
Perch, Patterns and Old BrowsersPerch, Patterns and Old Browsers
Perch, Patterns and Old Browsers
Rachel Andrew1.8K views
Where does CSS come from? by Rachel Andrew
Where does CSS come from?Where does CSS come from?
Where does CSS come from?
Rachel Andrew2.8K views
Confoo: You can use CSS for that! by Rachel Andrew
Confoo: You can use CSS for that!Confoo: You can use CSS for that!
Confoo: You can use CSS for that!
Rachel Andrew1.5K views

Recently uploaded

TouchLog: Finger Micro Gesture Recognition Using Photo-Reflective Sensors by
TouchLog: Finger Micro Gesture Recognition  Using Photo-Reflective SensorsTouchLog: Finger Micro Gesture Recognition  Using Photo-Reflective Sensors
TouchLog: Finger Micro Gesture Recognition Using Photo-Reflective Sensorssugiuralab
21 views15 slides
Five Things You SHOULD Know About Postman by
Five Things You SHOULD Know About PostmanFive Things You SHOULD Know About Postman
Five Things You SHOULD Know About PostmanPostman
36 views43 slides
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLive by
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLiveAutomating a World-Class Technology Conference; Behind the Scenes of CiscoLive
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLiveNetwork Automation Forum
34 views35 slides
Evolving the Network Automation Journey from Python to Platforms by
Evolving the Network Automation Journey from Python to PlatformsEvolving the Network Automation Journey from Python to Platforms
Evolving the Network Automation Journey from Python to PlatformsNetwork Automation Forum
13 views21 slides
Igniting Next Level Productivity with AI-Infused Data Integration Workflows by
Igniting Next Level Productivity with AI-Infused Data Integration Workflows Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration Workflows Safe Software
280 views86 slides
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院 by
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院IttrainingIttraining
58 views8 slides

Recently uploaded(20)

TouchLog: Finger Micro Gesture Recognition Using Photo-Reflective Sensors by sugiuralab
TouchLog: Finger Micro Gesture Recognition  Using Photo-Reflective SensorsTouchLog: Finger Micro Gesture Recognition  Using Photo-Reflective Sensors
TouchLog: Finger Micro Gesture Recognition Using Photo-Reflective Sensors
sugiuralab21 views
Five Things You SHOULD Know About Postman by Postman
Five Things You SHOULD Know About PostmanFive Things You SHOULD Know About Postman
Five Things You SHOULD Know About Postman
Postman36 views
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLive by Network Automation Forum
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLiveAutomating a World-Class Technology Conference; Behind the Scenes of CiscoLive
Automating a World-Class Technology Conference; Behind the Scenes of CiscoLive
Igniting Next Level Productivity with AI-Infused Data Integration Workflows by Safe Software
Igniting Next Level Productivity with AI-Infused Data Integration Workflows Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Igniting Next Level Productivity with AI-Infused Data Integration Workflows
Safe Software280 views
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院 by IttrainingIttraining
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院
【USB韌體設計課程】精選講義節錄-USB的列舉過程_艾鍗學院
HTTP headers that make your website go faster - devs.gent November 2023 by Thijs Feryn
HTTP headers that make your website go faster - devs.gent November 2023HTTP headers that make your website go faster - devs.gent November 2023
HTTP headers that make your website go faster - devs.gent November 2023
Thijs Feryn22 views
Piloting & Scaling Successfully With Microsoft Viva by Richard Harbridge
Piloting & Scaling Successfully With Microsoft VivaPiloting & Scaling Successfully With Microsoft Viva
Piloting & Scaling Successfully With Microsoft Viva
STKI Israeli Market Study 2023 corrected forecast 2023_24 v3.pdf by Dr. Jimmy Schwarzkopf
STKI Israeli Market Study 2023   corrected forecast 2023_24 v3.pdfSTKI Israeli Market Study 2023   corrected forecast 2023_24 v3.pdf
STKI Israeli Market Study 2023 corrected forecast 2023_24 v3.pdf
Special_edition_innovator_2023.pdf by WillDavies22
Special_edition_innovator_2023.pdfSpecial_edition_innovator_2023.pdf
Special_edition_innovator_2023.pdf
WillDavies2218 views
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f... by TrustArc
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...
TrustArc Webinar - Managing Online Tracking Technology Vendors_ A Checklist f...
TrustArc11 views

Grid and Flexbox - Smashing Conf SF

  • 1. LAYING OUT THE FUTURE WITH GRID & FLEXBOX @rachelandrew @ Smashing Conf SF
  • 2. YOU CAN START USING CSS GRID LAYOUT TODAY! @rachelandrew @ Smashing Conf SF
  • 3. March 2017 March 2017 March 2017 March 2017 March 2017 Soooooon!
  • 4. 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
  • 5. Start using CSS 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?
  • 7. Flexbox is for one-dimensional layout
  • 9. Grid is for two-dimensional layout
  • 11. .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
  • 12. If you are adding widths to all your flex items, you probably need grid.
  • 13. .example1 { display: flex; justify-content: space-between; flex-wrap: wrap; margin: 30px; } Flexbox Using space-between http://codepen.io/rachelandrew/pen/MpBbwX
  • 15. .example2 { display: flex; flex-wrap: wrap; } .example2 > div { flex: 1 1 0; } .example2 > div.bigger { flex: 4 1 0; } Flexbox Some things grow larger than other things. This is defined using flex properties on the item. http://codepen.io/rachelandrew/pen/MpBbwX
  • 17. Grid works from the container in
  • 18. .example1 { display: grid; grid-gap: 20px; grid-template-columns: 1fr 1fr 1fr; margin: 20px; } Grid Define column tracks. Items are constrained by those tracks. http://codepen.io/rachelandrew/pen/JWBbJP
  • 20. .example2 { display: grid; grid-gap: 20px; grid-template-columns: 2fr 1fr 2fr; 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. http://codepen.io/rachelandrew/pen/JWBbJP
  • 22. Other layout methods start with the item.
  • 23. .box { float: left; width: 33.3333%; } A float grid The float property and widths are added to the items.
  • 24. .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.
  • 25. .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.
  • 26. .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.
  • 27. Grid is all about the container
  • 28. Grid or Flexbox … and that’s just the start ‣ Grid allows you to layer items, or for two items to occupy the same space ‣ Grid allows full control of negative space in your designs ‣ Grid has methods such as the dense packing mode to backfill gaps in a tight-packed grid
  • 29. Flexbox or Grid? Use Flexbox when … ‣ Your content is a row OR a column ‣ You want the size of items to dictate their layout ‣ You want to distribute space
  • 30. Flexbox or Grid? Consider grid when … ‣ You need to control rows and columns ‣ You are adding widths to a flex item in order to make it line up with rows above. ‣ You want control of the layout from the parent ‣ Items need to occupy the same space or overlap
  • 34. <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.
  • 35. .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 equal columns.
  • 36. The fr unit defines a fraction of the available space in the grid container
  • 38. .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.
  • 39. Grid lines respect writing mode. Column line 1 is on the left and -1 on the right in a LTR language.
  • 40. 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.
  • 42. .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.
  • 44. 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
  • 47. .listing { display: grid; grid-template-columns: repeat(12,1fr); grid-gap: 20px; } The listing The container for our boxes has 12 equal columns.
  • 50. .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
  • 52. .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.
  • 53. .box-older { grid-column: auto / span 3; } Smaller boxes The boxes for older items span 3 tracks.
  • 56. .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.
  • 57. .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.
  • 60. 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
  • 61. You do not need to build “two layouts”
  • 63. .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.
  • 64. Feature Queries are your new best friend
  • 66. .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.
  • 68. .listing .box-feature { flex: 1 1 60%; } Flex layout Give the feature box a larger flex- basis percentage.
  • 70. .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
  • 71. .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
  • 72. .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
  • 73. .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
  • 74. .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
  • 75. .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
  • 76. Overrides inside @supports are mostly widths & margins
  • 77. * { 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
  • 79. Edge Grid implementation ▸ Currently tied to the IE10 implementation ▸ 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/
  • 81. March 2017 March 2017 March 2017 March 2017 March 2017 Soooooon!
  • 82. Let browser vendors know which features you want.
  • 86. .exclusion { -ms-wrap-flow: both; wrap-flow: both; } Exclusions Defines the wrap-flow property, which enables wrapping content round all sides of an element.
  • 88. You can get involved in the future of CSS.
  • 91. 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.
  • 93. 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 4 years of grid thoughts on my site at:
 https://rachelandrew.co.uk/archives/tag/cssgrid 

  • 94. THANK YOU! @rachelandrew
 
 Talk resources & code: https://rachelandrew.co.uk/speaking/event/smashingsf-2017