KEMBAR78
Enhancing Responsiveness With Flexbox (CSS Day) | PDF
Flexbox
Zoe Mickley Gillenwater @zomigiCSS Day
June 2015
Enhancing
WITH
Responsiveness
I used to make fixed-width sites…2
…with tables3
I even wrote a
book about it
in 2008.
Then I got into CSS “liquid” layout4
Problems with CSS 2 layout
 Float containment
 Wrapping hard to control
 Difficulty making boxes equal height
 Difficulty doing vertical centering
 Difficulty mixing units of measurement
 Visual location still tied to HTML order
5
Flexbox solves a lot of those issues
 Make boxes automatically grow to fill
space or shrink to avoid overflow
 Give boxes proportional measurements
 Lay out boxes in any direction
 Align boxes on any side
 Place boxes out of order from HTML
6
Use flexbox now on UI components as
progressive enhancement.
You can use flexbox now8
“Easy for you to say.
I have to support IE 8.”
– You, maybe
“I work for Booking.com,
and we support IE 7,
and I use flexbox.”
– Me
Progressive enhancement possibilities
 Align items in new ways
 Fill up the gaps in your layout
 Reorder decorative content
 Increase responsiveness
11
Responsiveness is a continuum.
Flexbox can help make your site more
responsive.
RWD is not binary12
Flexbox and RWD
Make better use of the space
at all screen sizes
Reorder content at different
screen sizes
13
Space
Placement
Making better use of space14
Demo: horizontal navigation
Without flexbox:
.list-nav {
margin: 0;
padding: 0;
list-style: none;
text-align: center;
}
.list-nav li {
display: inline-block;
padding: 0 .5em;
text-align: center;
}
15
Demo: horizontal navigation
1. Turn <ul> into flex container:
.list-nav {
display: flex;
flex-direction: row; /* default */
...
}
2. Children <li> become flex items laid out on
single horizontal line
16
Demo: horizontal navigation
Non-flexbox
fallback version
Flexbox version
17
Making it full-width
 All links on same line
 First link flush left, last link flush right
 Equal spaces between all links
18
Trying display:table-cell
 All links on same line
 First link flush left, last link flush right
 Equal spaces between all links
19
Spacing with table-layout:fixed20
Making it full-width
.list-nav {
display: flex;
justify-content: space-between;
margin: 0;
padding: 0;
list-style: none;
}
.list-nav li {
text-align: center;
}
21
justify-content22
aligns flex items along
main axis
space-around
flex-endcenter
flex-start
(default)
space-between
(flow direction set by flex-direction)
Combine with inline-block
.list-nav {
display: flex;
justify-content: space-between;
margin: 0;
padding: 0;
list-style: none;
text-align: center; /* fallback */
}
.list-nav li {
display: inline-block; /* fallback */
padding: 0 .5em; /* fallback */
text-align: center;
}
.list-nav li:first-child { padding-left: 0; }
.list-nav li:last-child { padding-right: 0; }
23
Combine with inline-block
Non-flexbox
fallback version
Flexbox version
24
Improve the wide layout25
Wide: too stretched out
A more responsive enhancement
Wide variation: two-piece main nav
1. Add media query for wide width:
@media (min-width:860px) {
}
2. Add link to Modernizr:
<script src="js/modernizr.js"></script>
26
<html class="flexbox">
Supporting browsers:
<html class="no-flexbox">
Non-supporting browsers:
Add Modernizr as needed with flexbox
 Flexbox and fallback styles can often co-
exist, but sometimes need (or want) to
isolate them
 Modernizr can add flexbox, no-flexbox,
