The flexible box module, also referred to as flexbox helps us in designing a responsive website. To start using the flexbox model, we need to first define a flex container.
<div class="container">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
The element above represents a flex container(blue area) with three flex items in it.
Flexbox Properties:-
- display
- flex-direction
- flex-wrap
- flex-flow
- justify-content
- align-items
- align-content
display:-
The flex container becomes flexible by applying display property to flex.
<style>
.container{
height: 100%;
display:flex;
}
body{
height: 100vh;
}
img{
width: 300px;
height: 200px;
}
</style>
</head>
<body>
<div class="container">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
</div>
</body>
flex-direction:-
The flex-direction property defines in which direction the container wants to stack the flex items. Here, we can use four values like row, row-reverse, column, column-reverse.
<style>
.container{
height: 100%;
display:flex;
flex-direction: row;
}
body{
height: 100vh;
}
img{
width: 300px;
height: 200px;
}
</style>
</head>
<body>
<div class="container">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
</div>
</body>
flex-wrap:-
The flex-wrap property defines whether the flex items should wrap or not. Here, we can use two values like wrap, nowrap.
<style>
.container{
height: 100%;
display:flex;
flex-wrap: wrap;
}
body{
height: 100vh;
}
img{
width: 300px;
}
</style>
</head>
<body>
<div class="container">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
</div>
</body>
flex-flow:-
The flex-flow property is a shorthand property to set both flex-direction and flex-wrap properties.
<style>
.container{
height: 100%;
display:flex;
flex-flow: column wrap;
}
body{
height: 100vh;
}
img{
width: 300px;
}
</style>
</head>
<body>
<div class="container">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
</div>
</body>
justify-content:-
The justify-content property is used to align the flex items. We can use different values like center, space-around, space-between, flex-start, flex-end. The center value aligns the flex items in the center of the container.
<style>
.container{
height: 100%;
display:flex;
flex-flow: column wrap;
justify-content: center;
}
body{
height: 100vh;
}
img{
width: 300px;
}
</style>
</head>
<body>
<div class="container">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
</div>
</body>
align-items:-
The align-items property is used to align the flex items. We can use different values for align-items like center. flex-start, flex-end, stretch, baseline.
<style>
.container{
height: 100%;
display:flex;
flex-flow: column wrap;
align-items: center;
}
body{
height: 100vh;
}
img{
width: 300px;
}
</style>
</head>
<body>
<div class="container">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
</div>
</body>
align-content:-
The align-content property is used to align the flex lines. We can use different values like space-between, space-around, stretch, center, flex-start, flex-end.
<style>
.container{
height: 100%;
display:flex;
flex-flow: column wrap;
align-items: center;
align-content: space-between;
}
body{
height: 100vh;
}
img{
width: 300px;
}
</style>
</head>
<body>
<div class="container">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
<img src="https://images.unsplash.com/photo-1500576992153-0271099def59?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8MTR8fHN0aWNrZXJzfGVufDB8fDB8fA%3D%3D&auto=format&fit=crop&w=600&q=60" alt="help corner">
</div>
</body>
How to center a flex item ?
Solution:- The famous problem to center a flex item can be solved by using the flex-box properties. We just need to set properties for the container according to the flexbox and it will be all centered. We can do so by setting display:flex
, justify-content:center
, align-items:center
.container{
display:flex;
justify-content:center;
align-items:center;
}
Thank you for reading this article. Please like, comment and share your valuable feedback.