SlideShare a Scribd company logo
Making the most of
New CSS Layout
Rachel Andrew, 

SmartWeb, Bucharest 2016
Rachel Andrew
rachelandrew.co.uk
@rachelandrew
CSS Working Group Invited Expert
Google Developer Expert for Web Technologies
Co-founder Perch CMS: https://grabaperch.com
Contact: me@rachelandrew.co.uk
Modern CSS Layout?
• Floats
• Inline-block
• display: table
• Absolute & Relative positioning
• Frameworks … lots of frameworks
Snippet from
Bootstrap Grid
mark-up.
<div class="row">
<div class="col-md-8">
.col-md-8
<div class="row">
<div class="col-md-6">
.col-md-6
</div>
<div class="col-md-6">
.col-md-6
</div>
</div>
</div>
<div class="col-md-4">
.col-md-4
</div>
</div>
Our great hopes for layout
• Flexbox

https://drafts.csswg.org/css-flexbox/
• CSS Grid Layout

https://drafts.csswg.org/css-grid/
• Box Alignment

https://drafts.csswg.org/css-align/
http://caniuse.com/#feat=css-grid
http://gridbyexample.com/browsers
http://caniuse.com/#feat=css-grid
The new CSS for Layout
Items in our layouts understand
themselves as part of a complete
layout.
http://alistapart.com/article/fauxcolumns
http://colintoh.com/blog/display-table-anti-hero
Flexbox
Full height
columns with
flexbox, taking
advantage of
default alignment
values.
.wrapper {
display: flex;
}
.sidebar {
flex: 1 1 30%;
}
.content {
flex: 1 1 70%;
}
Grid Layout
Full height
columns in CSS
Grid Layout.
.wrapper {
display: grid;
grid-template-columns: 30% 70%;
}
.sidebar {
grid-column: 1;
}
.content {
grid-column: 2;
}
Separation of source and display
Flexbox
The flex-direction
property can take
a value of row to
display things as a
row or column to
display them as a
column.
nav ul{
display: flex;
justify-content: space-between;
flex-direction: row;
}
Flexbox
The visual order
can be switched
using row-reverse
or column-
reverse.
nav ul{
display: flex;
justify-content: space-between;
flex-direction: row-reverse;
}
Flexbox
Adding display:
flex to our
container element
causes the items
to display flexibly
in a row.
.wrapper {
display: flex;
}
Flexbox
The order
property means
we can change
the order of flex
items using CSS.
This does not
change their
source order.
li:nth-child(1) {
order: 3;
}
li:nth-child(2) {
order: 1;
}
li:nth-child(3) {
order: 4;
}
li:nth-child(4) {
order: 2;
}
Grid Layout
I have created a
grid on my
wrapper element.
The grid has 3
equal width
columns.
Rows will be
created as
required as we
position items
into them.
.wrapper {
display: grid;
grid-template-columns: 1fr 1fr
1fr;
}
Grid Layout
I am positioning
my elements
using CSS Grid
Layout line-based
positioning.
Setting a column
and a row line
using the grid-
column and grid-
row properties.
li:nth-child(1) {
grid-column: 3 ;
grid-row: 2 ;
}
li:nth-child(2) {
grid-column: 1 ;
grid-row: 2 ;
}
li:nth-child(3) {
grid-column: 1 ;
grid-row: 1 ;
}
li:nth-child(4) {
grid-column: 2 ;
grid-row: 1 ;
}
Making the most of New CSS Layout
CSS Grid automatic placement
http://www.w3.org/TR/2015/WD-css-grid-1-20150917/#grid-
auto-flow-property
https://rachelandrew.co.uk/archives/2015/04/14/grid-layout-
automatic-placement-and-packing-modes/
Making the most of New CSS Layout
Grid Layout
When using
automatic
placement we
can create rules
for items in our
document - for
example
displaying
portrait and
landscape images
differently.
.wrapper {
display: grid;
grid-template-columns: 1fr 1fr
1fr 1fr;
grid-template-rows: auto;
}
.landscape {
grid-column-end: span 2;
}
grid-auto-
flow
The default value of grid-
auto-flow is sparse. Grid will
move forward planning
items skipping cells if items
do not fit .
Grid Layout
With a dense
packing mode
grid will move
items out of
source order to
backfill spaces.
.wrapper {
display: grid;
grid-template-columns: 1fr 1fr
1fr 1fr;
grid-template-rows: auto;
grid-auto-flow: dense;
}
.landscape {
grid-column-end: span 2;
}
grid-auto-
flow
With grid-auto-flow dense
items are displayed out of
source order. Grid backfills
any suitable gaps.
With great power comes
responsibility.
Power and responsibility
• Good = creating the most accessible source
order and using Grid or Flexbox to get the
optimal display for each device.
• Bad = using Grid or Flexbox as an excuse to
forget about the source.
• Terrible - stripping out semantic elements to
make everything a grid or flex item.
https://drafts.csswg.org/css-flexbox/#order-accessibility
Authors must use order only for
visual, not logical, reordering of
content. Style sheets that use order to
perform logical reordering are non-
conforming.
Léonie Watson | On CSS
accessibility and drinking tea | CSS
Day 2016
https://vimeo.com/180566024
Control of alignment
CSS Box Alignment Module Level 3
“This module contains the features of CSS relating to the
alignment of boxes within their containers in the various CSS
box layout models: block layout, table layout, flex layout, and
grid layout.” - https://drafts.csswg.org/css-align/
It’s 2016. We can now centre
things.
Box Alignment Properties
- justify-content
- align-content
- justify-self
- align-self
- justify-items
- align-items
Flexbox
The justify-
content property
is set to space-
between.
The items at each
end are placed
against the
container and the
remaining space
distributed evenly.
nav ul{
display: flex;
justify-content: space-between;
flex-direction: row;
}
Flexbox
The justify-
content property
is set to space-
around.
The items are
evenly distributed
in the container
with a half size
space at each end.
nav ul{
display: flex;
justify-content: space-around;
flex-direction: row;
}
Grid
If there is space in
the grid container
after all column
and row tracks
have been added.
Use space-around
and space-
between to space
the tracks.
.wrapper {
width: 500px;
height: 400px;
display: grid;
grid-gap: 10px;
grid-template-columns: repeat(4,
80px);
grid-template-rows: repeat(3,100px);
align-content: space-around;
justify-content: space-between;
}
Flexbox
The value of align-
items is stretch
by default.
If I add extra text
in one navigation
point the others
all take the same
height.
nav ul{
display: flex;
justify-content: space-around;
flex-direction: row;
align-items: stretch;
}
Flexbox
If I set the value
of align-items to
center then we
get vertical
centring.
nav ul{
display: flex;
justify-content: space-around;
flex-direction: row;
align-items: center;
}
My grid has an
absolute width
and height. This is
larger than
required for the
tracks.
.wrapper {
width: 500px;
height: 400px;
display: grid;
grid-gap: 10px;
grid-template-columns: repeat(4, 80px);
grid-template-rows: repeat(3,100px);
}
The align-content
property controls
the block axis.
This axis aligns
the grid rows.
.wrapper {
width: 500px;
height: 400px;
display: grid;
grid-gap: 10px;
grid-template-columns: repeat(4, 80px);
grid-template-rows: repeat(3,100px);
align-content: end;
}
The justify-
content property
controls the inline
axis.
This axis aligns
the grid columns.
.wrapper {
width: 500px;
height: 400px;
display: grid;
grid-gap: 10px;
grid-template-columns: repeat(4, 80px);
grid-template-rows: repeat(3,100px);
align-content: end;
justify-content: center;
}
I can create this
same layout with
flexbox or Grid.
With flexbox the
items are laid out
in a row.
.wrapper {
display: flex;
}
.wrapper li {
flex: 1 0 25%;
}
The first item is at
the default
stretch and as the
tallest item is
dictating the
height of the flex
container.
The second is
entered in the
container.
The third aligned
to the start.
The fourth aligned
to the end.
.wrapper li:nth-child(2) {
align-self: center;
}
.wrapper li:nth-child(3) {
align-self: flex-start;
}
.wrapper li:nth-child(4) {
align-self: flex-end;
}
For Grid I use a
single row, 4
column Grid.
.wrapper {
display: grid;
grid-template-columns: 1fr 1fr 1fr 1fr;
}
Grid alignment
properties for the
three landscape
images.
.wrapper li:nth-child(2) {
align-self: center;
}
.wrapper li:nth-child(3) {
align-self: start;
}
.wrapper li:nth-child(4) {
align-self: end;
}
Making the most of New CSS Layout
100vh
body {
align-content: center;
justify-content: center;
}
Aligning the grid.
body {
height: 100vh;
display: grid;
grid-template-columns: 80%;
align-content: center;
justify-content: center;
}
.account {
align-self: end;
}
1fr
1fr
1fr 1fr 1fr
2fr
2fr
.login > div
.login > div
.login > div.actions
Setting align-
items to centre
lines the fields
and labels up on
their centre line.
.login > div {
display: grid;
grid-template-columns: 1fr 2fr;
align-items: center;
}
.login > div.actions {
grid-template-columns: 1fr 1fr 1fr;
}
Responsive by default
Ethan Marcotte, Fluid Grids
“… every aspect of the grid—and the
elements laid upon it—can be
expressed as a proportion relative to
its container.”
target ÷ context
= result
h1 {
margin-left: 14.575%; /* 144px / 988px =
0.14575 */
width: 70.85%; /* 700px / 988px = 0.7085 */
}
Flexbox
The most simple
flexbox example
demonstrates the
inherent
flexibility.
The items will be
displayed as a
row, with equal
space between
each item.
nav ul{
display: flex;
justify-content: space-between;
}
The flex property
• flex-grow - add space
• flex-shrink - remove space
• flex-basis - the initial size before any growing
or shrinking
Flexbox
flex: 1 1 300px;
flex-grow: 1
flex-shrink: 1;
flex-basis:
300px;
The initial width
of our li is 300
pixels, however it
can grow larger
and shrink
smaller than 300
pixels.
.wrapper {
display: flex;
}
.wrapper li {
flex: 1 1 300px;
min-width: 1px;
}
Making the most of New CSS Layout
Flexbox
flex: 1 1 300px;
flex-grow: 1
flex-shrink: 1;
flex-basis:
300px;
If we allow the
flex items to
wrap we can see
how flex-basis
works by
dragging the
window smaller.
.wrapper {
display: flex;
flex-flow: row wrap;
justify-content: space-around;
}
.wrapper li {
flex: 1 1 300px;
min-width: 1px;
}
Making the most of New CSS Layout
Flexbox
flex: 1 1 300px;
flex-grow: 1;
flex-shrink: 1;
flex-basis:
300px;
The 3rd item has
flex: 0 1 300px;
so cannot grow.
.wrapper {
display: flex;
}
.wrapper li {
flex: 1 1 300px;
min-width: 1px;
}
.wrapper li:nth-child(3) {
flex: 0 1 300px;
}
Making the most of New CSS Layout
Flexbox
If we set the 3rd
item to