and flexboxlegacy classes to do this
27
Wide variation: two-piece main nav
3. Move nav bar up to overlap logo’s line:
@media (min-width:860px) {
.flexbox .list-nav {
position: relative;
top: -70px;
}
}
28
Wide variation: two-piece main nav
4. Add margins to control extra space in line:
.flexbox .link-party {
margin-left: auto;
}
.flexbox .link-home { margin-right: 15px; }
.flexbox .link-tumblr { margin-left: 15px; }
29
(margin)
A more responsive nav bar30
Content blocks’ height varies in RWD,
and flexbox can also help you make
better use of varying vertical space
This works vertically too31
Demo: full-height stacked icons
.wrapper
.icons
.content
32
Demo: full-height stacked icons
1. Turn children .icons and .content into
side-by-side, equal-height flex items
.wrapper {
display: flex;
align-items: stretch; /* default */
}
33
align-items34
aligns flex items in
cross axis
flex-start flex-end
center baseline
stretch
(default)
foo foo foo
(justify-content aligned in main axis)
Only children become flex items35
So these 2 children are the flex items
This is the flex container
These 3 grandchildren aren’t flex items (yet)
Demo: full-height stacked icons
2. Turn .icons into flex container with
vertically stacked children (the 3 icons):
.icons {
display: flex;
flex-direction: column; /* main axis */
}
36
Demo: full-height stacked icons
3. Equally space the 3 icons along the vertical
main axis:
.icons {
display: flex;
flex-direction: column;
justify-content: space-between;
}
37
Demo: full-height stacked icons38
Combine with table-cell
.wrapper {
display: table; /* fallback */
display: flex;
}
.icons {
display: table-cell; /* fallback */
vertical-align: middle; /* fallback */
display: flex;
flex-direction: column;
justify-content: space-between;
}
.content {
display: table-cell; /* fallback */
vertical-align: top; /* fallback */
flex: 1 0 0px;
}
39
Combine with float
.wrapper {
display: flex;
}
.icons {
float: left; /* fallback */
position: relative; /* fix for old WebKit bug w/ floated flex items */
width: 40px;
display: flex;
flex-direction: column;
justify-content: space-between;
}
.content {
flex: 1 0 0px;
}
.no-flexbox .content {
margin-left: 60px; /* fallback */
}
40
Fallback alignment options41
Top-aligned (float) Centered (table-cell)
Combining units of measurement across
a line can make RWD tricky.
ems + % + px + [blank] = ???42
Demo: responsive form43
Inspired by http://jobs.theguardian.com/,
which uses floats and percentage widths
But it would be nicer if…44
 The drop-down and button were sized
automatically by their content, so this
doesn’t happen:
 The fields and button all matched each
other exactly in height
Enhance form with flexbox45
1. Let the fields wrap when needed:
.jobs-form {
display: flex;
flex-wrap: wrap;
}
Enhance form with flexbox46
2. Override the % widths and use flex instead:
/* hide from non-flex browsers */
.flexbox .jobs-form_field-wrapper {
width: auto;
flex: 1 1 100%;
}
Enhance form with flexbox47
3. When wider, let fields size to their content:
@media (min-width:40em) {
.jobs-form_keywords, /* the 2 text fields */
.jobs-form_location {
flex: 1 1 auto;
}
.jobs-form_distance, /* select and button */
.jobs-form_submit {
flex: 0 0 auto;
}
}
Defining the flex property
Makes flex items change their main size
(width or height) to fit available space
48
Defining the flex property
flex-grow
how much flex
item will grow
relative to
other items if
extra space is
available
(proportion
of extra space
that it gets)
flex-shrink
how much item
will shrink
relative to others
if there is not
enough space
(proportion of
overflow that
gets shaved off)
flex-basis
the initial
starting size
before free
space is
distributed
(any standard
width/height
value, including
auto)
49
Breaking down the flex property50
@media (min-width:40em) {
.jobs-form_keywords,
.jobs-form_location {
flex: 1 1 auto;
}
.jobs-form_distance,
.jobs-form_submit {
flex: 0 0 auto;
}
}
flex-basis = auto
start field at “main
size” value (in this
case, width) or
natural content size if
main size not set
Breaking down the flex property51
@media (min-width:40em) {
.jobs-form_keywords,
.jobs-form_location {
flex: 1 1 auto;
}
.jobs-form_distance,
.jobs-form_submit {
flex: 0 0 auto;
}
}
flex-shrink = 1
it’s ok to shrink smaller
than starting width if
there’s not enough space
flex-shrink = 0
don’t shrink smaller
than starting width
Breaking down the flex property52
@media (min-width:40em) {
.jobs-form_keywords,
.jobs-form_location {
flex: 1 1 auto;
}
.jobs-form_distance,
.jobs-form_submit {
flex: 0 0 auto;
}
}
flex-grow = 1
give it 1 share of
any extra width on
its line
flex-grow = 0
don’t grow bigger
than starting width
In other words...53
@media (min-width:40em) {
.jobs-form_keywords,
.jobs-form_location {
flex: 1 1 auto;
}
.jobs-form_distance,
.jobs-form_submit {
flex: 0 0 auto;
}
}
Text fields:
You guys adjust to the space
available to fill the line.
Select and button:
You guys just stay at your
content width.
The flexbox form so far54
flex-basis: 100%
flex-basis: auto
Enhance form with flexbox55
4. Refine keywords field’s flex-basis values
to improve the layout:
@media (min-width:40em) {
.jobs-form_keywords { flex: 1 1 100%; }
.jobs-form_location { flex: 1 1 auto; }
.jobs-form_distance,
.jobs-form_submit { flex: 0 0 auto; }
}
@media (min-width:50em) {
.jobs-form_keywords { flex-basis: auto; }
}
Enhance form with flexbox56
5. Turn each field wrapper into flex container
so field inside will stretch to match height
of its line:
.flexbox .jobs-form_field-wrapper {
display: flex; /* sets align-items:stretch */
width: auto;
flex: 1 1 100%;
}
Fields misaligned without flexbox Fields match height due to align-items
Smarter sizing57
Non-flexbox
Flexbox enhanced
Content-driven breakpoints
aren’t perfect.
Automatic orientation switch59
Booking’s responsive customer service form
doesn’t use any media queries
http://www.booking.com/content/cs.html
Automatic orientation switch60
form.cs-message {
display: flex;
flex-flow: row wrap;
margin-right: -10px;
}
input.cs-message__text {
flex: 1 0 40%;
width: 43%;
float: left;
margin-right: 10px;
padding: 8px 10px;
}
1 property creates
2 responsive layouts,
both always full width
Demo: responsive article header
 No media query
 display: block
 50em media query
 display: table-cell
