CSS FLEXBOX CHEATSHEET
Flexbox Cheatsheet
Display
Enable flex container, inline or block.
- display:flex
<!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">
<title>CSS Flexbox</title>
<style>
.container{
display: flex;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
</style>
</head>
<body>
<div class="container" style="background-color: black; width: 50%; height: 50%;">
<div class="child1"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: green"></div>
<div class="child2"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: orange"></div>
<div class="child3"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: red"></div>
<div class="child4"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: blue"></div>
</div>
</body>
</html>
OUTPUT
flex-direction
Specifies the direction of the flexible items.
- flex-direction: row;
- flex-direction: row-reverse;
- flex-direction: column;
- flex-direction: column-reverse;
1. flex-direction: row;
<style>
.container{
display: flex;
flex-direction: row;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
</style>
</head>
<body>
<div class="container" style="background-color: black; width: 50%; height: 50%;">
<div class="child1"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: green"></div>
<div class="child2"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: orange"></div>
<div class="child3"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: red"></div>
<div class="child4"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: blue"></div>
</div>
</body>
OUTPUT
2. flex-direction: row-reverse;
<style>
.container{
display: flex;
flex-direction: row-reverse;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
</style>
</head>
<body>
<div class="container" style="background-color: black; width: 50%; height: 50%;">
<div class="child1"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: green"></div>
<div class="child2"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: orange"></div>
<div class="child3"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: red"></div>
<div class="child4"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: blue"></div>
</div>
</body>
OUTPUT
3. flex-direction: column;
<style>
.container{
display: flex;
flex-direction: column;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
</style>
</head>
<body>
<div class="container" style="background-color: black; width: 50%; height: 50%;">
<div class="child1"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: green"></div>
<div class="child2"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: orange"></div>
<div class="child3"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: red"></div>
<div class="child4"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: blue"></div>
</div>
</body>
OUTPUT
4. flex-direction: column-reverse;
<style>
.container{
display: flex;
flex-direction: column-reverse;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
</style>
</head>
<body>
<div class="container" style="background-color: black; width: 50%; height: 50%;">
<div class="child1"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: green"></div>
<div class="child2"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: orange"></div>
<div class="child3"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: red"></div>
<div class="child4"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: blue"></div>
</div>
</body>
OUTPUT
flex-wrap
Specifies whether the flex items should wrap or not.
- flex-wrap: nowrap;
- flex-wrap: wrap;
- flex-wrap: wrap-reverse;
1. flex-wrap: nowrap;
<style>
.container{
display: flex;
flex-wrap: nowrap;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
</style>
</head>
<body>
<div class="container" style="background-color: black; width: 50%; height: 50%;">
<div class="child1"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: green"></div>
<div class="child2"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: orange"></div>
<div class="child3"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: red"></div>
<div class="child4"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: blue"></div>
</div>
</body>
OUTPUT
2. flex-wrap: wrap;
<style>
.container{
display: flex;
flex-wrap: wrap;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
</style>
</head>
<body>
<div class="container" style="background-color: black; width: 50%; height: 50%;">
<div class="child1"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: green"></div>
<div class="child2"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: orange"></div>
<div class="child3"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: red"></div>
<div class="child4"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: blue"></div>
</div>
</body>
OUTPUT
3. flex-wrap: wrap-reverse;
<style>
.container{
display: flex;
flex-wrap: wrap-reverse;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
</style>
</head>
<body>
<div class="container" style="background-color: black; width: 50%; height: 50%;">
<div class="child1"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: green"></div>
<div class="child2"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: orange"></div>
<div class="child3"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: red"></div>
<div class="child4"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: blue"></div>
</div>
</body>
OUTPUT
justify-content
Property is used to align the flex items.
- justify-content: flex-start;
- justify-content: flex-end;
- justify-content: center;
- justify-content: space-between;
- justify-content: space-around;
- justify-content: space-evenly;
1. justify-content: flex-start;
<style>
.container{
display: flex;
justify-content: flex-start;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
</style>
</head>
<body>
<div class="container" style="background-color: black; width: 50%; height: 50%;">
<div class="child1"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: green"></div>
<div class="child2"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: orange"></div>
<div class="child3"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: red"></div>
<div class="child4"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: blue"></div>
</div>
</body>
OUTPUT
2. justify-content: flex-end;
<style>
.container{
display: flex;
justify-content: flex-end;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
</style>
</head>
<body>
<div class="container" style="background-color: black; width: 50%; height: 50%;">
<div class="child1"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: green"></div>
<div class="child2"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: orange"></div>
<div class="child3"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: red"></div>
<div class="child4"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: blue"></div>
</div>
</body>
OUTPUT
3. justify-content: center;
<style>
.container{
display: flex;
justify-content: center;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
</style>
</head>
<body>
<div class="container" style="background-color: black; width: 50%; height: 50%;">
<div class="child1"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: green"></div>
<div class="child2"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: orange"></div>
<div class="child3"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: red"></div>
<div class="child4"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: blue"></div>
</div>
</body>
OUTPUT
4. justify-content: space-between;
<style>
.container{
display: flex;
justify-content: space-between;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
</style>
</head>
<body>
<div class="container" style="background-color: black; width: 50%; height: 50%;">
<div class="child1"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: green"></div>
<div class="child2"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: orange"></div>
<div class="child3"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: red"></div>
<div class="child4"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: blue"></div>
</div>
</body>
OUTPUT
5. justify-content: space-around;
<style>
.container{
display: flex;
justify-content: space-around;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
</style>
</head>
<body>
<div class="container" style="background-color: black; width: 50%; height: 50%;">
<div class="child1"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: green"></div>
<div class="child2"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: orange"></div>
<div class="child3"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: red"></div>
<div class="child4"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: blue"></div>
</div>
</body>
OUTPUT
6. justify-content: space-evenly;
<style>
.container{
display: flex;
justify-content: space-evenly;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
</style>
</head>
<body>
<div class="container" style="background-color: black; width: 50%; height: 50%;">
<div class="child1"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: green"></div>
<div class="child2"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: orange"></div>
<div class="child3"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: red"></div>
<div class="child4"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: blue"></div>
</div>
</body>
OUTPUT
align-items
Determines the behavior for how flex items are laid out along the cross axis on the current line.
- align-items: flex-start;
- align-items: flex-end;
- align-items: center;
- align-items: baseline;
- align-items: stretch;
1. align-items: flex-start;
<style>
.container{
display: flex;
align-items: flex-start;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
</style>
</head>
<body>
<div class="container" style="background-color: black; width: 50%; height: 50%;">
<div class="child1"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: green"></div>
<div class="child2"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: orange"></div>
<div class="child3"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: red"></div>
<div class="child4"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: blue"></div>
</div>
</body>
OUTPUT
2. align-items: flex-end;
<style>
.container{
display: flex;
align-items: flex-end;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
</style>
</head>
<body>
<div class="container" style="background-color: black; width: 50%; height: 50%;">
<div class="child1"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: green"></div>
<div class="child2"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: orange"></div>
<div class="child3"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: red"></div>
<div class="child4"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: blue"></div>
</div>
</body>
OUTPUT
3. align-items: center;
<style>
.container{
display: flex;
align-items: center;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
</style>
</head>
<body>
<div class="container" style="background-color: black; width: 50%; height: 50%;">
<div class="child1"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: green"></div>
<div class="child2"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: orange"></div>
<div class="child3"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: red"></div>
<div class="child4"><img src="/logo.png" alt="" style="width: 3rem; height: 3rem; background-color: blue"></div>
</div>
</body>
OUTPUT
align-content
Aligns a flex container’s lines within when there is extra space in the cross-axis.
- align-content: flex-start;
- align-content: flex-end;
- align-content: center;
- align-content: space-between;
- align-content: space-around;
- align-content: stretch;
Flexbox children
_order__
Controls the order in which flex items appear in the flex container.
order: 1;
flex-grow
Allows you to define the ability for a flex item to grow.
flex-grow: 1;
flex-basis
This defines the default size of an element before the remaining space is distributed.
flex-basis: 50%;
flex-shrink
This defines the ability for a flex item to shrink.
flex-shrink: 1;
align-self
Sets the alignment for individual items.
align-self: flex-start|flex-end|center|baseline|stretch;