flex-grow: 2
This item will be
assigned twice of
much of the
available free
space after we
have reached the
300 pixel initial
width.
.wrapper {
display: flex;
}
.wrapper li {
flex: 1 1 300px;
min-width: 1px;
}
.wrapper li:nth-child(3) {
flex: 2 1 300px;
}
Making the most of New CSS Layout
http://madebymike.com.au/demos/flexbox-tester/
The CSS Grid Layout fr unit
Grid Layout
I am creating
three grid column
tracks, all 1fr in
width.
This gives me
three equally
sized column
tracks.
.wrapper {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
}
Grid Layout
If I create the first
column as 600
pixels and then
have two 1fr
columns the 600
pixel track is
removed from the
available space
and the remainder
is distributed
equally between
the two columns.
.wrapper {
display: grid;
grid-template-columns: 600px 1fr 1fr;
}
Grid Layout
With a 600 pixel
column, a 1fr and
a 3fr column. The
600 pixels is
removed from the
available space
then the
remaining space is
divided by 4.
The 1fr column
gets 25% and the
3fr column 75%.
.wrapper {
display: grid;
grid-template-columns: 600px 1fr 3fr;
}
Making the most of New CSS Layout
Flexbox for 1 dimensional layout.
CSS Grid is for 2 dimensional
layout.
Making the most of New CSS Layout
The value of the
grid-template-
columns property
says:
repeat this track
listing, auto-filing
as many columns
with a minimum
width of 300
pixels and a
maximum of 1fr.
.wrapper {
display: grid;
grid-template-columns:
repeat(auto-fill, minmax(300px,
1fr));
}
Making the most of New CSS Layout
Solving common problems
Making the most of New CSS Layout
Making the most of New CSS Layout
Making the most of New CSS Layout
Using the
minmax()
function with
grid-auto-rows.
.home-hero {
display: grid;
grid-gap: 1px;
grid-auto-rows: minmax(150px,
auto);
}
An item on the
grid can become a
grid or flex
container itself.
In this case I am
using flexbox and
auto margins to
push my content
to the bottom of
the box.
.special {
display: flex;
flex-direction: column;
}
.special h3{
margin-top: auto;
}
Making the most of New CSS Layout
Real-world Grid Layout
Say hello to Feature Queries.
I have set three
classes to
display: none;
.has-flex,
.has-grid,
.has-grid-shapes {
display: none;
}
My @supports
rule tests for
support of the
display property
with a value of
flex.
If it is supported
we show the div.
@supports (display: flex) {
.has-flex {
display: block;
}
}
My @supports
rule tests for
support of the
display property
with a value of
grid.
If it is supported
we show the div.
@supports (display: grid) {
.has-grid {
display: block;
}
}
My @supports
rule tests for
support of the
display property
with a value of
grid AND shape-
outside:circle.
If it is supported
we show the div.
@supports (display: grid) and
(shape-outside:circle()) {
.has-grid-shapes {
display: block;
}
}
http://caniuse.com/#feat=css-featurequeries
Making the most of New CSS Layout
Defaults for all
browsers will be
loaded by even
really old
browsers.
body {
padding-top: 20%;
}
h1,
.login,
.account,
.contact{
width:80%;
margin: 0 auto;
}
Making the most of New CSS Layout
Within a Feature
Query we add
some information
for flexbox
supporting
browsers.
@supports (display: flex) {
body {
padding:0;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}
h1, .login, .account, .contact {
margin: 0;
width: 80%;
}
}
Making the most of New CSS Layout
The Feature
Query for Grid
supporting
browsers.
@supports (display: grid) {
body {
display: grid;
grid-template-columns: 80%;
align-content: center;
align-items: stretch;
}
@media (min-width: 650px) {
body {
grid-template-columns: repeat(2, minmax(150px, 30%));
}
h1,
.login {
grid-column-end: span 2;
width: auto;
}
.login > div {
display: grid;
grid-template-columns: 1fr 2fr;
align-items: center;
}
.login > div.actions {
grid-template-columns: 1fr 1fr 1fr;
}
.account {
border-right: 1px dotted rgb(191, 216, 227);
padding: 0 10px 0 0;
align-self: end;
width: auto;
}
.contact {
padding: 0 0 0 10px;
width: auto;
}
}
}
Your users ‘grow into’
enhancements as their browsers
auto-update.
https://cssgrid.me/SmartWeb16-code
http://gridbyexample.com
Why are we looking at something I
can’t use yet?
https://wiki.csswg.org/ideas/mistakes
Get involved with developing specs!
• While a spec is being developed your feedback
is wanted and can be included in the spec.
• Wait until browsers ship and you lose that
chance.
• It just got easier. CSS Spec issues are now on
GitHub.

