Cascading Style Sheets/Flexbox
This page was last edited on 11 December 2024, and is still under heavy construction. Content that is added is likely to be moved/deleted/edited significantly in a short amount of time. All Wikibookians with knowledge in this subject are welcome to help out. You can remove this tag when the book has become more mature. |
Flexbox is a great way to improve layouts in web pages. It is supported in every major browsers.
Container
[edit | edit source]flex-direction
[edit | edit source]column
[edit | edit source]column-reverse
[edit | edit source]row
[edit | edit source]row-reverse
[edit | edit source]flex-wrap
[edit | edit source]nowrap
[edit | edit source]As you can see, the elements are squeezed to fit the box.
wrap
[edit | edit source]As you can see, the elements are wrapped.
wrap-reverse
[edit | edit source]As you can see, the elements are wrapped from bottom to top.
flex-flow
[edit | edit source]Syntax: flex-flow: flex-direction flex-wrap;
justify-content
[edit | edit source]center
[edit | edit source]flex-start
[edit | edit source]flex-end
[edit | edit source]space-evenly
[edit | edit source]space-around
[edit | edit source]space-between
[edit | edit source]align-items
[edit | edit source]A container height of 4em is used to demonstrate these examples.
center
[edit | edit source]flex-start
[edit | edit source]flex-end
[edit | edit source]stretch
[edit | edit source]baseline
[edit | edit source]This demonstration is shown with different font sizes. The position matches the bottom of text.
align-content
[edit | edit source]A container height of 16em is used to demonstrate these examples. The container must wrap the items in order to work.
flex-start
[edit | edit source]flex-end
[edit | edit source]center
[edit | edit source]space-evenly
[edit | edit source]space-around
[edit | edit source]space-between
[edit | edit source]gap
[edit | edit source]This CSS function for flexbox is a fairly new function. It is introduced in Chrome and Edge 84, Firefox 63, Opera 70 and Safari 14.1. It measures the gutter between flex items.
This is a example with 4px gap around the items.
This is another example with 12px 6px gap around the items (12px between rows, 6px between columns).
Items
[edit | edit source]order
[edit | edit source]This property determines the order of each flex item. It is sorted from lowest to highest.
This demonstration shows:
- 1st div shows an order of 4
- 2nd div shows an order of 6
- 3rd div shows an order of 2
- 4th div shows an order of 3
- 5th div shows an order of 1
- 6th div shows an order of 5
flex-grow
[edit | edit source]This property controls the growth relative to each flex item.
flex-shrink
[edit | edit source]This property controls the shrinkage relative to each flex item. Width of 25% for each item is used (150% in total), allowing for shrinking without wrapping.