Flexbox Container Properties
The flexbox has a flex-container which is also known as parent container. This container holds all the flex-items inside it. In this tutorial we will go through all the flex properties related to flex container.
Remember that these properties will be applied directly to the flex container and not on the flex-items. Let's get through them one by one.
CSS Display Flex
To start using Flexbox, the first thing to add is the property display:flex;
in the parent container.
Just after you add this property, your parent container will become a flex-container and all of its child will become flex-items. These flex-items will by-default align themselves in a row.
Divs are block level-elements so they take up all horizontal space available and the next div automatically get inserted on a new line. But, by using display:flex;
, Divs leave their default behaviour and get aligned one after another in a horizontal line. Let's see the example below:
<!DOCTYPE html>
<html lang="en">
<head>
<title> CSS Flex Box </title>
<meta charset="UTF-8">
<style>
.flex-container {
display: flex;
background-color: DodgerBlue;
}
.flex-item {
background-color: #f1f1f1;
margin: 10px;
padding: 20px;
font-size: 30px;
}
</style>
</head>
<body>
<div class="flex-container">
<div class="flex-item">A</div>
<div class="flex-item">B</div>
<div class="flex-item">C</div>
</div>
<p>Look how the display:flex; is getting used in parent container.</p>
</body>
</html>
Output
Look how the display:flex; is getting used in parent container.
CSS Flex Direction
After setting up the container as Flexbox, the browser by-default uses the row direction for the flex-items, i.e., items get placed from left to right. But, what if we want to change the direction?
CSS Flex direction property makes it possible to display the flex-items as we want. They can be aligned in a horizontal row, in a vertical column, in a row reverse direction or in a column reverse direction. Let's see them one by one:
- flex-direction:row; - It is the default property. It place flex-items from left to right in a row.
- flex-direction:column; - It place the flex-items from top to bottom in a column.
- flex-direction:row-reverse; - It places items from right to left and it also reverses the order of flex-items. The first item will be added last and vice versa.
- flex-direction:column-reverse; - As the name suggests, it will place items from bottom to top and also but in a reverse order.
Let's see all the properties in an example:
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container1 {
display: flex;
flex-direction:row;
background-color: DodgerBlue;
}
.flex-container2 {
display: flex;
flex-direction:column;
background-color: red;
}
.flex-container3 {
display: flex;
flex-direction:row-reverse;
background-color: tomato;
}
.flex-container4 {
display: flex;
flex-direction:column-reverse;
background-color: green;
}
.flex-item {
background-color: #f1f1f1;
margin: 5px;
padding: 10px;
font-size: 15px;
width:100px;
}
</style>
</head>
<body>
<h2>Flex Direction Row</h2>
<div class="flex-container1">
<div class="flex-item">A</div>
<div class="flex-item">B</div>
<div class="flex-item">C</div>
</div>
<h2>Flex Direction column</h2>
<div class="flex-container2">
<div class="flex-item">A</div>
<div class="flex-item">B</div>
<div class="flex-item">C</div>
</div>
<h2>Flex Direction Row-reverse</h2>
<div class="flex-container3">
<div class="flex-item">A</div>
<div class="flex-item">B</div>
<div class="flex-item">C</div>
</div>
<h2>Flex Direction Column-reverse</h2>
<div class="flex-container4">
<div class="flex-item">A</div>
<div class="flex-item">B</div>
<div class="flex-item">C</div>
</div>
<p>Look how the display:flex; is getting used in parent container.</p>
</body>
</html>
Output
Flex Direction Row
Flex Direction column
Flex Direction Row-reverse
Flex Direction Column-reverse
Look how the display:flex; is getting used in parent container.
CSS Flex Wrap
Flex Wrap is a very useful property in case of overflow of content. By default, CSS Flexbox tries to fit all the elements in a single row, it can result into overflow of content outside of the flex-container.
Flex Wrap property automatically places the flex-items in the next row, when they don't fit in a single row.
There are three values which you can give to this property:
- Flex-wrap: Wrap; - It activates the Wrap property. Now, all the overflown content will slip into next row and out of the container.
- Flex-wrap: Wrap-reverse; - It also wraps the content but all the flex-items will be wrapped in a reverse order.
- Flex-wrap: Nowrap; - It is the default value, flex-items won't get wrapped.
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container1 {
display: flex;
flex-wrap: nowrap;
background-color: Tomato;
height: auto;
}
.flex-container2 {
display: flex;
flex-wrap: wrap;
background-color: Tomato;
height: auto;
}
.flex-container3 {
display: flex;
flex-wrap: wrap-reverse;
background-color: Tomato;
height: auto;
}
.flex-items {
background-color: #f1f1f1;
padding: 10px 25px;
text-align: center;
height: 20px;
width: 100px;
margin: 5px;
}
</style>
</head>
<body>
<h3>Flex-wrap: nowrap; </h3>
<div class="flex-container1">
<div class="flex-items">1</div>
<div class="flex-items">2</div>
<div class="flex-items">3</div>
<div class="flex-items">4</div>
<div class="flex-items">5</div>
<div class="flex-items">6</div>
<div class="flex-items">7</div>
<div class="flex-items">8</div>
</div>
<h3>Flex-wrap: wrap;</h3>
<div class="flex-container2">
<div class="flex-items">1</div>
<div class="flex-items">2</div>
<div class="flex-items">3</div>
<div class="flex-items">4</div>
<div class="flex-items">5</div>
<div class="flex-items">6</div>
<div class="flex-items">7</div>
<div class="flex-items">8</div>
</div>
<h3>Flex-wrap: wrap-reverse;</h3>
<div class="flex-container3">
<div class="flex-items">1</div>
<div class="flex-items">2</div>
<div class="flex-items">3</div>
<div class="flex-items">4</div>
<div class="flex-items">5</div>
<div class="flex-items">6</div>
<div class="flex-items">7</div>
<div class="flex-items">8</div>
</div>
</body>
</html>
Output
Flex-wrap: nowrap;
Flex-wrap: wrap;
Flex-wrap: wrap-reverse;
CSS Flex Flow (Shorthand property)
CSS Flex Flow property is a shorthand property in which we can combine both Flex-direction
and Flex-Wrap
properties. We can give the values for both the properties just by separating them with a space.
Syntax
<Flex-direction> <Flex-wrap>;
Example- Flex-flow: column wrap;
Let's take a loot at the example:
<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
display: flex;
flex-flow: row wrap;
background-color: Tomato;
}
.flex-items {
background-color: #f1f1f1;
width: 100px;
margin: 5px;
text-align: center;
height: 40px;
font-size: 20px;
}
</style>
</head>
<body>
<h2>Flex-flow</h2>
<p>Notice how perfectly both flex-direction and flex-wrap are working.</p>
<div class="flex-container">
<div class="flex-items">1</div>
<div class="flex-items">2</div>
<div class="flex-items">3</div>
<div class="flex-items">4</div>
<div class="flex-items">5</div>
<div class="flex-items">6</div>
</div>
</body>
</html>
Output
Flex-flow
Notice how perfectly both flex-direction and flex-wrap are working.