Flexbox

A new CSS box model

 

Johan Wallin

2013-11-20

display:flex;

Example

I am pushing the container vertically, and the other two items next to me will get the same height even though they don't have as much content
hello
yo
.example-container {
    display: flex;
}

.example-item {
    flex: 1;
}

Example, again

hello
yo
I'm wide.
.example-container {
    display: flex;
}

.example-item {
    flex: 1;
}

.example-item-wide {
    flex:none;
    width:50%;
}

Direction

See example
.example-container {
    display: flex;
    flex-direction: column;
}

Wrap

1
2
3
4
5
6
7
8
9
10
.example-container {
    display: flex;
    flex-wrap: wrap;
}

.example-item {
    flex: 1 0 100px;
}
See example

What's in this for me?

Responsive design

(obviously)

Grids

1
2
3
4
5
6
7
8
9
10

Sticky footer

See example
body {
    display: flex;
    flex-direction: column;
}

main {
    flex:1;
}

footer {
}

Vertical centering

See example

main {
    display: flex;
    align-items: center;
    justify-content: center;
}

Holy Grail

Example

<body class="holy-grail">
    <header>Header</header>
    <div class="content">
        <main role="main">Main</main>
        <nav>Navigation</nav>
        <aside>Sidebar</aside>
    </div>
    <footer>Footer</footer>
</body>
.holy-grail {
    display: flex;
    flex-direction: column;
}
.content {
    display: flex;
    flex: 1;
} 
aside, nav { flex: 0 0 10em; }
nav { order: -1; }

(background information)

Browser support

display:flex;

  • 12+
  • 11+

Browser support

display: -webkit-box;
display: -webkit-flex;
display: -moz-box;
display: -ms-flexbox;
display: flex;

  • 12+
  • 10+

Vendor-specific attributes

display:flex
display: -webkit-box;
display: -webkit-flex;     
display: -moz-box;    
display: -ms-flexbox; 
display: flex;
flex:1
-webkit-box-flex: 1;
-moz-box-flex: 1;
-webkit-flex: 1;
-ms-flex: 1;
flex: 1;
flex-wrap: wrap
-ms-flex-wrap: wrap;
-webkit-flex-wrap: wrap;
flex-wrap: wrap;
justify-content: center;
-ms-flex-pack: center;
-webkit-justify-content: center;
justify-content: center;
flex-direction: column
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-webkit-flex-direction: column;
-moz-box-orient: vertical;
-moz-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;

Can I use this today?

Yes

Resources

https://github.com/jwallin/flexbox-slides

http://www.w3.org/TR/css3-flexbox/

http://css-tricks.com/using-flexbox/

http://philipwalton.github.io/solved-by-flexbox/

Thank you.