display
property set to flex
or inline - flex
. It serves as a parent element that contains one or more flex items. For example:<!DOCTYPE html>
<html lang="en">
<head>
<style>
.flex-container {
display: flex;
background-color: lightblue;
}
</style>
</head>
<body>
<div class="flex-container">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
</div>
</body>
</html>
In this code, the div
with the class flex-container
is the flex container, and the three inner div
elements are the flex items.
flex-direction
: This property defines the direction of the main axis. It can take values like row
(default, left - to - right), row-reverse
(right - to - left), column
(top - to - bottom), and column-reverse
(bottom - to - top)..flex-container {
display: flex;
flex-direction: column;
}
flex-wrap
: It determines whether flex items should wrap onto multiple lines if there is not enough space on the main axis. Values include nowrap
(default, no wrapping), wrap
(wrap to the next line), and wrap-reverse
(wrap to the previous line in reverse order)..flex-container {
display: flex;
flex-wrap: wrap;
}
justify-content
: This property aligns flex items along the main axis. Values such as flex-start
(items are packed at the start of the main axis), flex-end
(items are packed at the end of the main axis), center
(items are centered along the main axis), space-between
(items are evenly distributed with space between them), and space-around
(items are evenly distributed with space around them)..flex-container {
display: flex;
justify-content: center;
}
flex-grow
: It defines how much a flex item should grow relative to the other flex items in the container when there is extra space on the main axis. A value of 0
means the item will not grow. For example:.item {
flex-grow: 1;
}
flex-shrink
: This property determines how much a flex item should shrink relative to the other flex items when there is not enough space on the main axis. A value of 0
means the item will not shrink..item {
flex-shrink: 0;
}
flex-basis
: It sets the initial size of a flex item before any remaining space is distributed. It can be a length value (e.g., 100px
) or a keyword like auto
(default, uses the item’s width/height)..item {
flex-basis: 200px;
}
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.nav {
display: flex;
justify-content: space-around;
background-color: lightgray;
}
.nav li {
list-style-type: none;
}
</style>
</head>
<body>
<ul class="nav">
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</body>
</html>
In this example, the ul
element is set as a flex container, and the li
elements are flex items. The justify-content: space-around
property evenly distributes the menu items with space around them.
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.card-container {
display: flex;
flex-wrap: wrap;
justify-content: center;
}
.card {
width: 200px;
margin: 10px;
border: 1px solid #ccc;
padding: 10px;
}
</style>
</head>
<body>
<div class="card-container">
<div class="card">
<h2>Card 1</h2>
<p>Some text here...</p>
</div>
<div class="card">
<h2>Card 2</h2>
<p>More text...</p>
</div>
<div class="card">
<h2>Card 3</h2>
<p>Even more text...</p>
</div>
</div>
</body>
</html>
Here, the card-container
is a flex container that wraps the cards onto multiple lines if necessary and centers them.
Centering elements both horizontally and vertically can be easily achieved with Flexbox.
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.center-container {
display: flex;
justify-content: center;
align-items: center;
height: 300px;
background-color: lightgreen;
}
.center-item {
background-color: white;
padding: 20px;
}
</style>
</head>
<body>
<div class="center-container">
<div class="center-item">
Centered Content
</div>
</div>
</body>
</html>
The justify-content: center
centers the item horizontally, and align-items: center
centers it vertically.
In a multi - column layout, Flexbox can ensure that all columns have the same height.
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.column-container {
display: flex;
}
.column {
flex: 1;
padding: 20px;
border: 1px solid #ccc;
}
</style>
</head>
<body>
<div class="column-container">
<div class="column">
<p>Column 1 content...</p>
</div>
<div class="column">
<p>Column 2 content that is longer...</p>
<p>More text in column 2...</p>
</div>
</div>
</body>
</html>
The flex: 1
property on each column makes them grow equally to fill the available space, resulting in equal - height columns.
Instead of using flex-grow
, flex-shrink
, and flex-basis
separately, use the flex
shorthand property. For example, flex: 1 1 auto
is equivalent to flex-grow: 1; flex-shrink: 1; flex-basis: auto;
.
Although Flexbox has good browser support, it’s still important to test your layouts in different browsers (Chrome, Firefox, Safari, Edge) to ensure consistent rendering.
A clean and simple HTML structure makes it easier to apply Flexbox styles. Avoid over - nesting elements when possible.
CSS Flexbox is a game - changer in web layout design. Its ability to create flexible, responsive, and efficient layouts with minimal code makes it an essential tool for modern web developers. By understanding the fundamental concepts, usage methods, common practices, and best practices, you can harness the full power of Flexbox to build engaging and user - friendly websites.