name: cover # Dr. Flexbox or
how I learned to stop worrying
and vertically align the box. Johannes Ewald
Peerigon GmbH
[@jhnnns](https://twitter.com/jhnnns)
--- layout: true class: theme-whiskey, slides-centered .slides-header[ Flexbox ] --- class: slides-chapter count: false background-image: url(./assets/strangelove-1.jpg) ## Dr. Flexbox --- What is flexbox? > Method of positioning elements in horizontal or vertical stacks. --- .slides-content[ For over 10 years, CSS has only provided four types of layout:
] --- .slides-content[ Now, there's a new kid on the block: .slides-columns[
] ] --- .slides-columns[ .col[
Container
``` display: block; ``` ]
1
2
3
] --- .slides-columns[ .col[
Flex container
``` display: flex; ``` ]
1
2
3
] --- .slides-columns[ .col[
Flex container
``` display: flex; flex-direction: row; /* default */ ``` ]
1
2
3
] --- .slides-columns[ .col[
Flex container
``` display: flex; flex-direction: column; ``` ]
1
2
3
] --- .slides-columns[ .col[
Flex container
``` display: flex; flex-direction: row-reverse; ``` ]
1
2
3
] --- .slides-columns[ .col[
Flex container
``` display: flex; flex-direction: column-reverse; ``` ]
1
2
3
] --- ### `display: flex` .slides-content[ Aligns all **flex items** along the so-called **main axis**. ] --- ### `flex-direction` .slides-content[ Determines the **direction** the **main axis** is pointing to. ] ---
`flex-direction: row` ---
`flex-direction: column` ---
`flex-direction: row-reverse` ---
`flex-direction: column-reverse` --- .slides-columns[ .col[
Flex container
``` display: flex; ``` ]
1
2
3
] --- .slides-columns[ .col[
Flex container
``` display: flex; justify-content: flex-start; /* default */ ``` ]
1
2
3
] --- .slides-columns[ .col[
Flex container
``` display: flex; justify-content: flex-end; ``` ]
1
2
3
] --- .slides-columns[ .col[
Flex container
``` display: flex; justify-content: center; ``` ]
1
2
3
] --- .slides-columns[ .col[
Flex container
``` display: flex; justify-content: space-between; ``` ]
1
2
3
] --- .slides-columns[ .col[
Flex container
``` display: flex; justify-content: space-around; ``` ]
1
2
3
] --- ### `justify-content` .slides-content[ Determines how **flex items** on the **main axis** are distributed. ] --- .slides-columns[ .col[
Flex container
``` display: flex; ``` ]
1
2
3
] --- .slides-columns[ .col[
Flex container
``` display: flex; align-items: stretch; /* default */ ``` ]
1
2
3
] --- .slides-columns[ .col[
Flex container
``` display: flex; align-items: flex-start; ``` ]
1
2
3
] --- .slides-columns[ .col[
Flex container
``` display: flex; align-items: flex-end; ``` ]
1
2
3
] --- .slides-columns[ .col[
Flex container
``` display: flex; align-items: center; ``` ]
1
2
3
] --- class: slides-chapter background-image: url(./assets/strangelove-2.png) ##
Wait...
--
Did we just center a box vertically?
--- class: slides-chapter background-image: url(./assets/strangelove-3.jpg) --- .slides-columns[ .col[
Flex container
``` display: flex; align-items: baseline; ``` ]
1
2
3
] --- .slides-columns[ .col[
Flex container
``` display: flex; align-items: baseline; ``` `baseline` aligns all flex items
on the biggest baseline among all items. ]
1
2
3
] --- ### `align-items` .slides-content[ Aligns all **flex items** along the so-called **cross axis**. ] --- .slides-columns[ .col[
Flex container
``` display: flex; ``` ]
1
2
3
4
5
6
] --- .slides-columns[ .col[
Flex container
``` display: flex; flex-wrap: nowrap; /* default */ ``` ]
1
2
3
4
5
6
] --- .slides-columns[ .col[
Flex container
``` display: flex; flex-wrap: wrap; ```
Not supported in older implementations.
]
1
2
3
4
5
6
] --- .slides-columns[ .col[
Flex container
``` display: flex; flex-wrap: wrap-reverse; ```
Not supported in older implementations.
]
1
2
3
4
5
6
] --- ### `flex-wrap` .slides-content[ Controls how items are wrapped along the **main axis**. ] --- .slides-columns[ .col[
Flex container
``` display: flex; flex-wrap: wrap; ```
Not supported in older implementations.
]
1
2
3
4
5
6
] --- .slides-columns[ .col[
Flex container
``` display: flex; flex-wrap: wrap; align-content: stretch; /* default */ ```
Not supported in older implementations.
]
1
2
3
4
5
6
] --- .slides-columns[ .col[
Flex container
``` display: flex; flex-wrap: wrap; align-content: flex-start; ```
Not supported in older implementations.
]
1
2
3
4
5
6
] --- .slides-columns[ .col[
Flex container
``` display: flex; flex-wrap: wrap; align-content: flex-end; ```
Not supported in older implementations.
]
1
2
3
4
5
6
] --- .slides-columns[ .col[
Flex container
``` display: flex; flex-wrap: wrap; align-content: center; ```
Not supported in older implementations.
]
1
2
3
4
5
6
] --- .slides-columns[ .col[
Flex container
``` display: flex; flex-wrap: wrap; align-content: space-between; ```
Not supported in older implementations.
]
1
2
3
4
5
6
] --- .slides-columns[ .col[
Flex container
``` display: flex; flex-wrap: wrap; align-content: space-around; ```
Not supported in older implementations.
]
1
2
3
4
5
6
] --- ### `align-content` .slides-content[ Controls how lines are distributed along the **cross axis**. ] --- class: slides-chapter background-image: url(./assets/strangelove-4.jpg) ##
Let's talk about flex items...
--- .slides-columns[ .col[
Flex item 1
``` ``` ]
1
2
3
] --- .slides-columns[ .col[
Flex item 1
``` flex-grow: 0; /* default */ ``` ]
1
2
3
] --- .slides-columns[ .col[
Flex item 1
``` flex-grow: 1; ``` ]
1
2
3
] --- .slides-columns[ .col[
Flex item 1
``` flex-grow: 1; ```
Flex item 2
``` flex-grow: 1; ``` ]
1
2
3
] --- .slides-columns[ .col[
Flex item 1
``` flex-grow: 1; ```
Flex item 2
``` flex-grow: 1; ```
Flex item 3
``` flex-grow: 1; ``` ]
1
2
3
] --- .slides-columns[ .col[
Flex item 1
``` flex-grow: 2; ```
Flex item 2
``` flex-grow: 1; ```
Flex item 3
``` flex-grow: 1; ``` ]
1
2
3
] --- .slides-columns[ .col[
Flex item 1
``` flex-grow: 10; ```
Flex item 2
``` flex-grow: 10; ```
Flex item 3
``` flex-grow: 10; ``` ]
1
2
3
] --- ### `flex-grow` .slides-content[ Specifies the **flex item's** ability to grow along the **main axis** when space is available. ] --- .slides-columns[ .col[
Flex item 1
``` ``` ]
1
2
3
] --- .slides-columns[ .col[
Flex item 1
``` flex-basis: auto; /* default */ ``` ]
1
2
3
] --- .slides-columns[ .col[
Flex item 1
``` flex-basis: 10rem; ``` ]
1
2
3
] --- .slides-columns[ .col[
Flex item 1
``` flex-basis: 10rem; flex-grow: 1; ```
Flex item 2
``` flow-grow: 1 ``` ]
1
2
3
] --- ### `flex-basis` .slides-content[ Specifies the **flex item's** initial size along the **main axis** before space is distributed. ] --- What's the difference between
`width`/`height` and `flex-basis`? --- .slides-columns[ .col[ `flex-basis` - Flex direction agnostic - Is only applied inside a
flex container ]
.col[ `width` / `height` - Generic property of
the element's dimension ] ] --- Say hello to... ### Flexman
--- .slides-text-centered[
Available space ] --- .slides-columns[ .slides-text-centered[
`flex-grow: 1` ] .slides-text-centered[
Available space ]
] --- .slides-columns[ .slides-text-centered[
`flex-grow: 1` ] .slides-text-centered[
Available space ] .slides-text-centered[
`flex-grow: 3` ] ] --- .slides-columns[ .slides-text-centered[
`flex-grow: 1`
`flex-basis: 30%` ] .slides-text-centered[
Available space ] .slides-text-centered[
`flex-grow: 3` ] ] --- .slides-columns[ .slides-text-centered[
`flex-basis: 60%` ] .slides-text-centered[
] .slides-text-centered[
`flex-basis: 60%` ] ] --- .slides-columns[ .slides-text-centered[
`flex-basis: 60%`
`flex-shrink: 3` ] .slides-text-centered[
] .slides-text-centered[
`flex-basis: 60%` ] ] --- .slides-columns[ .slides-text-centered[
`flex-basis: 60%`
`flex-shrink: 3` ] .slides-text-centered[
] .slides-text-centered[
`flex-basis: 60%`
`flex-shrink: 1` ] ] --- ### `flex-shrink` .slides-content[ Specifies the **flex item's** ability to shrink along the **main axis**
when **negative space** needs to be distributed. ] --- .slides-columns[ .col[
Flex item 1
``` order: 2; ```
Flex item 2
``` order: 3; ```
Flex item 3
``` order: 1; ``` ]
1
2
3
] --- ### `order` .slides-content[ Allows to override the **flex item's** natural position inside the **flex container**. ] --- .slides-columns[ .col[
Flex item 2
``` align-self: flex-end; ```
Flex item 3
``` align-self: flex-start; ``` ]
1
2
3
] --- ### `align-self` .slides-content[ Allows to override the **flex item's** alignment along the **cross axis**. ] --- layout: true class: theme-whiskey, slides-centered .slides-header[ Gotchas ] --- class: slides-chapter count: false background-image: url(./assets/strangelove-6.jpg) ## Gotchas --- ### Text inside flex containers .slides-content[ - Text that is directly contained inside a flex container
is automatically wrapped in an anonymous flex item. - However, an anonymous flex item that contains only white space
is not rendered, as if it were designated `display: none`.
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout/Using_CSS_flexible_boxes
] --- .slides-content[
1
This is some text
2
And here again
3
] --- ### Pseudo elements are flex items --- .slides-content[ .slides-columns[ .col[
Flex container
``` flex-direction: row-reverse; justify-content: space-around; align-items: center; ```
::before
``` content: "before"; ```
::after
``` content: "after"; ``` ]
1
] ] --- ### Absolute positioned items .slides-content[ - Are not part of the flex layout - Are initially positioned at the start of the **main axis** ] --- .slides-content[ .slides-columns[ .col[
Flex container
``` flex-direction: column-reverse; ```
Flex item 1
``` position: absolute; ```
Flex item 2
``` position: absolute; right: 0; ``` ]
1
2
3
] ] --- ### Margins of flex items .slides-content[ - Margins do not collapse - `margin: auto ` distributes the remaining space - `flex-grow` beats `margin: auto` ] --- .slides-content[ .slides-columns[ .col[
Flex item 1
``` margin-right: 5rem; ```
Flex item 2
``` margin: 5rem auto 0 auto; ``` ]
1
2
3
] ] --- .slides-content[ .slides-columns[ .col[
Flex item 1
``` margin-right: 5rem; ```
Flex item 2
``` margin: 5rem auto 0 auto; ```
Flex item 3
``` flex-grow: 1; ``` ]
1
2
3
] ] --- ### Source order independence is only visual .slides-content[ - Screen readers do not honor it - Navigation order for keyboard users is not affected ] --- layout: true class: theme-whiskey, slides-centered .slides-header[ Conclusion ] --- class: slides-chapter count: false background-image: url(./assets/strangelove-5.jpg) ## Conclusion --- ### `display: flex` allows us to... - specify complex layout rules for dynamic content - handle the distribution of positive and negative space - achieve true source order independence ---
--- [https://css-tricks.com/snippets/css/a-guide-to-flexbox/](https://css-tricks.com/snippets/css/a-guide-to-flexbox/)