๐ช๐ช๐ช CSS Flexbox - Cheatsheet ๐ช๐ช๐ช
A quick, short, snazzy approach...
Table of contents
- I am back with another quick refresher cheatsheet to brush up your flexbox concepts in case you get stuck while writing code and need a rack up on your head for getting your flex moves in place... The idea is simple : Read less, learn fast, code more... like really really more... As always do not waste too much time on reading articles and implement actual projects !!!
- That is where the real learning happens ๐๐๐
- ๐ฆง What is Flexbox ? ๐ฆง
- ๐ฆง Why use it ? ๐ฆง
- ๐ The flex model ๐
- ๐ฆ Reference example : ๐ฆ
- ๐ฆง Ok... But what is flex-direction ? ๐ฆง
- ๐ Wrapping ๐
- ๐ Horizontal and vertical alignment ๐
- ๐ฏ๐ฏ๐ฏ Advanced Properties ๐ฏ๐ฏ๐ฏ
- ๐ฆ Reference example : ๐ฆ
- ๐ Combining properties ๐
- ๐ More on Flex-direction ๐
- ๐ More on Justify-Content ๐
- ๐ More on Align-Items ๐
- ๐ฆ Ordering flex items ๐ฆ
- So this sums up Flexbox properties for you. I hope you use it to your full advantage as this is an invaluable asset for frontend design using pure CSS and HTML.
- ๐๐๐ Happy Coding ๐๐๐
I am back with another quick refresher cheatsheet to brush up your flexbox concepts in case you get stuck while writing code and need a rack up on your head for getting your flex moves in place... The idea is simple : Read less, learn fast, code more... like really really more... As always do not waste too much time on reading articles and implement actual projects !!!
That is where the real learning happens ๐๐๐
๐ฆง What is Flexbox ? ๐ฆง
- Flexbox is a display property for DOM elements and a one-dimensional layout method for arranging items in rows or columns.
๐ฆง Why use it ? ๐ฆง
- It is probably the best way to arrange your elements in the DOM and can be a huge asset in mobile responsivity of your beloved website. Ofcourse, there are frontend frameworks like Bootstrap, Tailwind CSS, etc but guess what ? .... They too use flexbox...
๐ The flex model ๐
When elements are laid out as flex items, they are laid out along two axes:
- Main Axis
- Cross Axis
Do not confuse with the conventional mathematical X and Y axis !!! Main and cross axis can either be X or Y axis and vice versa.
For flex-direction : row
- Main Axis : X-axis
- Cross Axis : Y-axis
For flex-direction : column
- Main Axis : Y-axis
- Cross Axis : X-axis
๐ฆ Reference example : ๐ฆ
- Please consider the below code snippets as a starting point or reference for all topics mentioned :
<!-- HTML -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Flexbox</title>
</head>
<body>
<div class="container">
<div class="d">
<img src="./logo.svg" alt="">
</div>
<div class="d">
<img src="./logo.svg" alt="">
</div>
<div class="d">
<img src="./logo.svg" alt="">
</div>
<div class="d">
<img src="./logo.svg" alt="">
</div>
</div>
</body>
</html>
/* CSS */
body{
background-color: black;
}
.container{
}
๐ฆง Ok... But what is flex-direction ? ๐ฆง
Flexbox provides a property called flex-direction that specifies which direction the main axis runs (which direction the flexbox children are laid out in).
Default : row
So it basically tells how your elements would be laid out in your document, either horizontally (row) or vertically (column).
.container{
display: flex;
/* Not needed as flex-direction by default is row
flex-direction: row; */
}
.container{
display: flex;
flex-direction: column;
}
๐ Wrapping ๐
A very useful property to auto-align your elements when they overflow the container.
Typically used for mobile-responsiveness.
The nth element that tries to overflow the container would automatically shifted to the next row or column depending on the flex-direction.
/* Assume the images to be pretty big*/
.container{
display: flex;
flex-wrap: wrap;
}
- flex-flow shorthand :
- A shorthand exists for flex-direction and flex-wrap: flex-flow
flex-direction: row;
flex-wrap: wrap;
/* Shorthand*/
flex-flow: row wrap;
๐ Horizontal and vertical alignment ๐
You can also use flexbox features to align flex items along the main or cross axis. There are two main properties for this :
- justify-content
- align-items
justify-content : Controls where the flex items sit on the main axis.
align-items : Controls where the flex items sit on the cross axis.
.container{
display: flex;
justify-content: center;
}
.container{
height: 100vh;
display: flex;
align-items: center;
}
๐ฏ๐ฏ๐ฏ Advanced Properties ๐ฏ๐ฏ๐ฏ
- Now it's time to test a few advanced properties and values that we can use to tweak our flexbox experience...
๐ฆ Reference example : ๐ฆ
<!-- HTML -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Flexbox</title>
</head>
<body>
<div class="container">
<div class="red">
<img src="./logo.svg" alt="">
</div>
<div class="blue">
<img src="./logo.svg" alt="">
</div>
<div class="green">
<img src="./logo.svg" alt="">
</div>
<div class="purple">
<img src="./logo.svg" alt="">
</div>
</div>
</body>
</html>
/* CSS */
body{
background-color: lightcoral;
}
.red{
background-color: red;
width: 100px;
margin: 10px;
}
.blue{
background-color: blue;
width: 100px;
margin: 10px;
}
.green{
background-color: green;
width: 100px;
margin: 10px;
}
.purple{
background-color: purple;
width: 100px;
margin: 10px;
}
.container{
}
๐ Combining properties ๐
- We can combine certain flex properties to create a more cohesive effect.
.container{
display: flex;
height: 100vh;
justify-content: center;
align-items: center;
}
๐ More on Flex-direction ๐
- row-reverse will reverse the normal row direction.
- column-reverse will reverse the normal column direction.
.container{
display: flex;
height: 100vh;
justify-content: center;
align-items: center;
flex-direction: row-reverse;
}
.container{
display: flex;
height: 100vh;
justify-content: center;
align-items: center;
flex-direction: column-reverse;
}
๐ More on Justify-Content ๐
There are few more types of justify-content namely :
- start
- end
- space-between
- space-around
- space-evenly
- flex-start
- flex-end
- left
- right
- normal
justify-content: start ๐
- Pack items from the start
.container{
display: flex;
height: 100vh;
justify-content: start;
align-items: center;
}
- justify-content: end ๐
- Pack items from the end
.container{
display: flex;
height: 100vh;
justify-content: end;
align-items: center;
}
- justify-content: flex-start ๐
- Pack flex items from the start
.container{
display: flex;
height: 100vh;
justify-content: flex-start;
align-items: center;
}
- justify-content: flex-end ๐
- Pack flex items from the end
.container{
display: flex;
height: 100vh;
justify-content: flex-end;
align-items: center;
}
- justify-content: left ๐
- Pack items from the left
.container{
display: flex;
height: 100vh;
justify-content: left;
align-items: center;
}
- justify-content: right ๐
- Pack items from the right
.container{
display: flex;
height: 100vh;
justify-content: right;
align-items: center;
}
- justify-content: normal ๐
- Normal alignment
.container{
display: flex;
height: 100vh;
justify-content: normal;
align-items: center;
}
- justify-content: space-between ๐
- Distribute items evenly, the first item is flush with the start and the last is flush with the end.
.container{
display: flex;
height: 100vh;
justify-content: space-between;
align-items: center;
}
- justify-content: space-around ๐
- Distribute items evenly, Items have a half-size space on either end.
.container{
display: flex;
height: 100vh;
justify-content: space-around;
align-items: center;
}
- justify-content: space-evenly ๐
- Distribute items evenly, Items have equal space around them.
.container{
display: flex;
height: 100vh;
justify-content: space-evenly;
align-items: center;
}
๐ More on Align-Items ๐
There are few more types of align-items namely :
- stretch
- start
- end
- flex-start
- flex-end
align-items: stretch ๐ฆฎ
- Items will stretch across the whole cross axis.
.container{
display: flex;
height: 100vh;
justify-content: center;
align-items: stretch;
}
- align-items: start ๐ฆฎ
- Pack items from the start
.container{
display: flex;
height: 100vh;
justify-content: center;
align-items: start;
}
- align-items: end ๐ฆฎ
- Pack items from the end
.container{
display: flex;
height: 100vh;
justify-content: center;
align-items: end;
}
- align-items: flex-start ๐ฆฎ
- Pack flex items from the start
.container{
display: flex;
height: 100vh;
justify-content: center;
align-items: flex-start;
}
- align-items: flex-end ๐ฆฎ
- Pack flex items from the end
.container{
display: flex;
height: 100vh;
justify-content: center;
align-items: flex-end;
}
๐ฆ Ordering flex items ๐ฆ
- Flexbox also has a feature for changing the layout order of flex items without affecting the source order.
- By default, all flex items have an order value of 0.
- Flex items with higher specified order values will appear later in the display order than items with lower order values.
.red{
background-color: red;
width: 100px;
margin: 10px;
order: 2;
}
.blue{
background-color: blue;
width: 100px;
margin: 10px;
}
.green{
background-color: green;
width: 100px;
margin: 10px;
order: -1;
}
.purple{
background-color: purple;
width: 100px;
margin: 10px;
order: 1;
}
.container{
display: flex;
height: 100vh;
justify-content: center;
align-items: center;
}