10.1 Simple Grid
Flexbox lets you create flexible and responsive layouts. We'll check out some examples of using Flexbox to create different layouts, including a simple grid, centering elements, creating navigation menus, and product cards.
Creating a simple grid using Flexbox. This example shows how you can easily create evenly distributed elements in a container.
Usage Example:
.flex-container {
display: flex;
flex-wrap: wrap; /* Allows items to wrap onto a new line */
background-color: lightgrey;
padding: 10px;
}
.flex-item {
background-color: deepskyblue;
margin: 10px;
padding: 20px;
color: white;
font-size: 20px;
flex: 1 1 calc(33.333% - 40px); /* Flexible space distribution */
box-sizing: border-box;
}
<div class="flex-container">
<div class="flex-item">Item 1</div>
<div class="flex-item">Item 2</div>
<div class="flex-item">Item 3</div>
<div class="flex-item">Item 4</div>
<div class="flex-item">Item 5</div>
<div class="flex-item">Item 6</div>
</div>
flex-basis: calc(33.333% - 40px): sets the initial size of an element before any stretching or shrinking is applied. In this case, the element will take up 33.333% of the container width minus 40 pixels. calc() lets you perform calculations right in CSS.
10.2 Centering Elements
Centering elements horizontally and vertically in a container using Flexbox.
Usage Example:
.flex-container {
display: flex;
justify-content: center; /* Center horizontally */
align-items: center; /* Center vertically */
height: 100vh; /* Container height full screen */
background-color: lightgrey;
}
.flex-item {
background-color: deepskyblue;
padding: 20px;
color: white;
font-size: 20px;
}
<div class="flex-container">
<div class="flex-item">Centered Item</div>
</div>
10.3 Navigation Menu
Creating a horizontal navigation menu using Flexbox.
Usage Example:
.nav-container {
display: flex;
justify-content: space-around; /* Distribute items with equal spacing */
background-color: #333;
padding: 10px;
}
.nav-item {
color: white;
text-decoration: none;
padding: 10px 20px;
}
.nav-item:hover {
background-color: #575757;
}
<div class="nav-container">
<a href="#" class="nav-item">Home</a>
<a href="#" class="nav-item">About</a>
<a href="#" class="nav-item">Services</a>
<a href="#" class="nav-item">Contact</a>
</div>
10.4 Product Cards
Creating a product card layout using Flexbox.
Usage Example:
.product-list {
display: flex;
flex-wrap: wrap;
gap: 20px;
padding: 20px;
}
.product-item {
flex: 1 1 calc(33.333% - 40px);
border: 1px solid #ccc;
padding: 20px;
background-color: #fff;
text-align: center;
}
.product-item img {
max-width: 100%;
height: auto;
}
.product-title {
font-size: 1.2em;
margin: 10px 0;
}
.product-price {
font-size: 1.5em;
color: #e74c3c;
}
<div class="product-list">
<div class="product-item">
<img src="https://via.placeholder.com/150" alt="Product Image">
<h3 class="product-title">Product 1</h3>
<p class="product-price">$99.99</p>
</div>
<div class="product-item">
<img src="https://via.placeholder.com/150" alt="Product Image">
<h3 class="product-title">Product 2</h3>
<p class="product-price">$79.99</p>
</div>
<div class="product-item">
<img src="https://via.placeholder.com/150" alt="Product Image">
<h3 class="product-title">Product 3</h3>
<p class="product-price">$89.99</p>
</div>
</div>
Code Explanation:
.product-list: A flex container with wrap support and gaps between product cards.product-item: Flex items (product cards) with fixed width that automatically adapt to the container size
GO TO FULL VERSION