SlideShare a Scribd company logo
Making Sense
of CSS Layout
Rachel Andrew, ConFoo 2016
https://www.flickr.com/photos/nossreh/5510993121/
Rachel Andrew
Blogging about tech/business and other things at rachelandrew.co.uk
On Twitter and other places as @rachelandrew
Co-founder of Perch & Perch Runway CMS, see: grabaperch.com
Teaching CSS Layout at thecssworkshop.com
Google Developer Expert for Web Technologies
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>
The cost of taming layout methods
• Developer hours spent learning non-obvious
concepts.
• Compromises in terms of document semantics in
order to achieve responsive layouts.
• Needing to lean on frameworks to help with
complex maths.
• Adding markup to create grids
• Using preprocessors to abstract layout hacks
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/
The new CSS for Layout
• Items in our layouts understand themselves as
part of an overall layout.
• True separation of document source order and
visual display.
• Precise control of alignment - horizontally and
vertically.
• Responsive and flexible by default.
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 / 4 ;
grid-row: 2 / 3;
}
li:nth-child(2) {
grid-column: 1 / 2;
grid-row: 2 / 3;
}
li:nth-child(3) {
grid-column: 1 / 2;
grid-row: 1 / 2;
}
li:nth-child(4) {
grid-column: 2 / 3;
grid-row: 1 / 2;
}
ConFoo 2016: Making Sense of 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/
ConFoo 2016: Making Sense of 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.
https://rachelandrew.co.uk/archives/2015/07/28/modern-css-layout-power-and-responsibility/
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/
Vertically centre ALL THE THINGS!
Distributed alignment
justify-content and align-content properties.
Values: space-between, space-around, stretch, space-evenly
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;
}
Default alignment
Used by the justify-items and align-items properties.
The align-items and justify-items properties set the default align-
self and justify-self behavior of the items contained by the
element.
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;
}
Flexbox
If flex-direction is column
and I set the value of align-
items to center then we
get horizontal centring.
nav ul{
display: flex;
justify-content: space-around;
flex-direction: column;
align-items: center;
}
Self alignment
justify-self and align-self properties.
The justify-self and align-self properties control alignment of the
box within its containing block.
Flexbox
You can use the align-self
and justify-self properties
to target individual flex
items.
In this example I have set
the group to centre, but
the third item to stretch.
nav ul{
display: flex;
justify-content: space-around;
flex-direction: row;
align-items: center;
}
nav li:nth-child(3) {
align-self: stretch;
}
Box alignment in CSS Grid Layout
Grid Layout
Creating a grid with the
align-items property set
to centre.
All items will be centered
inside their grid area.
.wrapper {
display: grid;
align-items: center;
grid-template-columns: repeat(5, 150px 10px) 150px;
grid-template-rows: 150px 10px 150px 10px 150px 10px 150px;
}
.a {
grid-column: 1 / 4;
grid-row: 1 / 4;
}
.b {
grid-column: 5 / 8;
grid-row: 1 / 4;
}
.c {
grid-column: 1 / 4;
grid-row: 5 / 10;
}
.d {
grid-column: 5 / 8;
grid-row: 5 / 10;
}
.e {
grid-column: 9 / 12;
grid-row: 1 / 10;
}
http://gridbyexample.com/examples/#example24
Grid Layout
Creating a grid with the
justify-items property set
to centre.
All items will be centered
horizontally inside their
grid area.
.wrapper {
display: grid;
justify-items: center;
grid-template-columns: repeat(5, 150px 10px) 150px;
grid-template-rows: 150px 10px 150px 10px 150px 10px
150px;
}
.a {
grid-column: 1 / 4;
grid-row: 1 / 4;
}
.b {
grid-column: 5 / 8;
grid-row: 1 / 4;
}
.c {
grid-column: 1 / 4;
grid-row: 5 / 10;
}
.d {
grid-column: 5 / 8;
grid-row: 5 / 10;
}
.e {
grid-column: 9 / 12;
grid-row: 1 / 10;
}
http://gridbyexample.com/examples/#example25
Grid Layout
As with flexbox we can
use align-self and justify-
self to target individual
grid items.
.a {
grid-column: 1 / 4;
grid-row: 1 / 4;
align-self: stretch;
}
.b {
grid-column: 5 / 8;
grid-row: 1 / 4;
align-self: end;
}
.c {
grid-column: 1 / 4;
grid-row: 5 / 10;
align-self: start;
}
.d {
grid-column: 5 / 8;
grid-row: 5 / 10;
align-self: center;
}
.e {
grid-column: 9 / 12;
grid-row: 1 / 10;
}
http://gridbyexample.com/examples/#example26
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
https://drafts.csswg.org/css-flexbox/#flex-components
Authors are encouraged to control
flexibility using the flex shorthand rather
than with its longhand properties
directly, as the shorthand correctly
resets any unspecified components to
accommodate common uses.
Flexbox
flex: 1 1 200px;
flex-grow: 1
flex-shrink: 1;
flex-basis: 200px;
The initial width of our
box is 200 pixels,
however it can grow
larger and shrink smaller
than 200 pixels.
.boxes {
display: flex;
justify-content: space-around;
}
.box {
flex: 1 1 200px;
min-width: 1px;
}
ConFoo 2016: Making Sense of CSS Layout
Flexbox
flex: 1 1 200px;
flex-grow: 1
flex-shrink: 1;
flex-basis: 200px;
If we allow the flex items
to wrap we can see how
flex-basis works by
dragging the window
smaller.
.boxes {
display: flex;
flex-flow: row wrap;
justify-content: space-around;
}
.box {
flex: 1 1 200px;
min-width: 1px;
}
ConFoo 2016: Making Sense of CSS Layout
Flexbox
flex: 0 1 200px;
flex-grow: 0
flex-shrink: 1;
flex-basis: 200px;
The initial width of our
box is 200 pixels, it can
shrink smaller than 200
pixels but may not get
larger.
.boxes {
display: flex;
justify-content: space-around;
}
.box {
flex: 0 1 200px;
min-width: 1px;
}
ConFoo 2016: Making Sense of CSS Layout
Flexbox
flex: 1 1 200px;
flex-grow: 1;
flex-shrink: 1;
flex-basis: 200px;
.box3 has been set to
flex: 0 1 200px;
so cannot grow.
.boxes {
display: flex;
justify-content: space-around;
}
.box {
flex: 1 1 200px;
min-width: 1px;
}
.box3 {
flex: 0 1 200px;
}
ConFoo 2016: Making Sense of CSS Layout
Flexbox
If we set box3 to