61
Narrow starting styles Wide starting styles
Enhance header with flexbox62
.article-header
.article-header-image
.article-header-text
Enhance header with flexbox63
1. Make photo and text block automatically sit side-
by-side when they can fit (300px + 20em):
.article-header {
display: flex;
flex-wrap: wrap;
margin-left: -20px;
}
.article-header-image {
flex: 1 1 300px;
padding: 0 0 20px 20px;
}
.article-header-text {
flex: 1 1 20em;
padding: 0 0 20px 20px;
}
Enhance header with flexbox64
2. Enhance alignment of text within the text block:
...
.article-header-text {
display: flex;
flex-wrap: wrap;
align-items: baseline;
align-content: space-between;
flex: 1 1 20em;
padding: 0 0 20px 20px;
}
.article-title {
flex: 1 1 100%;
}
.article-category {
flex: 1 1 auto;
}
Demo: responsive article header65
flex: 1 1 auto
align-content:
space-between
Improved wrapping66
With float or text-align With flex or justify-content
A real-world wrapping example
Without flexbox (IE 9) With flexbox (Chrome)
67
Flexbox with float fallback
.iw_mini_details_wrapper {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
align-items: baseline;
}
.iw_mini_review_score_wrapper {
float: left;
}
.iw_mini_price_wrapper {
float: right;
}
68
Reordering content69
Remember this?
.flexbox .list-nav {
position: relative;
top: -70px;
}
.flexbox .link-party {
margin-left: auto;
}
.flexbox .link-home { margin-right: 15px; }
.flexbox .link-tumblr { margin-left: 15px; }
70
Nav overlaps logo’s line,
so link text could overlap
logo if viewport too
narrow or text too big
Use order property to move logo
1. Divide nav bar into order groups:
.link-home, .link-builder {
order: 0; /* default, and first here */
}
.logo {
order: 1; /* second */
}
.link-party, .link-tumblr {
order: 2; /* last */
}
71
(margin)
Use order property to move logo
2. Split extra space on line to center logo:
.logo {
margin-left: auto;
}
.link-party {
margin-left: auto;
}
72
Order only works on siblings73
To move logo to middle of list, it needs to be
part of list
<div class="logo"><img src="images/logo.png"></div>
<ul class="list-nav">
<li class="logo"><img src="images/logo.png"></li>
<li class="link-home"><a>home</a></li>
<li class="link-builder"><a>s'mores builder</a></li>
<li class="link-party"><a>throw a party</a></li>
<li class="link-tumblr"><a>tumblr</a></li>
</ul>
Accessibility implications
Pro
Can keep content in
logical order in HTML
instead of structuring
HTML just to achieve a
visual layout.
Cons
If HTML order is illogical,
screen reader users still
hear it.
Focus/tab order won’t
always match expected
order, may jump around
seemingly randomly.
74
If you’re using it for progressive
enhancement, the content should make
sense in both the HTML and visual order.
Use the order property sparingly75
Reordering mobile content
In RWD, narrow-view
(mobile) stacking order
doesn’t always match
needed HTML order for
wide-view (desktop)
layout
Keep HTML order needed
for desktop and use
order property only on
mobile, since browser
support is great there
Problem Solution
76
Demo: moving a photo on mobile
Desktop: HTML order (no flexbox)Mobile: reordered
77
Use flexbox order in mobile styles
.recipe {
display: flex;
flex-direction: column;
}
.recipe figure {
order: -1; /* before all items with default
order: 0 */
}
.recipe figure img {
width: 100%;
}
Inspired by Jonathan Cutrell’s example at http://webdesign.tutsplus.com/
tutorials/tricks-with-flexbox-for-better-css-patterns--cms-19449
78
Turn off flexbox in desktop styles
@media screen and (min-width:800px) {
.recipe {
display: block; /* turn off flexbox */
}
.recipe figure {
float: right;
width: 55%;
}
}
79
Demo: moving a photo on mobile
Flexbox version Non-flexbox version
80
The Guardian: opposite approach81
Stacking order you
see when narrow
is the HTML order,
unchanged
1
2
3
4
5
6
The Guardian: opposite approach82
Reordered when
wide, but not
using order 12 3
4 56
flex-direction: row-reverse
flex-direction: row-reverse
Using flexbox today83
v.10+
2009 syntax
(display:box)
2011 syntax
(display:flexbox)
Current syntax
(display:flex)
v.10
Pick which versions you’ll use
* with -webkit- prefix
† with -ms- prefix
v.6+
*
†
*
v.3+
*
v.4.4v.11
*
v.7+
*
84
I recommend you skip the ‘09 syntax85
 It’s slower to render than current syntax*
 Doesn’t support wrapping
 Its browsers have tiny market share
 You should be using flexbox in progressive
