Flexbox is a CSS layout model that allows you to design complex layouts with ease. It is especially useful for creating responsive designs that adapt to different screen sizes. there have some flexBox
example
To start using Flexbox, you need to set the display
property of a container element to flex
. This makes its children flex items.
<div class="container">
<div class="item">Item 1</div>
<div class="item">Item 2</div>
<div class="item">Item 3</div>
</div>
<style>
.container {
display: flex;
}
.item {
background: #f4f4f4;
margin: 10px;
padding: 20px;
border: 1px solid #ddd;
}
</style>
The flex-direction
property defines the direction of the flex items inside the container. It can be row
(default), row-reverse
, column
, or column-reverse
.
<style>
.container {
display: flex;
flex-direction: row; /* Items in a row */
}
</style>
The justify-content
property aligns the flex items along the main axis (horizontally if flex-direction
is row
). Options include flex-start
, flex-end
, center
, space-between
, space-around
, and space-evenly
.
<style>
.container {
display: flex;
justify-content: space-between; /* Distribute items with space between */
}
</style>
The align-items
property aligns the flex items along the cross axis (vertically if flex-direction
is row
). It can be stretch
(default), flex-start
, flex-end
, center
, or baseline
.
<style>
.container {
display: flex;
align-items: center; /* Center items vertically */
}
</style>
The align-content
property aligns the flex lines within the flex container when there is extra space on the cross axis. It affects multi-line containers and can be flex-start
, flex-end
, center
, space-between
, space-around
, or stretch
.
<style>
.container {
display: flex;
flex-wrap: wrap; /* Allow items to wrap */
align-content: space-between; /* Space between rows */
}
</style>
The flex-wrap
property determines whether the flex items should wrap or not. It can be nowrap
(default), wrap
, or wrap-reverse
.
<style>
.container {
display: flex;
flex-wrap: wrap; /* Allow items to wrap onto multiple lines */
}
</style>
flex-basis
: Specifies the initial size of a flex item before any growing or shrinking.
<style>
.item {
flex-grow: 1; /* All items grow equally */
flex-basis: 100px; /* Initial size */
}
</style>
flex-shrink
: Specifies how much a flex item should shrink relative to the rest.
<style>
.item {
flex-grow: 1; /* All items grow equally */
flex-shrink: 100px; /* Initial size */
}
</style>
flex-grow
: Specifies how much a flex item should grow relative to the rest
<style>
.item {
flex-grow: 1; /* All items grow equally */
flex-grow: 100px; /* Initial size */
}
</style>
<div class="container">
<div class="item">Home</div>
<div class="item">About</div>
<div class="item">Services</div>
<div class="item">Contact</div>
</div>
<style>
.container {
display: flex;
flex-direction: row;
justify-content: space-around;
align-items: center;
flex-wrap: wrap;
padding: 20px;
background-color: #f0f0f0;
}
.item {
background: #007BFF;
color: white;
padding: 15px 20px;
margin: 5px;
flex-grow: 1;
text-align: center;
border-radius: 5px;
}
</style>