This Flexbox layout is easier to design flexible responsive layouts without using float or positioning.
Before flexbox layout, there were other layouts.
Rule
- To start using the Flexbox model, you need to first define a flex container.
A Flexible Layout must have a parent element with the display property set to flex.
Direct child elements of the flexible container automatically becomes flexible items.
HTML
<div class="flex-container">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
CSS
.flex-container {
display: flex;
background-color: lightgreen;
height: 100px;
width: 50%;
}
.flex-container > div {
margin: 10px;
height: 80px;
width: 80px;
padding: 20px;
background-color: blueviolet;
font-size: 2rem;
text-align: center;
}
This will produce the following result −
CSS
.flex-container {
display: flex;
flex-direction: column;
background-color: lightgreen;
width: 50%;
}
This will produce the following result −
CSS
.flex-container {
display: flex;
justify-content: center;
background-color: lightgreen;
height: 100px;
width: 50%;
}
This will produce the following result −
CSS
.flex-container {
display: flex;
justify-content: space-around;
background-color: lightgreen;
height: 100px;
width: 50%;
}
This will produce the following result −
CSS
.flex-container {
display: flex;
justify-content: space-around;
align-items: center;
background-color: lightgreen;
height: 400px;
width: 50%;
}
This will produce the following result −
CSS with nowrap
.flex-container {
display: flex;
background-color: lightgreen;
height: 200px;
width: 50%;
}
This will produce the following result −
CSS with wrap
.flex-container {
display: flex;
flex-wrap: wrap;
background-color: lightgreen;
height: 200px;
width: 50%;
}
This will produce the following result −