enhancement sort of way regardless, so its
browsers will just get same fallback you
provide to non-supporting browsers
* http://updates.html5rocks.com/2013/10/Flexbox-layout-isn-t-slow
Set up your tools
 Let Autoprefixer, Sass, or LESS add the
browser variants for you:
 https://github.com/ai/autoprefixer
 https://github.com/mastastealth/sass-flex-mixin
 https://gist.github.com/cimmanon/4461470
 https://github.com/thoughtbot/bourbon/blob/mas
ter/app/assets/stylesheets/css3/_flex-box.scss
 https://github.com/annebosman/FlexboxLess
 Keep Modernizr on hand to help feed
different styles to different browsers:
http://modernizr.com/download/
86
Adding Modernizr classes with Sass87
@mixin supports-flexbox($support) {
@if $support == yes {
.flexbox & { @content }
}
@if $support == no {
.no-flexbox & { @content }
}
}
Adding Modernizr classes with Sass88
.container {
display: flex;
}
.sidebar {
float: left;
width: 300px;
}
.main-content {
margin-left: 320px;
@include supports-flexbox(yes) {
margin-left: 20px;
}
}
Adding Modernizr classes with Sass89
.container {
display: flex;
}
.sidebar {
float: left;
width: 300px;
}
.main-content {
margin-left: 320px;
}
.flexbox .main-content {
margin-left: 20px;
}
Pick your starter/fallback layout CSS
 Floats
 table-cell
 inline-block
 Absolute positioning
90
Flexbox will override: Flexbox will not override:
No single right/best answer. Use whatever
you normally would.
Flexbox is not
ALL or NOTHING
Learn more92
Download slides and get links at
www.zomigi.com/blog/rwd-flexbox
Thanks!
Zoe Mickley Gillenwater
@zomigi
design@zomigi.com
zomigi.com | stunningcss3.com | flexiblewebbook.com
Photo credits: “oh.my.goshk” by Abulic Monkey and “A Cone Undone” by patersor on Flickr.

