cheatsheets/css-flexbox.md

197 lines
2.9 KiB
Markdown
Raw Permalink Normal View History

2014-04-28 12:57:58 +02:00
---
title: CSS flexbox
2015-11-24 05:48:01 +01:00
category: CSS
2020-07-04 15:33:09 +02:00
updated: 2020-06-13
2017-08-30 16:03:16 +02:00
prism_languages: [css]
2017-08-29 17:57:21 +02:00
weight: -3
2014-04-28 12:57:58 +02:00
---
2017-08-29 17:57:21 +02:00
### Simple example
```css
.container {
display: flex;
}
```
```css
.container > div {
flex: 1 1 auto;
}
```
### Container
```css
.container {
```
{: .-setup}
```css
display: flex;
display: inline-flex;
```
```css
flex-direction: row; /* ltr - default */
flex-direction: row-reverse; /* rtl */
flex-direction: column; /* top-bottom */
flex-direction: column-reverse; /* bottom-top */
```
```css
flex-wrap: nowrap; /* one-line */
flex-wrap: wrap; /* multi-line */
```
```css
align-items: flex-start; /* vertical-align to top */
align-items: flex-end; /* vertical-align to bottom */
align-items: center; /* vertical-align to center */
align-items: stretch; /* same height on all (default) */
```
```css
2020-06-13 02:20:17 +02:00
justify-content: flex-start; /* [xxx ] */
justify-content: center; /* [ xxx ] */
justify-content: flex-end; /* [ xxx] */
justify-content: space-between; /* [x x x] */
justify-content: space-around; /* [ x x x ] */
justify-content: space-evenly; /* [ x x x ] */
2017-08-29 17:57:21 +02:00
```
```css
}
```
{: .-setup}
### Child
```css
.container > div {
```
{: .-setup}
```css
/* This: */
flex: 1 0 auto;
/* Is equivalent to this: */
flex-grow: 1;
flex-shrink: 0;
flex-basis: auto;
```
```css
order: 1;
```
```css
align-self: flex-start; /* left */
margin-left: auto; /* right */
```
```css
}
```
{: .-setup}
2014-04-28 12:57:58 +02:00
2014-06-09 09:12:20 +02:00
## Tricks
2014-06-07 16:32:02 +02:00
### Vertical center
2014-04-28 12:57:58 +02:00
2017-08-29 17:57:21 +02:00
```css
.container {
display: flex;
}
2014-04-28 12:57:58 +02:00
2017-08-29 17:57:21 +02:00
.container > div {
width: 100px;
height: 100px;
margin: auto;
}
```
2014-06-04 09:14:41 +02:00
2016-03-28 08:56:43 +02:00
### Vertical center (2)
2017-08-29 17:57:21 +02:00
```css
.container {
display: flex;
align-items: center; /* vertical */
justify-content: center; /* horizontal */
}
```
2016-03-28 08:56:43 +02:00
2014-06-07 16:32:02 +02:00
### Reordering
2017-08-29 17:57:21 +02:00
```css
.container > .top {
order: 1;
}
2016-01-07 05:47:48 +01:00
2017-08-29 17:57:21 +02:00
.container > .bottom {
order: 2;
}
```
2014-06-07 16:32:02 +02:00
### Mobile layout
2014-06-04 09:14:41 +02:00
2014-06-09 09:12:20 +02:00
2017-08-29 17:57:21 +02:00
```css
.container {
display: flex;
flex-direction: column;
}
2014-06-04 09:14:41 +02:00
2017-08-29 17:57:21 +02:00
.container > .top {
flex: 0 0 100px;
}
2014-06-04 09:14:41 +02:00
2017-08-29 17:57:21 +02:00
.container > .content {
flex: 1 0 auto;
}
```
A fixed-height top bar and a dynamic-height content area.
2014-06-04 09:14:41 +02:00
2014-06-09 09:12:20 +02:00
### Table-like
2017-08-29 17:57:21 +02:00
```css
.container {
display: flex;
}
/* the 'px' values here are just suggested percentages */
.container > .checkbox { flex: 1 0 20px; }
.container > .subject { flex: 1 0 400px; }
.container > .date { flex: 1 0 120px; }
```
2014-06-09 09:12:20 +02:00
This creates columns that have different widths, but size accordingly according
to the circumstances.
2017-08-29 17:57:21 +02:00
### Vertical
2014-06-09 09:12:20 +02:00
2017-08-29 17:57:21 +02:00
```css
.container {
align-items: center;
}
```
2016-01-07 05:47:48 +01:00
2014-06-09 09:12:20 +02:00
Vertically-center all items.
2016-03-28 08:56:43 +02:00
### Left and right
2017-08-29 17:57:21 +02:00
```css
.menu > .left { align-self: flex-start; }
.menu > .right { align-self: flex-end; }
```
2016-03-28 08:56:43 +02:00
2017-08-29 17:57:21 +02:00
## References
{: .-one-column}
2014-06-09 09:12:20 +02:00
* [MDN: Using CSS flexbox](https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Flexible_boxes)
* [Ultimate flexbox cheatsheet](https://www.sketchingwithcss.com/samplechapter/cheatsheet.html)