flex-grow: 2
This box will be assigned
twice of much of the
available free space after
we have reached the 200
pixel initial width.
.boxes {
display: flex;
justify-content: space-around;
}
.box {
flex: 1 1 200px;
min-width: 1px;
}
.box3 {
2 1 200px;
}
ConFoo 2016: Making Sense of 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;
}
http://alistapart.com/article/holygrail
Three columns. One fixed-width
sidebar for your navigation, another
for, say, your Google Ads or your Flickr
photos—and, as in a fancy truffle, a
liquid center for the real substance.
Grid Layout
CSS Grid “Holy Grail”
using grid-template-
areas.
//css
.header { grid-area: header;}
.footer { grid-area: footer;}
.side1 { grid-area: nav;}
.side2 { grid-area: ads;}
.content { grid-area: content;}
.wrapper {
display: grid;
grid-template-columns: 300px 20px 1fr 20px 300px;
grid-template-rows: auto;
grid-template-areas:
"header header header header header"
"nav ...... content ...... ads"
"footer footer footer footer footer"
;
}
//html
<div class="wrapper">
<header class="header">This is the header</header>
<article class="content"></article>
<div class="side1"></div>
<div class="side2"></div>
<footer class="footer"></div>
</div>
ConFoo 2016: Making Sense of CSS Layout
CSS Anthology, 2004
Q. Is it a bad thing to use effects that
don’t work in some browsers?
2004 Rachel
“Users [of IE] might see square corners
instead of rounded ones […] but they’ll
be able to use the site just as well as
their Mozilla-wielding counterparts.”
It’s one thing to have no rounded
corners. Quite another to have no
layout in an older browser.
http://positioniseverything.net/explorer.html
We need to think about feature
support differently in a world of
evergreen browsers.
Evergreen browsers mean we can
enhance with newer techniques.
Over time our users see the site
incrementally improve.
CSS Grid Layout
• Early implementation in IE10, 11 and current Edge
• Implementation of most of the current spec
behind a flag in Blink
• Prefixed in Webkit Nightlies
• Partial implementation in Firefox Nightlies
• Edge have updating to current spec as ‘High
Priority’ on the backlog
Start with small UI elements
• Your layout doesn’t have to be all flexbox or
nothing.
• Build your layout using the methods that work
for your audience profile.
• Finesse using more modern methods
Grid Layout
Display a dl in two
columns using Grid.
dt elements alway start
on the left, dd on the
right.
@media (min-width: 550px) {
.grid-dl {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: auto;
}
.grid-dl dt {
grid-column-start: 1;
}
.grid-dl dd {
grid-column-start: 2;
}
.grid-dl dt+dd {
border-top: 2px solid #ccc;
}
}
http://codepen.io/rachelandrew/pen/wKgePy
http://codepen.io/rachelandrew/pen/wKgePy
Flexbox in the real world
Zoe M. Gillenwater - Enhancing Responsiveness With Flexbox
https://www.youtube.com/watch?v=_98SE8WUvLk
Check your stats before making
assumptions about support.
http://get.gaug.es/
Separate desktop and mobile
statistics - your mobile users may
well have far more capability.
Use modern methods for
prototyping and benefit from rapid
design in the browser.
Take an interest in early stage
specifications in order to feed back
to the process. Have your say.
http://gridbyexample.com
Thank you
Slides & Resources: 

