A Pragmatic Look at Flexbox
Flexing your layout muscles
Stephanie Rewis
Lead Developer, Design Systems
@stefsull
The problem with layout
position: absolute;
float: left;
float: right;
clearfix
display: inline-block;
display: table;
display: flex;
Can you use flexbox?
caniuse.com
Windows for Business
Fallbacks work nicely
Almost complete guide to flexbox without flexbox
Polyfill for IE 8 & 9
Modernizr
Flexbox Containers
display
flex-direction
flex-wrap
flex-flow
justify-content
align-items
align-content
Container properties — start & end
Flex container
Flex item Flex item
flex-direction: row;
Main axis
Cross axis
Main axis start Main axis end
Cross axis start
Cross axis end
Flex container
Flex item
Flex item
Cross axis
Main axis
Cross axis start Cross axis end
Main axis start
Main axis end
flex-direction: column;
Flexbox Items
flex-grow
flex-shrink
flex-basis
flex
order
align-self
What Can We Do
with Flexbox?
Inline-flex
Code for inline-flex
.flex-container {

display: inline-flex;

}
Equal height boxes
Equal height boxes
.container {

display: flex; // row is the default

}
Equal height boxes
.container {

display: flex;

}
.item {
flex: 0 1 32%;
}
Equal height boxes
.container {

display: flex;
justify-content: space-around;

}
.item {
flex: 0 1 32%;
}
Align to the bottom
Align to the Bottom
.chart {

display: flex;

align-items: flex-end;

justify-content: space-around;

}
Flex item alignment
Flex item alignment
.container {

display: flex;

align-items: flex-start; //no more stretching to fill the space

}
Flex item alignment
.container {

display: flex;

align-items: flex-start; //no more stretching to fill the space

}

.slds-align-middle {

align-self: center; // This overrides the align-items property

}
Flex item alignment
.container {

display: flex;

align-items: flex-start; //no more stretching to fill the space

}

.slds-align-middle {

align-self: center; // This overrides the align-items property

}
Absolute centering
Absolute centering
.container {

display: flex;

align-items: center; // aligns vertically

justify-content: center; // aligns horizontally

}
Media object
Media object
.media {

display: flex;

align-items: flex-start; // start at the top instead of stretch

}
.media__figure {

flex-shrink: 0; // maintains the integrity of the attached media

}
.media__body {

flex: 1; // takes up the remaining space

min-width: 0; // Allows truncation inside flexbox due to changes in
flex basis

}
Media object
.media {

display: flex;

align-items: flex-start; // start at the top instead of stretch

}
.media__figure {

flex-shrink: 0; // maintains the integrity of the attached media

}
.media__body {

flex: 1; // takes up the remaining space

min-width: 0; // Allows truncation inside flexbox due to changes in
flex basis

}
Media object
.media--center {

align-items: center;

}
Media object
.media__figure--reverse {

margin-right: 0; // remove the right margin
margin-left: .5rem; //set the left margin to desired value

}
Media object
.media__figure--reverse {

margin-right: 0; // remove the right margin
margin-left: .5rem; //set the left margin to desired value

}
Horizontal Lists
Horizontal lists
.list--horizontal {

display: flex;

justify-content: space-between;

}
.list__item {

display: flex;

align-items: center;

justify-content: center;

}
Horizontal lists
.list--horizontal {

display: flex;

}
.list__item {
flex-grow: 1;
display: flex;
align-items: center;
justify-content: center;

}
Auto Margins
Auto margins
.col--bump-right {
margin-right: auto;
}
.col--bump-left {
margin-left: auto;
}
Auto margin - vertical
.container {

display: flex;

flex-direction: column; // stack the flex-items up

align-items: center; // align to center on column’s main axis

}
.col--bump-top { // bump classes come from our grid system

margin-top: auto;

}
Auto margin - vertical
.container {

display: flex;

flex-direction: column; // stack the flex-items up

align-items: center; // align to center on column’s main axis

}
.col--bump-top { // bump classes come from our grid system

margin-top: auto;

}
Back to absolute centering
Absolute centering
.absolute-center {
display: flex;
//justify-content: center;
//align-items: center;
}
.content {
margin: auto;
}
Grids
Grid base
Grid base
.grid {
display: flex;
}
.col {
flex: 1 1 auto;
}
Grid utilities
Flex-direction
.slds-grid--reverse {
flex-direction: row-reverse; // reverse the row
}
.slds-grid--vertical {
flex-direction: column; // create a column
}
.slds-grid--vertical-reverse {
flex-direction: column-reverse; // reverse the column
}
Flex-wrap
.slds-wrap { // This is placed on the .grid container
flex-wrap: wrap;
align-items: flex-start
}
Flex-wrap with flex-basis
.slds-col {
flex-basis: 10rem;
}
Horizontal alignment
.slds-grid—align-center {
justify-content: center;
}
.slds-grid—align-space {
justify-content: space-around;
}
.slds-grid—align-spread {
justify-content: space-between;
}
.slds-grid—align-end {
justify-content: flex-end;
}
Vertical alignment
.slds-grid—vertical-align-center {
align-items: center;
align-content: center;
}
.slds-grid--vertical-align-end {
align-items: flex-end;
align-content: flex-end
}
Vertical alignment
.slds-grid—vertical-align-center {
align-items: center;
align-content: center;
}
.slds-grid--vertical-align-end {
align-items: flex-end;
align-content: flex-end
}
Support for — 2,3,4,5,6,8
and 12 Column Units
.slds-size--x-of-y
Responsive sizing
Responsive sizing
<div class="slds-size--1-of-1 slds-small-size--1-of-2 

slds-medium-size—1-of-4">
~content~
</div>
Responsive sizing
Responsive sizing
Responsive sizing
Codepen Example
Resources
A Complete Guide to Flexbox - CSS Tricks
Known Bugs and Workarounds for Flexbox — Phil Walton
Flexbox froggy game
Anything by Zoe Gillenwater if you need to do progressive enhancement
Flexbox tester - Understand how to calculate the width of flex items
Almost complete guide to flexbox (without flexbox)
Visual Flexbox builder for Webflow
Flexbox Spec from W3C
Salesforce Lightning Design System - Grids
Smashing Magazine- the flexbox reading list
thank y u
& Greg Rewis… ;)

Flexing your layout muscles - a pragmatic look at Flexbox