Enhancing Responsiveness With Flexbox (CSS Day)

  • 1.
    Flexbox Zoe Mickley Gillenwater@zomigiCSS Day June 2015 Enhancing WITH Responsiveness
  • 2.
    I used tomake fixed-width sites…2
  • 3.
  • 4.
    I even wrotea book about it in 2008. Then I got into CSS “liquid” layout4
  • 5.
    Problems with CSS2 layout  Float containment  Wrapping hard to control  Difficulty making boxes equal height  Difficulty doing vertical centering  Difficulty mixing units of measurement  Visual location still tied to HTML order 5
  • 6.
    Flexbox solves alot of those issues  Make boxes automatically grow to fill space or shrink to avoid overflow  Give boxes proportional measurements  Lay out boxes in any direction  Align boxes on any side  Place boxes out of order from HTML 6
  • 8.
    Use flexbox nowon UI components as progressive enhancement. You can use flexbox now8
  • 9.
    “Easy for youto say. I have to support IE 8.” – You, maybe
  • 10.
    “I work forBooking.com, and we support IE 7, and I use flexbox.” – Me
  • 11.
    Progressive enhancement possibilities Align items in new ways  Fill up the gaps in your layout  Reorder decorative content  Increase responsiveness 11
  • 12.
    Responsiveness is acontinuum. Flexbox can help make your site more responsive. RWD is not binary12
  • 13.
    Flexbox and RWD Makebetter use of the space at all screen sizes Reorder content at different screen sizes 13 Space Placement
  • 14.
  • 15.
    Demo: horizontal navigation Withoutflexbox: .list-nav { margin: 0; padding: 0; list-style: none; text-align: center; } .list-nav li { display: inline-block; padding: 0 .5em; text-align: center; } 15
  • 16.
    Demo: horizontal navigation 1.Turn <ul> into flex container: .list-nav { display: flex; flex-direction: row; /* default */ ... } 2. Children <li> become flex items laid out on single horizontal line 16
  • 17.
  • 18.
    Making it full-width All links on same line  First link flush left, last link flush right  Equal spaces between all links 18
  • 19.
    Trying display:table-cell  Alllinks on same line  First link flush left, last link flush right  Equal spaces between all links 19
  • 20.
  • 21.
    Making it full-width .list-nav{ display: flex; justify-content: space-between; margin: 0; padding: 0; list-style: none; } .list-nav li { text-align: center; } 21
  • 22.
    justify-content22 aligns flex itemsalong main axis space-around flex-endcenter flex-start (default) space-between (flow direction set by flex-direction)
  • 23.
    Combine with inline-block .list-nav{ display: flex; justify-content: space-between; margin: 0; padding: 0; list-style: none; text-align: center; /* fallback */ } .list-nav li { display: inline-block; /* fallback */ padding: 0 .5em; /* fallback */ text-align: center; } .list-nav li:first-child { padding-left: 0; } .list-nav li:last-child { padding-right: 0; } 23
  • 24.
  • 25.
    Improve the widelayout25 Wide: too stretched out A more responsive enhancement
  • 26.
    Wide variation: two-piecemain nav 1. Add media query for wide width: @media (min-width:860px) { } 2. Add link to Modernizr: <script src="js/modernizr.js"></script> 26 <html class="flexbox"> Supporting browsers: <html class="no-flexbox"> Non-supporting browsers:
  • 27.
    Add Modernizr asneeded with flexbox  Flexbox and fallback styles can often co- exist, but sometimes need (or want) to isolate them  Modernizr can add flexbox, no-flexbox, and flexboxlegacy classes to do this 27
  • 28.
    Wide variation: two-piecemain nav 3. Move nav bar up to overlap logo’s line: @media (min-width:860px) { .flexbox .list-nav { position: relative; top: -70px; } } 28
  • 29.
    Wide variation: two-piecemain nav 4. Add margins to control extra space in line: .flexbox .link-party { margin-left: auto; } .flexbox .link-home { margin-right: 15px; } .flexbox .link-tumblr { margin-left: 15px; } 29 (margin)
  • 30.
  • 31.
    Content blocks’ heightvaries in RWD, and flexbox can also help you make better use of varying vertical space This works vertically too31
  • 32.
    Demo: full-height stackedicons .wrapper .icons .content 32
  • 33.
    Demo: full-height stackedicons 1. Turn children .icons and .content into side-by-side, equal-height flex items .wrapper { display: flex; align-items: stretch; /* default */ } 33
  • 34.
    align-items34 aligns flex itemsin cross axis flex-start flex-end center baseline stretch (default) foo foo foo (justify-content aligned in main axis)
  • 35.
    Only children becomeflex items35 So these 2 children are the flex items This is the flex container These 3 grandchildren aren’t flex items (yet)
  • 36.
    Demo: full-height stackedicons 2. Turn .icons into flex container with vertically stacked children (the 3 icons): .icons { display: flex; flex-direction: column; /* main axis */ } 36
  • 37.
    Demo: full-height stackedicons 3. Equally space the 3 icons along the vertical main axis: .icons { display: flex; flex-direction: column; justify-content: space-between; } 37
  • 38.
  • 39.
    Combine with table-cell .wrapper{ display: table; /* fallback */ display: flex; } .icons { display: table-cell; /* fallback */ vertical-align: middle; /* fallback */ display: flex; flex-direction: column; justify-content: space-between; } .content { display: table-cell; /* fallback */ vertical-align: top; /* fallback */ flex: 1 0 0px; } 39
  • 40.
    Combine with float .wrapper{ display: flex; } .icons { float: left; /* fallback */ position: relative; /* fix for old WebKit bug w/ floated flex items */ width: 40px; display: flex; flex-direction: column; justify-content: space-between; } .content { flex: 1 0 0px; } .no-flexbox .content { margin-left: 60px; /* fallback */ } 40
  • 41.
    Fallback alignment options41 Top-aligned(float) Centered (table-cell)
  • 42.
    Combining units ofmeasurement across a line can make RWD tricky. ems + % + px + [blank] = ???42
  • 43.
    Demo: responsive form43 Inspiredby http://jobs.theguardian.com/, which uses floats and percentage widths
  • 44.
    But it wouldbe nicer if…44  The drop-down and button were sized automatically by their content, so this doesn’t happen:  The fields and button all matched each other exactly in height
  • 45.
    Enhance form withflexbox45 1. Let the fields wrap when needed: .jobs-form { display: flex; flex-wrap: wrap; }
  • 46.
    Enhance form withflexbox46 2. Override the % widths and use flex instead: /* hide from non-flex browsers */ .flexbox .jobs-form_field-wrapper { width: auto; flex: 1 1 100%; }
  • 47.
    Enhance form withflexbox47 3. When wider, let fields size to their content: @media (min-width:40em) { .jobs-form_keywords, /* the 2 text fields */ .jobs-form_location { flex: 1 1 auto; } .jobs-form_distance, /* select and button */ .jobs-form_submit { flex: 0 0 auto; } }
  • 48.
    Defining the flexproperty Makes flex items change their main size (width or height) to fit available space 48
  • 49.
    Defining the flexproperty flex-grow how much flex item will grow relative to other items if extra space is available (proportion of extra space that it gets) flex-shrink how much item will shrink relative to others if there is not enough space (proportion of overflow that gets shaved off) flex-basis the initial starting size before free space is distributed (any standard width/height value, including auto) 49
  • 50.
    Breaking down theflex property50 @media (min-width:40em) { .jobs-form_keywords, .jobs-form_location { flex: 1 1 auto; } .jobs-form_distance, .jobs-form_submit { flex: 0 0 auto; } } flex-basis = auto start field at “main size” value (in this case, width) or natural content size if main size not set
  • 51.
    Breaking down theflex property51 @media (min-width:40em) { .jobs-form_keywords, .jobs-form_location { flex: 1 1 auto; } .jobs-form_distance, .jobs-form_submit { flex: 0 0 auto; } } flex-shrink = 1 it’s ok to shrink smaller than starting width if there’s not enough space flex-shrink = 0 don’t shrink smaller than starting width
  • 52.
    Breaking down theflex property52 @media (min-width:40em) { .jobs-form_keywords, .jobs-form_location { flex: 1 1 auto; } .jobs-form_distance, .jobs-form_submit { flex: 0 0 auto; } } flex-grow = 1 give it 1 share of any extra width on its line flex-grow = 0 don’t grow bigger than starting width
  • 53.
    In other words...53 @media(min-width:40em) { .jobs-form_keywords, .jobs-form_location { flex: 1 1 auto; } .jobs-form_distance, .jobs-form_submit { flex: 0 0 auto; } } Text fields: You guys adjust to the space available to fill the line. Select and button: You guys just stay at your content width.
  • 54.
    The flexbox formso far54 flex-basis: 100% flex-basis: auto
  • 55.
    Enhance form withflexbox55 4. Refine keywords field’s flex-basis values to improve the layout: @media (min-width:40em) { .jobs-form_keywords { flex: 1 1 100%; } .jobs-form_location { flex: 1 1 auto; } .jobs-form_distance, .jobs-form_submit { flex: 0 0 auto; } } @media (min-width:50em) { .jobs-form_keywords { flex-basis: auto; } }
  • 56.
    Enhance form withflexbox56 5. Turn each field wrapper into flex container so field inside will stretch to match height of its line: .flexbox .jobs-form_field-wrapper { display: flex; /* sets align-items:stretch */ width: auto; flex: 1 1 100%; } Fields misaligned without flexbox Fields match height due to align-items
  • 57.
  • 58.
  • 59.
    Automatic orientation switch59 Booking’sresponsive customer service form doesn’t use any media queries http://www.booking.com/content/cs.html
  • 60.
    Automatic orientation switch60 form.cs-message{ display: flex; flex-flow: row wrap; margin-right: -10px; } input.cs-message__text { flex: 1 0 40%; width: 43%; float: left; margin-right: 10px; padding: 8px 10px; } 1 property creates 2 responsive layouts, both always full width
  • 61.
    Demo: responsive articleheader  No media query  display: block  50em media query  display: table-cell 61 Narrow starting styles Wide starting styles
  • 62.
    Enhance header withflexbox62 .article-header .article-header-image .article-header-text
  • 63.
    Enhance header withflexbox63 1. Make photo and text block automatically sit side- by-side when they can fit (300px + 20em): .article-header { display: flex; flex-wrap: wrap; margin-left: -20px; } .article-header-image { flex: 1 1 300px; padding: 0 0 20px 20px; } .article-header-text { flex: 1 1 20em; padding: 0 0 20px 20px; }
  • 64.
    Enhance header withflexbox64 2. Enhance alignment of text within the text block: ... .article-header-text { display: flex; flex-wrap: wrap; align-items: baseline; align-content: space-between; flex: 1 1 20em; padding: 0 0 20px 20px; } .article-title { flex: 1 1 100%; } .article-category { flex: 1 1 auto; }
  • 65.
    Demo: responsive articleheader65 flex: 1 1 auto align-content: space-between
  • 66.
    Improved wrapping66 With floator text-align With flex or justify-content
  • 67.
    A real-world wrappingexample Without flexbox (IE 9) With flexbox (Chrome) 67
  • 68.
    Flexbox with floatfallback .iw_mini_details_wrapper { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: baseline; } .iw_mini_review_score_wrapper { float: left; } .iw_mini_price_wrapper { float: right; } 68
  • 69.
  • 70.
    Remember this? .flexbox .list-nav{ position: relative; top: -70px; } .flexbox .link-party { margin-left: auto; } .flexbox .link-home { margin-right: 15px; } .flexbox .link-tumblr { margin-left: 15px; } 70 Nav overlaps logo’s line, so link text could overlap logo if viewport too narrow or text too big
  • 71.
    Use order propertyto move logo 1. Divide nav bar into order groups: .link-home, .link-builder { order: 0; /* default, and first here */ } .logo { order: 1; /* second */ } .link-party, .link-tumblr { order: 2; /* last */ } 71 (margin)
  • 72.
    Use order propertyto move logo 2. Split extra space on line to center logo: .logo { margin-left: auto; } .link-party { margin-left: auto; } 72
  • 73.
    Order only workson siblings73 To move logo to middle of list, it needs to be part of list <div class="logo"><img src="images/logo.png"></div> <ul class="list-nav"> <li class="logo"><img src="images/logo.png"></li> <li class="link-home"><a>home</a></li> <li class="link-builder"><a>s'mores builder</a></li> <li class="link-party"><a>throw a party</a></li> <li class="link-tumblr"><a>tumblr</a></li> </ul>
  • 74.
    Accessibility implications Pro Can keepcontent in logical order in HTML instead of structuring HTML just to achieve a visual layout. Cons If HTML order is illogical, screen reader users still hear it. Focus/tab order won’t always match expected order, may jump around seemingly randomly. 74
  • 75.
    If you’re usingit for progressive enhancement, the content should make sense in both the HTML and visual order. Use the order property sparingly75
  • 76.
    Reordering mobile content InRWD, narrow-view (mobile) stacking order doesn’t always match needed HTML order for wide-view (desktop) layout Keep HTML order needed for desktop and use order property only on mobile, since browser support is great there Problem Solution 76
  • 77.
    Demo: moving aphoto on mobile Desktop: HTML order (no flexbox)Mobile: reordered 77
  • 78.
    Use flexbox orderin mobile styles .recipe { display: flex; flex-direction: column; } .recipe figure { order: -1; /* before all items with default order: 0 */ } .recipe figure img { width: 100%; } Inspired by Jonathan Cutrell’s example at http://webdesign.tutsplus.com/ tutorials/tricks-with-flexbox-for-better-css-patterns--cms-19449 78
  • 79.
    Turn off flexboxin desktop styles @media screen and (min-width:800px) { .recipe { display: block; /* turn off flexbox */ } .recipe figure { float: right; width: 55%; } } 79
  • 80.
    Demo: moving aphoto on mobile Flexbox version Non-flexbox version 80
  • 81.
    The Guardian: oppositeapproach81 Stacking order you see when narrow is the HTML order, unchanged 1 2 3 4 5 6
  • 82.
    The Guardian: oppositeapproach82 Reordered when wide, but not using order 12 3 4 56 flex-direction: row-reverse flex-direction: row-reverse
  • 83.
  • 84.
    v.10+ 2009 syntax (display:box) 2011 syntax (display:flexbox) Currentsyntax (display:flex) v.10 Pick which versions you’ll use * with -webkit- prefix † with -ms- prefix v.6+ * † * v.3+ * v.4.4v.11 * v.7+ * 84
  • 85.
    I recommend youskip the ‘09 syntax85  It’s slower to render than current syntax*  Doesn’t support wrapping  Its browsers have tiny market share  You should be using flexbox in progressive enhancement sort of way regardless, so its browsers will just get same fallback you provide to non-supporting browsers * http://updates.html5rocks.com/2013/10/Flexbox-layout-isn-t-slow
  • 86.
    Set up yourtools  Let Autoprefixer, Sass, or LESS add the browser variants for you:  https://github.com/ai/autoprefixer  https://github.com/mastastealth/sass-flex-mixin  https://gist.github.com/cimmanon/4461470  https://github.com/thoughtbot/bourbon/blob/mas ter/app/assets/stylesheets/css3/_flex-box.scss  https://github.com/annebosman/FlexboxLess  Keep Modernizr on hand to help feed different styles to different browsers: http://modernizr.com/download/ 86
  • 87.
    Adding Modernizr classeswith Sass87 @mixin supports-flexbox($support) { @if $support == yes { .flexbox & { @content } } @if $support == no { .no-flexbox & { @content } } }
  • 88.
    Adding Modernizr classeswith Sass88 .container { display: flex; } .sidebar { float: left; width: 300px; } .main-content { margin-left: 320px; @include supports-flexbox(yes) { margin-left: 20px; } }
  • 89.
    Adding Modernizr classeswith Sass89 .container { display: flex; } .sidebar { float: left; width: 300px; } .main-content { margin-left: 320px; } .flexbox .main-content { margin-left: 20px; }
  • 90.
    Pick your starter/fallbacklayout CSS  Floats  table-cell  inline-block  Absolute positioning 90 Flexbox will override: Flexbox will not override: No single right/best answer. Use whatever you normally would.
  • 91.
  • 92.
    Learn more92 Download slidesand get links at www.zomigi.com/blog/rwd-flexbox Thanks! Zoe Mickley Gillenwater @zomigi design@zomigi.com zomigi.com | stunningcss3.com | flexiblewebbook.com Photo credits: “oh.my.goshk” by Abulic Monkey and “A Cone Undone” by patersor on Flickr.