CodeSchool CrackingTheCaseWithFlexbox
CodeSchool CrackingTheCaseWithFlexbox
Foreshadowing Flexbox
A Display Type Created for Common Patterns
CSS Flexible Box Layout Module Level 1
Flexbox is a collection of CSS properties used to align content and distribute space.
11 new properties
http://go.codeschool.com/exbox-spec
http://go.codeschool.com/caniuse-exbox
UI Patterns in Flexbox
Equal heights
Media objects
Sticky footers
Column layouts
Flexbox Is a New Display Type
Flexbox adds ex containers, ex items, and ex lines in addition to new properties.
Block layout
Learn a
Table layout bout ot
and co h er layo
re HTM uts
Inline layout these c L and CS
ourses. S in
Positioning layout
Flex layout
Flex containers
Flex items
Flex lines
Default Behaviors of Elements
display: inline
display: block
display: block
Overriding Default Behaviors
Collapsed
Flex Containers Control Layout of Child Items
Flex container
Aligned horizontally
display: ex Creates a Block-level Container
Container adjusts to
width of the content
Flex Items Are Distributed Along Flex Lines
Flex items are direct children of a ex container and can be HTML elements or text.
Three ex items
Grouping Elements Into Flex Items
Grandchild elements do not become ex items.
Two ex items
Two ex items
Level 1 Section 2
Foreshadowing Flexbox
Wily Wrapping
Flexbox Wrapping and Direction Examples
index.html HTML
<article class="clue">
<img height="150" height="150" ...>
<div>
<h3>...</h3>
<p>...</p>
<h3>...</h3>
<p>...</p>
</div>
</article>
320px wide
Solution: ex-wrap
This propertys default value is nowrap and it accepts: nowrap | wrap | wrap-reverse.
320px wide
Put It in Reverse?
The rst of the exbox properties that allows for rendering items in reverse.
index.html HTML
<main>
<img ...>
<h1>...</h1>
<p>...</p>
<a>...</a>
</main>
main {
display: flex;
flex-wrap: wrap; }
Solution: ex-direction
The default value is row and it accepts: row | row-reverse | column | column-reverse.
site.css CSS
main {
display: flex;
flex-direction: column; }
Page Layout With ex-direction
Main elements for layout are ex items with the column direction.
index.html HTML
header
<body>
<header>...</header>
<main>...</main>
<footer>...</footer>
</body>
main
site.css CSS
body { footer
...
display: flex;
flex-direction: column; }
Mixing in Some Non-exbox CSS
Setting the page elements to 100% height uses all available space.
index.html HTML
<body>
<header>...</header>
<main>...</main>
<footer>...</footer>
</body>
site.css CSS
Positioning elements
vertically
Positioning elements
horizontally
Flex Items Are Distributed Along Axes
The main axis is determined by the flex-direction.
start end
justify-content
Used to distribute space on the main axis, the default value is flex-start and it accepts:
flex-start | flex-end | center | space-between | space-around.
start
site.css CSS
end
site.css CSS
.nav-link
Justifying Content in a Column
Main elements for layout are ex items with the column direction.
start
index.html HTML
<main>
...
</main>
center of column
site.css CSS
main {
display: flex;
flex-direction: column;
justify-content: center; }
end
Level 2 Section 2
order: 0
Changing the Order of a Flex Item
Using a negative number displays an item rst, based on the flex-direction.
index.html HTML
<main>
<img ...>
<h1>...</h1>
<p class="slogan">...</p> order: 0
<a>...</a>
</main>
order: 0
... order: 1
.slogan {
order: 1;
margin-top: 10px; }
Level 3
Aligning Alibis
Distributing Space Between Items
Distributing Space on the Cross Axis
Before Before After
After
Flex Items Are Aligned Along a Cross Axis
The cross axis is perpendicular to the main axis.
start
Stretch lls
center
available space
end
Start and End
start
site.css CSS
site.css CSS
site.css CSS
site.css CSS
index.html HTML
<main>
...
</main>
site.css CSS
...
main {
align-items: center;
... }
Level 4 Section 1
index.html HTML
<div ...>
<label ...>...</label>
<input class="add-input">
<button ...>...</button>
</div>
...
.add-input {
align-items: center;
display: flex;
min-width: 0; }
Fills all
available
space
Using ex-grow for Multiple Items
Assigning a value of 1 to multiple properties will have them all try to ll as much space as
possible.
index.html HTML
<article ...>
<div class="clue-img">...</div>
<div class="clue-info">...</div>
</article>
site.css CSS
...
.clue-img {
flex-shrink: 0; }
Consistent widths
ex-basis
Used to specify the initial size of a ex item. It defaults to auto and currently supports CSS
units: %, px, em, rem, etc.
justify-content: space-between
index.html HTML
<article class="clue">
<div class="clue-img">...</div>
<div class="clue-info">...</div>
</article>
site.css CSS
...
.clue{
flex-basis: 47.5%; } 140px
.clue-img { 47.5%
flex-basis: 140px; }
Combining ex-basis and ex-shrink
With one item set to a pixel-based value, the other will adjust to ll the remaining space.
Using ex-basis and Absolute Units
Items with a flex-basis that is an absolute unit will not grow beyond the value by default.
site.css CSS
div {
flex-basis: 250px; }
http://go.codeschool.com/ex-basis
Level 5 Section 1
Property Plotting
Aligning Content in Edge Cases
Our Layouts Could Use Some Polish
body {
...
align-items: stretch; }
main { Stretch in the
align-self: center;
... } cross axis
Controlling Page Layout With ex-wrap
index.html HTML
<body ...>
<header>...<header>
<main>...</main>
<footer>...<footer> Each item
</body>
defaults to
stretch
site.css CSS
body {
...
flex-wrap: wrap; }
header, footer {
...
flex-basis: 100%; }
align-items Doesnt Control Wrapped Items
index.html HTML
<body ...>
<header>...<header>
<main>...</main>
<footer>...<footer>
</body>
No change?!
site.css CSS
body {
...
align-items: space-between;
flex-wrap: wrap; }
header, footer {
...
flex-basis: 100%; }
align-content
Used to align wrapped ex items. The default value is stretch and it accepts: stretch |
flex-start | flex-end | center | space-between | space-around.
index.html HTML
<body ...>
...</body>
site.css CSS
Space between
body { CSS
...
align-content: space-between;
flex-wrap: wrap; }
header, footer {
...
flex-basis: 100%; }
Level 5 Section 2
Property Plotting
Grouping Multiple Properties Into a Single Property
ex
Shorthand property used to set values for flex-grow, flex-shrink, and flex-basis. The
default is: 0 1 auto.
.flex-item { .flex {
flex-grow: 0; flex: 0 1 auto; }
flex-shrink: 1;
flex-basis: auto; }
*If you have to support older IE, you may want to avoid shorthand.
More, Shorter Examples
Single number assigns the grow value
.flex-item { .flex {
flex-grow: 1; flex: 1; }
flex-shrink: 1; /* Default */
flex-basis: auto; } /* Default */
.flex-item { .flex {
flex-grow: 1; flex: 1 0; }
flex-shrink: 0;
flex-basis: auto; } /* Default */
More, Short Examples
Single number and percentage assigns the grow and basis values
.flex-item { .flex {
flex-grow: 1; flex: 1 47.5%; }
flex-shrink: 1; /* Default */
flex-basis: 47.5%; }
.flex-item { .flex {
flex-grow: 0; /* Default */ flex: none; }
flex-shrink: 0;
flex-basis: auto; } /* Default */
ex-ow
Shorthand property used to set values for flex-direction and flex-wrap. The default is:
row nowrap.
.flex-item { .flex {
flex-direction: row; /* Default */ flex-flow: row nowrap; }
flex-wrap: nowrap; } /* Default */
direction wrap
ex-ow Examples
Single direction value sets the direction and defaults to wrap
.flex-item { .flex {
flex-direction: column; flex-flow: column; }
flex-wrap: nowrap; } /* Default */
.flex-item { .flex {
flex-direction: row; /* Default */ flex-flow: wrap; }
flex-wrap: wrap; }