https://rachelandrew.co.uk/presentations/modern-css-layout
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
Fluent: Making Sense of the New CSS Layout
PDF
Flexbox and Grid Layout
PDF
Future Layout & Performance
PDF
The New CSS Layout - dotCSS
PDF
But what about old browsers?
PDF
Flexbox and Grid Layout
PDF
An Event Apart SF: CSS Grid Layout
PDF
CSS Grid Layout for Topconf, Linz
Fluent: Making Sense of the New CSS Layout
Flexbox and Grid Layout
Future Layout & Performance
The New CSS Layout - dotCSS
But what about old browsers?
Flexbox and Grid Layout
An Event Apart SF: CSS Grid Layout
CSS Grid Layout for Topconf, Linz

What's hot (20)

PDF
CSS Conf Budapest - New CSS Layout
PDF
CSS Day: CSS Grid Layout
PDF
The New CSS Layout - Dutch PHP Conference
PDF
An Event Apart Nashville: CSS Grid Layout
PDF
Devoxx Belgium: CSS Grid Layout
PDF
CSS Grid Layout: An Event Apart Boston 2016
PDF
CSS Grid vs. Flexbox
PDF
CSS Grid Layout
PDF
Grids of Tomorrow: CSS Grid Layout
PDF
CSS Grid Layout Introduction
PDF
Talk Web Design: Get Ready For CSS Grid Layout
PDF
CSS Grid Layout. Implementation status and roadmap (Webkit Contributors Meeti...
PDF
Making the most of New CSS Layout
PDF
PDF
Render Conf: Start using CSS Grid Layout Today
PDF
Introduction to CSS Grid Layout
PDF
Laracon Online: Grid and Flexbox
PDF
AEA Chicago CSS Grid Layout
PDF
CSS Grid Changes Everything About Web Layouts: WordCamp Europe 2017
PDF
The Future of CSS Layout
CSS Conf Budapest - New CSS Layout
CSS Day: CSS Grid Layout
The New CSS Layout - Dutch PHP Conference
An Event Apart Nashville: CSS Grid Layout
Devoxx Belgium: CSS Grid Layout
CSS Grid Layout: An Event Apart Boston 2016
CSS Grid vs. Flexbox
CSS Grid Layout
Grids of Tomorrow: CSS Grid Layout
CSS Grid Layout Introduction
Talk Web Design: Get Ready For CSS Grid Layout
CSS Grid Layout. Implementation status and roadmap (Webkit Contributors Meeti...
Making the most of New CSS Layout
Render Conf: Start using CSS Grid Layout Today
Introduction to CSS Grid Layout
Laracon Online: Grid and Flexbox
AEA Chicago CSS Grid Layout
CSS Grid Changes Everything About Web Layouts: WordCamp Europe 2017
The Future of CSS Layout
Ad

Viewers also liked (20)

PPTX
XE 오픈 세미나(2014-06-28) - (1/3) 레이아웃 제작 기본
PDF
4주 CSS Layout
PDF
CSS Layout
PDF
UX Layout Design_SYS4U
PPTX
Microsoft opensource
 
PPT
Pgt432 PBLproject
PDF
Priklady na pravdepodobnost
PDF
Hp All In 1
PDF
Unidad_2_Internet
PDF
Panduit ita
PDF
Открытые информационные источники для проверки контрагентов
PPTX
World Heritage in Danger
PPTX
Microsoft opensource
 
PDF
Asia-Pacific | Business Wire
PPTX
Intro to Creditera for SBDC
PPT
Cool twitterconference taphunter
PPTX
SEOGuardian - sistemi di climatizzazione
PPS
Civilizacao Solar
PDF
Facebook dmk proprty
PDF
Product Media Magazine: January - February 2016
XE 오픈 세미나(2014-06-28) - (1/3) 레이아웃 제작 기본
4주 CSS Layout
CSS Layout
UX Layout Design_SYS4U
Microsoft opensource
 
Pgt432 PBLproject
Priklady na pravdepodobnost
Hp All In 1
Unidad_2_Internet
Panduit ita
Открытые информационные источники для проверки контрагентов
World Heritage in Danger
Microsoft opensource
 
Asia-Pacific | Business Wire
Intro to Creditera for SBDC
Cool twitterconference taphunter
SEOGuardian - sistemi di climatizzazione
Civilizacao Solar
Facebook dmk proprty
Product Media Magazine: January - February 2016
Ad

Similar to ConFoo 2016: Making Sense of CSS Layout (20)

PDF
The Right Layout Tool for the Job
PDF
Better Layouts with Flexbox + CSS Grids
PDF
Laying out the future
PDF
CSSConf.asia - Laying out the future
PDF
Confoo: The New CSS Layout
PDF
The Near Future of CSS
PDF
Start Using CSS Grid Layout Today - RuhrJS
PDF
Grid and Flexbox - Smashing Conf SF
PDF
Evergreen websites for Evergreen browsers
PDF
DevFest Nantes - Start Using CSS Grid Layout today
PDF
Confoo: You can use CSS for that!
PDF
Frontend United: Start using CSS Grid Layout today!
PDF
Laying out the future with grid & flexbox - Smashing Conf Freiburg
PDF
Next-level CSS
PDF
The Future of Frontend - what is new in CSS?
PDF
Google Developers Experts Summit 2017 - CSS Layout
PDF
The Creative New World of CSS
PDF
What I discovered about layout vis CSS Grid
PDF
CSS Grid Layout
PPTX
2D Page Layout
The Right Layout Tool for the Job
Better Layouts with Flexbox + CSS Grids
Laying out the future
CSSConf.asia - Laying out the future
Confoo: The New CSS Layout
The Near Future of CSS
Start Using CSS Grid Layout Today - RuhrJS
Grid and Flexbox - Smashing Conf SF
Evergreen websites for Evergreen browsers
DevFest Nantes - Start Using CSS Grid Layout today
Confoo: You can use CSS for that!
Frontend United: Start using CSS Grid Layout today!
Laying out the future with grid & flexbox - Smashing Conf Freiburg
Next-level CSS
The Future of Frontend - what is new in CSS?
Google Developers Experts Summit 2017 - CSS Layout
The Creative New World of CSS
What I discovered about layout vis CSS Grid
CSS Grid Layout
2D Page Layout

More from Rachel Andrew (19)

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
Into the Weeds of CSS Layout
PDF
Solving Layout Problems with CSS Grid & Friends - DevFest17
PDF
Graduating to Grid
PDF
View Source London: Solving Layout Problems with CSS Grid & Friends
PDF
404.ie: Solving Layout Problems with CSS Grid & Friends
PDF
Solving Layout Problems with CSS Grid & Friends - WEBU17
PDF
Solving Layout Problems with CSS Grid & Friends - NordicJS
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
An Event Apart Seattle - New CSS Layout Meets the Real World
PDF
Introducing CSS Grid Layout
PDF
CSS Grid Layout for Frontend NE
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
Into the Weeds of CSS Layout
Solving Layout Problems with CSS Grid & Friends - DevFest17
Graduating to Grid
View Source London: Solving Layout Problems with CSS Grid & Friends
404.ie: Solving Layout Problems with CSS Grid & Friends
Solving Layout Problems with CSS Grid & Friends - WEBU17
Solving Layout Problems with CSS Grid & Friends - NordicJS
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
An Event Apart Seattle - New CSS Layout Meets the Real World
Introducing CSS Grid Layout
CSS Grid Layout for Frontend NE

Recently uploaded (20)

PPTX
international classification of diseases ICD-10 review PPT.pptx
PPTX
SAP Ariba Sourcing PPT for learning material
PDF
An introduction to the IFRS (ISSB) Stndards.pdf
PPTX
Module 1 - Cyber Law and Ethics 101.pptx
PPTX
Digital Literacy And Online Safety on internet
PDF
Triggering QUIC, presented by Geoff Huston at IETF 123
PPTX
QR Codes Qr codecodecodecodecocodedecodecode
PPTX
presentation_pfe-universite-molay-seltan.pptx
PDF
APNIC Update, presented at PHNOG 2025 by Shane Hermoso
PDF
The New Creative Director: How AI Tools for Social Media Content Creation Are...
PPTX
Introuction about WHO-FIC in ICD-10.pptx
PPTX
Introduction about ICD -10 and ICD11 on 5.8.25.pptx
PPTX
522797556-Unit-2-Temperature-measurement-1-1.pptx
PDF
Paper PDF World Game (s) Great Redesign.pdf
PDF
Tenda Login Guide: Access Your Router in 5 Easy Steps
PDF
Unit-1 introduction to cyber security discuss about how to secure a system
PPTX
CHE NAA, , b,mn,mblblblbljb jb jlb ,j , ,C PPT.pptx
PPTX
Introduction to Information and Communication Technology
PDF
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
PDF
The Internet -By the Numbers, Sri Lanka Edition
international classification of diseases ICD-10 review PPT.pptx
SAP Ariba Sourcing PPT for learning material
An introduction to the IFRS (ISSB) Stndards.pdf
Module 1 - Cyber Law and Ethics 101.pptx
Digital Literacy And Online Safety on internet
Triggering QUIC, presented by Geoff Huston at IETF 123
QR Codes Qr codecodecodecodecocodedecodecode
presentation_pfe-universite-molay-seltan.pptx
APNIC Update, presented at PHNOG 2025 by Shane Hermoso
The New Creative Director: How AI Tools for Social Media Content Creation Are...
Introuction about WHO-FIC in ICD-10.pptx
Introduction about ICD -10 and ICD11 on 5.8.25.pptx
522797556-Unit-2-Temperature-measurement-1-1.pptx
Paper PDF World Game (s) Great Redesign.pdf
Tenda Login Guide: Access Your Router in 5 Easy Steps
Unit-1 introduction to cyber security discuss about how to secure a system
CHE NAA, , b,mn,mblblblbljb jb jlb ,j , ,C PPT.pptx
Introduction to Information and Communication Technology
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
The Internet -By the Numbers, Sri Lanka Edition

ConFoo 2016: Making Sense of CSS Layout

  • 1. Making Sense of CSS Layout Rachel Andrew, ConFoo 2016 https://www.flickr.com/photos/nossreh/5510993121/
  • 2. Rachel Andrew Blogging about tech/business and other things at rachelandrew.co.uk On Twitter and other places as @rachelandrew Co-founder of Perch & Perch Runway CMS, see: grabaperch.com Teaching CSS Layout at thecssworkshop.com Google Developer Expert for Web Technologies Contact: me@rachelandrew.co.uk
  • 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. The cost of taming layout methods • Developer hours spent learning non-obvious concepts. • Compromises in terms of document semantics in order to achieve responsive layouts. • Needing to lean on frameworks to help with complex maths. • Adding markup to create grids • Using preprocessors to abstract layout hacks
  • 6. 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/
  • 7. The new CSS for Layout • Items in our layouts understand themselves as part of an overall layout. • True separation of document source order and visual display. • Precise control of alignment - horizontally and vertically. • Responsive and flexible by default.
  • 8. Items in our layouts understand themselves as part of a complete layout.
  • 11. 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%; }
  • 12. 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; }
  • 13. Separation of source and display
  • 14. 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; }
  • 15. 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; }
  • 16. Flexbox Adding display: flex to our container element causes the items to display flexibly in a row. .wrapper { display: flex; }
  • 17. 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; }
  • 18. 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; }
  • 19. 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 / 4 ; grid-row: 2 / 3; } li:nth-child(2) { grid-column: 1 / 2; grid-row: 2 / 3; } li:nth-child(3) { grid-column: 1 / 2; grid-row: 1 / 2; } li:nth-child(4) { grid-column: 2 / 3; grid-row: 1 / 2; }
  • 21. 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/
  • 23. 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; }
  • 24. 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 .
  • 25. 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; }
  • 26. grid-auto-flow With grid-auto-flow dense items are displayed out of source order. Grid backfills any suitable gaps.
  • 27. With great power comes responsibility.
  • 28. 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.
  • 29. 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. 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/
  • 33. Vertically centre ALL THE THINGS!
  • 34. Distributed alignment justify-content and align-content properties. Values: space-between, space-around, stretch, space-evenly
  • 35. 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; }
  • 36. 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; }
  • 37. Default alignment Used by the justify-items and align-items properties. The align-items and justify-items properties set the default align- self and justify-self behavior of the items contained by the element.
  • 38. 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; }
  • 39. 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; }
  • 40. Flexbox If flex-direction is column and I set the value of align- items to center then we get horizontal centring. nav ul{ display: flex; justify-content: space-around; flex-direction: column; align-items: center; }
  • 41. Self alignment justify-self and align-self properties. The justify-self and align-self properties control alignment of the box within its containing block.
  • 42. Flexbox You can use the align-self and justify-self properties to target individual flex items. In this example I have set the group to centre, but the third item to stretch. nav ul{ display: flex; justify-content: space-around; flex-direction: row; align-items: center; } nav li:nth-child(3) { align-self: stretch; }
  • 43. Box alignment in CSS Grid Layout
  • 44. Grid Layout Creating a grid with the align-items property set to centre. All items will be centered inside their grid area. .wrapper { display: grid; align-items: center; grid-template-columns: repeat(5, 150px 10px) 150px; grid-template-rows: 150px 10px 150px 10px 150px 10px 150px; } .a { grid-column: 1 / 4; grid-row: 1 / 4; } .b { grid-column: 5 / 8; grid-row: 1 / 4; } .c { grid-column: 1 / 4; grid-row: 5 / 10; } .d { grid-column: 5 / 8; grid-row: 5 / 10; } .e { grid-column: 9 / 12; grid-row: 1 / 10; }
  • 46. Grid Layout Creating a grid with the justify-items property set to centre. All items will be centered horizontally inside their grid area. .wrapper { display: grid; justify-items: center; grid-template-columns: repeat(5, 150px 10px) 150px; grid-template-rows: 150px 10px 150px 10px 150px 10px 150px; } .a { grid-column: 1 / 4; grid-row: 1 / 4; } .b { grid-column: 5 / 8; grid-row: 1 / 4; } .c { grid-column: 1 / 4; grid-row: 5 / 10; } .d { grid-column: 5 / 8; grid-row: 5 / 10; } .e { grid-column: 9 / 12; grid-row: 1 / 10; }
  • 48. Grid Layout As with flexbox we can use align-self and justify- self to target individual grid items. .a { grid-column: 1 / 4; grid-row: 1 / 4; align-self: stretch; } .b { grid-column: 5 / 8; grid-row: 1 / 4; align-self: end; } .c { grid-column: 1 / 4; grid-row: 5 / 10; align-self: start; } .d { grid-column: 5 / 8; grid-row: 5 / 10; align-self: center; } .e { grid-column: 9 / 12; grid-row: 1 / 10; }
  • 51. 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.”
  • 52. target ÷ context = result h1 { margin-left: 14.575%; /* 144px / 988px = 0.14575 */ width: 70.85%; /* 700px / 988px = 0.7085 */ }
  • 53. 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; }
  • 54. The flex property • flex-grow - add space • flex-shrink - remove space • flex-basis - the initial size before any growing or shrinking
  • 55. https://drafts.csswg.org/css-flexbox/#flex-components Authors are encouraged to control flexibility using the flex shorthand rather than with its longhand properties directly, as the shorthand correctly resets any unspecified components to accommodate common uses.
  • 56. Flexbox flex: 1 1 200px; flex-grow: 1 flex-shrink: 1; flex-basis: 200px; The initial width of our box is 200 pixels, however it can grow larger and shrink smaller than 200 pixels. .boxes { display: flex; justify-content: space-around; } .box { flex: 1 1 200px; min-width: 1px; }
  • 58. Flexbox flex: 1 1 200px; flex-grow: 1 flex-shrink: 1; flex-basis: 200px; If we allow the flex items to wrap we can see how flex-basis works by dragging the window smaller. .boxes { display: flex; flex-flow: row wrap; justify-content: space-around; } .box { flex: 1 1 200px; min-width: 1px; }
  • 60. Flexbox flex: 0 1 200px; flex-grow: 0 flex-shrink: 1; flex-basis: 200px; The initial width of our box is 200 pixels, it can shrink smaller than 200 pixels but may not get larger. .boxes { display: flex; justify-content: space-around; } .box { flex: 0 1 200px; min-width: 1px; }
  • 62. Flexbox flex: 1 1 200px; flex-grow: 1; flex-shrink: 1; flex-basis: 200px; .box3 has been set to flex: 0 1 200px; so cannot grow. .boxes { display: flex; justify-content: space-around; } .box { flex: 1 1 200px; min-width: 1px; } .box3 { flex: 0 1 200px; }
  • 64. Flexbox If we set box3 to
 flex-grow: 2 This box will be assigned twice of much of the available free space after we have reached the 200 pixel initial width. .boxes { display: flex; justify-content: space-around; } .box { flex: 1 1 200px; min-width: 1px; } .box3 { 2 1 200px; }
  • 67. The CSS Grid Layout fr unit
  • 68. 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; }
  • 69. 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; }
  • 70. 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; }
  • 71. http://alistapart.com/article/holygrail Three columns. One fixed-width sidebar for your navigation, another for, say, your Google Ads or your Flickr photos—and, as in a fancy truffle, a liquid center for the real substance.
  • 72. Grid Layout CSS Grid “Holy Grail” using grid-template- areas. //css .header { grid-area: header;} .footer { grid-area: footer;} .side1 { grid-area: nav;} .side2 { grid-area: ads;} .content { grid-area: content;} .wrapper { display: grid; grid-template-columns: 300px 20px 1fr 20px 300px; grid-template-rows: auto; grid-template-areas: "header header header header header" "nav ...... content ...... ads" "footer footer footer footer footer" ; } //html <div class="wrapper"> <header class="header">This is the header</header> <article class="content"></article> <div class="side1"></div> <div class="side2"></div> <footer class="footer"></div> </div>
  • 74. CSS Anthology, 2004 Q. Is it a bad thing to use effects that don’t work in some browsers?
  • 75. 2004 Rachel “Users [of IE] might see square corners instead of rounded ones […] but they’ll be able to use the site just as well as their Mozilla-wielding counterparts.”
  • 76. It’s one thing to have no rounded corners. Quite another to have no layout in an older browser.
  • 78. We need to think about feature support differently in a world of evergreen browsers.
  • 79. Evergreen browsers mean we can enhance with newer techniques. Over time our users see the site incrementally improve.
  • 80. CSS Grid Layout • Early implementation in IE10, 11 and current Edge • Implementation of most of the current spec behind a flag in Blink • Prefixed in Webkit Nightlies • Partial implementation in Firefox Nightlies • Edge have updating to current spec as ‘High Priority’ on the backlog
  • 81. Start with small UI elements • Your layout doesn’t have to be all flexbox or nothing. • Build your layout using the methods that work for your audience profile. • Finesse using more modern methods
  • 82. Grid Layout Display a dl in two columns using Grid. dt elements alway start on the left, dd on the right. @media (min-width: 550px) { .grid-dl { display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: auto; } .grid-dl dt { grid-column-start: 1; } .grid-dl dd { grid-column-start: 2; } .grid-dl dt+dd { border-top: 2px solid #ccc; } }
  • 85. Flexbox in the real world Zoe M. Gillenwater - Enhancing Responsiveness With Flexbox https://www.youtube.com/watch?v=_98SE8WUvLk
  • 86. Check your stats before making assumptions about support.
  • 88. Separate desktop and mobile statistics - your mobile users may well have far more capability.
  • 89. Use modern methods for prototyping and benefit from rapid design in the browser.
  • 90. Take an interest in early stage specifications in order to feed back to the process. Have your say.
  • 92. Thank you Slides & Resources: 
 https://rachelandrew.co.uk/presentations/modern-css-layout http://csslayout.news - sign up for my weekly CSS Layout email — @rachelandrew me@rachelandrew.co.uk — https://rachelandrew.co.uk https://grabaperch.com