http://logs.csswg.org/irc.w3.org/css/2016-05-10/#e684439
Do a good deed for your future self.
Thank you
Slides & Resources: https://cssgrid.me/SmartWeb16-slides
http://csslayout.news - sign up for my weekly CSS Layout email
—
@rachelandrew | me@rachelandrew.co.uk
—
https://rachelandrew.co.uk | https://grabaperch.com

More Related Content

PDF
GOTO Berlin - You can use CSS for that
PDF
The Right Layout Tool for the Job
PDF
Talk Web Design: Get Ready For CSS Grid Layout
PDF
New CSS Meets the Real World
PDF
Laracon Online: Grid and Flexbox
PDF
CSS Grid Layout for Frontend NE
PDF
AEA Chicago CSS Grid Layout
PDF
Render Conf: Start using CSS Grid Layout Today
GOTO Berlin - You can use CSS for that
The Right Layout Tool for the Job
Talk Web Design: Get Ready For CSS Grid Layout
New CSS Meets the Real World
Laracon Online: Grid and Flexbox
CSS Grid Layout for Frontend NE
AEA Chicago CSS Grid Layout
Render Conf: Start using CSS Grid Layout Today

What's hot (20)

PDF
Next-level CSS
PDF
Confoo: The New CSS Layout
PDF
CSS Grid Layout
PDF
CSSConf.asia - Laying out the future
PDF
Introducing CSS Grid Layout
PDF
Laying out the future
PDF
CSS Grid Layout - An Event Apart Orlando
PDF
CSS Grid Layout - All Things Open
PDF
Flexbox and Grid Layout
PDF
CSS Day: CSS Grid Layout
PDF
Frontend United: Start using CSS Grid Layout today!
PDF
Grid and Flexbox - Smashing Conf SF
PDF
CSS Grid for html5j
PDF
Confoo: You can use CSS for that!
PDF
An Event Apart Nashville: CSS Grid Layout
PDF
The New CSS Layout - dotCSS
PDF
An Event Apart Seattle - New CSS Layout Meets the Real World
PDF
Future Layout & Performance
PDF
CSS Grid Layout. Implementation status and roadmap (Webkit Contributors Meeti...
PDF
Introduction to CSS Grid Layout
Next-level CSS
Confoo: The New CSS Layout
CSS Grid Layout
CSSConf.asia - Laying out the future
Introducing CSS Grid Layout
Laying out the future
CSS Grid Layout - An Event Apart Orlando
CSS Grid Layout - All Things Open
Flexbox and Grid Layout
CSS Day: CSS Grid Layout
Frontend United: Start using CSS Grid Layout today!
Grid and Flexbox - Smashing Conf SF
CSS Grid for html5j
Confoo: You can use CSS for that!
An Event Apart Nashville: CSS Grid Layout
The New CSS Layout - dotCSS
An Event Apart Seattle - New CSS Layout Meets the Real World
Future Layout & Performance
CSS Grid Layout. Implementation status and roadmap (Webkit Contributors Meeti...
Introduction to CSS Grid Layout
Ad

Similar to Making the most of New CSS Layout (20)

PDF
But what about old browsers?
PDF
CSS Conf Budapest - New CSS Layout
PDF
ConFoo 2016: Making Sense of CSS Layout
PDF
Fluent: Making Sense of the New CSS Layout
PDF
Flexbox and Grid Layout
PDF
Start Using CSS Grid Layout Today - RuhrJS
PDF
DevFest Nantes - Start Using CSS Grid Layout today
PDF
Evergreen websites for Evergreen browsers
PDF
View Source London: Solving Layout Problems with CSS Grid & Friends
PDF
Solving Layout Problems with CSS Grid & Friends - DevFest17
PDF
What I discovered about layout vis CSS Grid
PDF
Solving Layout Problems with CSS Grid & Friends - NordicJS
PDF
Google Developers Experts Summit 2017 - CSS Layout
PDF
404.ie: Solving Layout Problems with CSS Grid & Friends
PDF
An Event Apart SF: CSS Grid Layout
PDF
Solving Layout Problems With CSS Grid and Friends
PDF
Solving Layout Problems with CSS Grid & Friends - WEBU17
PDF
CSS Grid Layout
PDF
Devoxx Belgium: CSS Grid Layout
PDF
CSS Grid Layout for Topconf, Linz
But what about old browsers?
CSS Conf Budapest - New CSS Layout
ConFoo 2016: Making Sense of CSS Layout
Fluent: Making Sense of the New CSS Layout
Flexbox and Grid Layout
Start Using CSS Grid Layout Today - RuhrJS
DevFest Nantes - Start Using CSS Grid Layout today
Evergreen websites for Evergreen browsers
View Source London: Solving Layout Problems with CSS Grid & Friends
Solving Layout Problems with CSS Grid & Friends - DevFest17
What I discovered about layout vis CSS Grid
Solving Layout Problems with CSS Grid & Friends - NordicJS
Google Developers Experts Summit 2017 - CSS Layout
404.ie: Solving Layout Problems with CSS Grid & Friends
An Event Apart SF: CSS Grid Layout
Solving Layout Problems With CSS Grid and Friends
Solving Layout Problems with CSS Grid & Friends - WEBU17
CSS Grid Layout
Devoxx Belgium: CSS Grid Layout
CSS Grid Layout for Topconf, Linz
Ad

More from Rachel Andrew (12)

PDF
All Day Hey! Unlocking The Power of CSS Grid Layout
PDF
SmashingConf SF: Unlocking the Power of CSS Grid Layout
PDF
Unlocking the Power of CSS Grid Layout
PDF
The Creative New World of CSS
PDF
Into the Weeds of CSS Layout
PDF
Graduating to Grid
PDF
Laying out the future with grid & flexbox - Smashing Conf Freiburg
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?
All Day Hey! Unlocking The Power of CSS Grid Layout
SmashingConf SF: Unlocking the Power of CSS Grid Layout
Unlocking the Power of CSS Grid Layout
The Creative New World of CSS
Into the Weeds of CSS Layout
Graduating to Grid
Laying out the future with grid & flexbox - Smashing Conf Freiburg
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?

Recently uploaded (20)

PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
cuic standard and advanced reporting.pdf
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
[발표본] 너의 과제는 클라우드에 있어_KTDS_김동현_20250524.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PPTX
Cloud computing and distributed systems.
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
cuic standard and advanced reporting.pdf
GamePlan Trading System Review: Professional Trader's Honest Take
Chapter 3 Spatial Domain Image Processing.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Advanced methodologies resolving dimensionality complications for autism neur...
20250228 LYD VKU AI Blended-Learning.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
[발표본] 너의 과제는 클라우드에 있어_KTDS_김동현_20250524.pdf
MYSQL Presentation for SQL database connectivity
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Cloud computing and distributed systems.
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Dropbox Q2 2025 Financial Results & Investor Presentation

Making the most of New CSS Layout

  • 1. Making the most of New CSS Layout Rachel Andrew, 
 SmartWeb, Bucharest 2016
  • 2. Rachel Andrew rachelandrew.co.uk @rachelandrew CSS Working Group Invited Expert Google Developer Expert for Web Technologies Co-founder Perch CMS: https://grabaperch.com Contact: [email protected]
  • 3. Modern CSS Layout? • Floats • Inline-block • display: table • Absolute & Relative positioning • Frameworks … lots of frameworks
  • 4. Snippet from Bootstrap Grid mark-up. <div class="row"> <div class="col-md-8"> .col-md-8 <div class="row"> <div class="col-md-6"> .col-md-6 </div> <div class="col-md-6"> .col-md-6 </div> </div> </div> <div class="col-md-4"> .col-md-4 </div> </div>
  • 5. Our great hopes for layout • Flexbox
 https://drafts.csswg.org/css-flexbox/ • CSS Grid Layout
 https://drafts.csswg.org/css-grid/ • Box Alignment
 https://drafts.csswg.org/css-align/
  • 9. The new CSS for Layout
  • 10. Items in our layouts understand themselves as part of a complete layout.
  • 13. Flexbox Full height columns with flexbox, taking advantage of default alignment values. .wrapper { display: flex; } .sidebar { flex: 1 1 30%; } .content { flex: 1 1 70%; }
  • 14. Grid Layout Full height columns in CSS Grid Layout. .wrapper { display: grid; grid-template-columns: 30% 70%; } .sidebar { grid-column: 1; } .content { grid-column: 2; }
  • 15. Separation of source and display
  • 16. Flexbox The flex-direction property can take a value of row to display things as a row or column to display them as a column. nav ul{ display: flex; justify-content: space-between; flex-direction: row; }
  • 17. Flexbox The visual order can be switched using row-reverse or column- reverse. nav ul{ display: flex; justify-content: space-between; flex-direction: row-reverse; }
  • 18. Flexbox Adding display: flex to our container element causes the items to display flexibly in a row. .wrapper { display: flex; }
  • 19. Flexbox The order property means we can change the order of flex items using CSS. This does not change their source order. li:nth-child(1) { order: 3; } li:nth-child(2) { order: 1; } li:nth-child(3) { order: 4; } li:nth-child(4) { order: 2; }
  • 20. Grid Layout I have created a grid on my wrapper element. The grid has 3 equal width columns. Rows will be created as required as we position items into them. .wrapper { display: grid; grid-template-columns: 1fr 1fr 1fr; }
  • 21. Grid Layout I am positioning my elements using CSS Grid Layout line-based positioning. Setting a column and a row line using the grid- column and grid- row properties. li:nth-child(1) { grid-column: 3 ; grid-row: 2 ; } li:nth-child(2) { grid-column: 1 ; grid-row: 2 ; } li:nth-child(3) { grid-column: 1 ; grid-row: 1 ; } li:nth-child(4) { grid-column: 2 ; grid-row: 1 ; }
  • 23. CSS Grid automatic placement http://www.w3.org/TR/2015/WD-css-grid-1-20150917/#grid- auto-flow-property https://rachelandrew.co.uk/archives/2015/04/14/grid-layout- automatic-placement-and-packing-modes/
  • 25. Grid Layout When using automatic placement we can create rules for items in our document - for example displaying portrait and landscape images differently. .wrapper { display: grid; grid-template-columns: 1fr 1fr 1fr 1fr; grid-template-rows: auto; } .landscape { grid-column-end: span 2; }
  • 26. grid-auto- flow The default value of grid- auto-flow is sparse. Grid will move forward planning items skipping cells if items do not fit .
  • 27. Grid Layout With a dense packing mode grid will move items out of source order to backfill spaces. .wrapper { display: grid; grid-template-columns: 1fr 1fr 1fr 1fr; grid-template-rows: auto; grid-auto-flow: dense; } .landscape { grid-column-end: span 2; }
  • 28. grid-auto- flow With grid-auto-flow dense items are displayed out of source order. Grid backfills any suitable gaps.
  • 29. With great power comes responsibility.
  • 30. Power and responsibility • Good = creating the most accessible source order and using Grid or Flexbox to get the optimal display for each device. • Bad = using Grid or Flexbox as an excuse to forget about the source. • Terrible - stripping out semantic elements to make everything a grid or flex item.
  • 31. https://drafts.csswg.org/css-flexbox/#order-accessibility Authors must use order only for visual, not logical, reordering of content. Style sheets that use order to perform logical reordering are non- conforming.
  • 32. Léonie Watson | On CSS accessibility and drinking tea | CSS Day 2016 https://vimeo.com/180566024
  • 34. CSS Box Alignment Module Level 3 “This module contains the features of CSS relating to the alignment of boxes within their containers in the various CSS box layout models: block layout, table layout, flex layout, and grid layout.” - https://drafts.csswg.org/css-align/
  • 35. It’s 2016. We can now centre things.
  • 36. Box Alignment Properties - justify-content - align-content - justify-self - align-self - justify-items - align-items
  • 37. Flexbox The justify- content property is set to space- between. The items at each end are placed against the container and the remaining space distributed evenly. nav ul{ display: flex; justify-content: space-between; flex-direction: row; }
  • 38. Flexbox The justify- content property is set to space- around. The items are evenly distributed in the container with a half size space at each end. nav ul{ display: flex; justify-content: space-around; flex-direction: row; }
  • 39. Grid If there is space in the grid container after all column and row tracks have been added. Use space-around and space- between to space the tracks. .wrapper { width: 500px; height: 400px; display: grid; grid-gap: 10px; grid-template-columns: repeat(4, 80px); grid-template-rows: repeat(3,100px); align-content: space-around; justify-content: space-between; }
  • 40. Flexbox The value of align- items is stretch by default. If I add extra text in one navigation point the others all take the same height. nav ul{ display: flex; justify-content: space-around; flex-direction: row; align-items: stretch; }
  • 41. Flexbox If I set the value of align-items to center then we get vertical centring. nav ul{ display: flex; justify-content: space-around; flex-direction: row; align-items: center; }
  • 42. My grid has an absolute width and height. This is larger than required for the tracks. .wrapper { width: 500px; height: 400px; display: grid; grid-gap: 10px; grid-template-columns: repeat(4, 80px); grid-template-rows: repeat(3,100px); }
  • 43. The align-content property controls the block axis. This axis aligns the grid rows. .wrapper { width: 500px; height: 400px; display: grid; grid-gap: 10px; grid-template-columns: repeat(4, 80px); grid-template-rows: repeat(3,100px); align-content: end; }
  • 44. The justify- content property controls the inline axis. This axis aligns the grid columns. .wrapper { width: 500px; height: 400px; display: grid; grid-gap: 10px; grid-template-columns: repeat(4, 80px); grid-template-rows: repeat(3,100px); align-content: end; justify-content: center; }
  • 45. I can create this same layout with flexbox or Grid. With flexbox the items are laid out in a row. .wrapper { display: flex; } .wrapper li { flex: 1 0 25%; }
  • 46. The first item is at the default stretch and as the tallest item is dictating the height of the flex container. The second is entered in the container. The third aligned to the start. The fourth aligned to the end. .wrapper li:nth-child(2) { align-self: center; } .wrapper li:nth-child(3) { align-self: flex-start; } .wrapper li:nth-child(4) { align-self: flex-end; }
  • 47. For Grid I use a single row, 4 column Grid. .wrapper { display: grid; grid-template-columns: 1fr 1fr 1fr 1fr; }
  • 48. Grid alignment properties for the three landscape images. .wrapper li:nth-child(2) { align-self: center; } .wrapper li:nth-child(3) { align-self: start; } .wrapper li:nth-child(4) { align-self: end; }
  • 51. Aligning the grid. body { height: 100vh; display: grid; grid-template-columns: 80%; align-content: center; justify-content: center; }
  • 53. 1fr 1fr 1fr 1fr 1fr 2fr 2fr .login > div .login > div .login > div.actions
  • 54. Setting align- items to centre lines the fields and labels up on their centre line. .login > div { display: grid; grid-template-columns: 1fr 2fr; align-items: center; } .login > div.actions { grid-template-columns: 1fr 1fr 1fr; }
  • 56. Ethan Marcotte, Fluid Grids “… every aspect of the grid—and the elements laid upon it—can be expressed as a proportion relative to its container.”
  • 57. target ÷ context = result h1 { margin-left: 14.575%; /* 144px / 988px = 0.14575 */ width: 70.85%; /* 700px / 988px = 0.7085 */ }
  • 58. Flexbox The most simple flexbox example demonstrates the inherent flexibility. The items will be displayed as a row, with equal space between each item. nav ul{ display: flex; justify-content: space-between; }
  • 59. The flex property • flex-grow - add space • flex-shrink - remove space • flex-basis - the initial size before any growing or shrinking
  • 60. Flexbox flex: 1 1 300px; flex-grow: 1 flex-shrink: 1; flex-basis: 300px; The initial width of our li is 300 pixels, however it can grow larger and shrink smaller than 300 pixels. .wrapper { display: flex; } .wrapper li { flex: 1 1 300px; min-width: 1px; }
  • 62. Flexbox flex: 1 1 300px; flex-grow: 1 flex-shrink: 1; flex-basis: 300px; If we allow the flex items to wrap we can see how flex-basis works by dragging the window smaller. .wrapper { display: flex; flex-flow: row wrap; justify-content: space-around; } .wrapper li { flex: 1 1 300px; min-width: 1px; }
  • 64. Flexbox flex: 1 1 300px; flex-grow: 1; flex-shrink: 1; flex-basis: 300px; The 3rd item has flex: 0 1 300px; so cannot grow. .wrapper { display: flex; } .wrapper li { flex: 1 1 300px; min-width: 1px; } .wrapper li:nth-child(3) { flex: 0 1 300px; }
  • 66. Flexbox If we set the 3rd item to
 flex-grow: 2 This item will be assigned twice of much of the available free space after we have reached the 300 pixel initial width. .wrapper { display: flex; } .wrapper li { flex: 1 1 300px; min-width: 1px; } .wrapper li:nth-child(3) { flex: 2 1 300px; }
  • 69. The CSS Grid Layout fr unit
  • 70. Grid Layout I am creating three grid column tracks, all 1fr in width. This gives me three equally sized column tracks. .wrapper { display: grid; grid-template-columns: 1fr 1fr 1fr; }
  • 71. Grid Layout If I create the first column as 600 pixels and then have two 1fr columns the 600 pixel track is removed from the available space and the remainder is distributed equally between the two columns. .wrapper { display: grid; grid-template-columns: 600px 1fr 1fr; }
  • 72. Grid Layout With a 600 pixel column, a 1fr and a 3fr column. The 600 pixels is removed from the available space then the remaining space is divided by 4. The 1fr column gets 25% and the 3fr column 75%. .wrapper { display: grid; grid-template-columns: 600px 1fr 3fr; }
  • 74. Flexbox for 1 dimensional layout. CSS Grid is for 2 dimensional layout.
  • 76. The value of the grid-template- columns property says: repeat this track listing, auto-filing as many columns with a minimum width of 300 pixels and a maximum of 1fr. .wrapper { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); }
  • 82. Using the minmax() function with grid-auto-rows. .home-hero { display: grid; grid-gap: 1px; grid-auto-rows: minmax(150px, auto); }
  • 83. An item on the grid can become a grid or flex container itself. In this case I am using flexbox and auto margins to push my content to the bottom of the box. .special { display: flex; flex-direction: column; } .special h3{ margin-top: auto; }
  • 86. Say hello to Feature Queries.
  • 87. I have set three classes to display: none; .has-flex, .has-grid, .has-grid-shapes { display: none; }
  • 88. My @supports rule tests for support of the display property with a value of flex. If it is supported we show the div. @supports (display: flex) { .has-flex { display: block; } }
  • 89. My @supports rule tests for support of the display property with a value of grid. If it is supported we show the div. @supports (display: grid) { .has-grid { display: block; } }
  • 90. My @supports rule tests for support of the display property with a value of grid AND shape- outside:circle. If it is supported we show the div. @supports (display: grid) and (shape-outside:circle()) { .has-grid-shapes { display: block; } }
  • 93. Defaults for all browsers will be loaded by even really old browsers. body { padding-top: 20%; } h1, .login, .account, .contact{ width:80%; margin: 0 auto; }
  • 95. Within a Feature Query we add some information for flexbox supporting browsers. @supports (display: flex) { body { padding:0; height: 100vh; display: flex; align-items: center; justify-content: center; flex-direction: column; } h1, .login, .account, .contact { margin: 0; width: 80%; } }
  • 97. The Feature Query for Grid supporting browsers. @supports (display: grid) { body { display: grid; grid-template-columns: 80%; align-content: center; align-items: stretch; } @media (min-width: 650px) { body { grid-template-columns: repeat(2, minmax(150px, 30%)); } h1, .login { grid-column-end: span 2; width: auto; } .login > div { display: grid; grid-template-columns: 1fr 2fr; align-items: center; } .login > div.actions { grid-template-columns: 1fr 1fr 1fr; } .account { border-right: 1px dotted rgb(191, 216, 227); padding: 0 10px 0 0; align-self: end; width: auto; } .contact { padding: 0 0 0 10px; width: auto; } } }
  • 98. Your users ‘grow into’ enhancements as their browsers auto-update.
  • 101. Why are we looking at something I can’t use yet?
  • 103. Get involved with developing specs! • While a spec is being developed your feedback is wanted and can be included in the spec. • Wait until browsers ship and you lose that chance. • It just got easier. CSS Spec issues are now on GitHub.
 http://logs.csswg.org/irc.w3.org/css/2016-05-10/#e684439
  • 104. Do a good deed for your future self.
  • 105. Thank you Slides & Resources: https://cssgrid.me/SmartWeb16-slides http://csslayout.news - sign up for my weekly CSS Layout email — @rachelandrew | [email protected] — https://rachelandrew.co.uk | https://grabaperch.com