DEV Community

Cover image for Mastering CSS Flexbox: A Hands-on Tutorial with Real-world Examples
Shrihari
Shrihari

Posted on

Mastering CSS Flexbox: A Hands-on Tutorial with Real-world Examples

Introduction
CSS Flexbox has revolutionized the way we build layouts on the web. It provides a powerful and intuitive way to create flexible and responsive designs. Whether you are a beginner or an experienced developer, mastering CSS Flexbox is essential for creating modern and dynamic user interfaces.

In this tutorial, we will take a hands-on approach to understanding CSS Flexbox. We will delve into its core concepts, explore its properties, and provide real-world examples to illustrate its practical applications. By the end of this tutorial, you will have a solid grasp of Flexbox and be equipped to create stunning layouts with ease.

  • Understanding Flexbox
  • Flexbox Properties
  1. display
  2. flex-direction
  3. justify-content
  4. align-items
  5. flex-wrap
  6. align-content
  • Real-world Examples
  • Example 1: Creating a Navbar
  • Example 2: Building a Pricing Grid
  • Example 3: Creating a Flexible Card Layout

Understanding Flexbox
Flexbox is a layout model that allows elements to align and distribute space within a container. It consists of two main components: flex containers and flex items. A flex container is any element that has its display property set to flex or inline-flex, and it acts as a parent container for flex items. Flex items are the child elements of the flex container.

The key concept in Flexbox is the flex container’s ability to distribute available space among its flex items. This distribution can be controlled through various flexbox properties, allowing for flexible, responsive, and dynamic layouts.

Flexbox Properties
Let’s explore some of the most commonly used Flexbox properties:

1. display
The display property is used to define the flex container. It can take one of two values: flex or inline-flex. The flex value creates a block-level flex container, while inline-flex creates an inline-level flex container.

2. flex-direction
The flex-direction property defines the direction in which the flex items are laid out within the flex container. It can take four values: row, row-reverse, column, and column-reverse.

3. justify-content
The justify-content property is used to align flex items along the main axis (horizontal axis by default). It can take values like flex-start, flex-end, center, space-between, space-around, and space-evenly, providing various ways to distribute space between flex items.

4. align-items
The align-items property is used to align flex items along the cross axis (vertical axis by default). It can take values like flex-start, flex-end, center, baseline, and stretch.

5. flex-wrap
The flex-wrap property controls whether flex items should wrap to the next line if there is not enough space in the container. It can take three values: nowrap (default), wrap, and wrap-reverse.

6. align-content
The align-content property is used to align multiple lines of flex items when they wrap onto new lines. It can take values like flex-start, flex-end, center, space-between, space-around, and stretch.

These are just a few of the many Flexbox properties available. Each property offers a unique way to control the layout of flex items within a Flexbox container.

Real-world Examples

Let’s dive into some real-world examples to showcase the power and versatility of CSS Flexbox. Each example will highlight a specific Flexbox property and demonstrate how it can be used to create compelling layouts.

Example 1: Creating a Navbar

One common use case for Flexbox is building a responsive navbar. In this example, we will use the justify-content and align-items properties to align the navbar links horizontally and vertically within the container. We will also utilize the flex-direction property to control the layout of the navbar items.

<nav class="navbar">
  <a href="#">Home</a>
  <a href="#">About</a>
  <a href="#">Services</a>
  <a href="#">Portfolio</a>
  <a href="#">Contact</a>
</nav>
Enter fullscreen mode Exit fullscreen mode
.navbar {
    display: flex;
    flex-direction: column; /* Mobile layout */
}

@media screen and (min-width: 768px) {
    .navbar {
        flex-direction: row; /* Desktop layout */
        justify-content: space-around; /* Horizontal alignment */
    }
}

.navbar a {
    text-decoration: none;
    padding: 10px;

Enter fullscreen mode Exit fullscreen mode

color: #333;
}

Example 2: Building a Pricing Grid

Flexbox excels at creating grid-like layouts. In this example, we will use Flexbox to build a responsive pricing grid with equal heights for each pricing plan. We will make use of the flex-direction, flex-wrap, and align-items properties to achieve this layout.

<div class="pricing-grid">
  <div class="pricing-plan">
      <h2>Basic Plan</h2>
      <p>Includes basic features</p>
      <span class="price">$10/month</span>
      <a href="#" class="btn">Sign Up</a>
  </div>
  <div class="pricing-plan">
      <h2>Pro Plan</h2>
      <p>Includes advanced features and support</p>
      <span class="price">$20/month</span>
      <a href="#" class="btn">Sign Up</a>
  </div>
  <div class="pricing-plan">
      <h2>Premium Plan</h2>
      <p>Includes premium features, support, and priority access</p>
      <span class="price">$30/month</span>
      <a href="#" class="btn">Sign Up</a>
  </div>
</div>
Enter fullscreen mode Exit fullscreen mode
/* Create a container for the pricing grid */
.pricing-grid {
    display: flex;
    justify-content: space-between; /* Distribute space between plans */
    max-width: 800px; /* Adjust as needed */
    margin: 0 auto; /* Center the grid horizontally */
}

/* Style each pricing plan */
.pricing-plan {
    text-align: center;
    border: 1px solid #ccc;
    padding: 20px;
    flex: 1; /* Distribute available space equally among plans */
    display: flex;
    flex-direction: column; /* Stack content vertically */
}

/* Style pricing details */
.price {
    font-size: 24px;
    font-weight: bold;
    color: #333;
    margin-top: auto; /* Push the price to the bottom of the card */
}

/* Style the "Sign Up" button */
.btn {
    display: inline-block;
    padding: 10px 20px;
    background-color: #007BFF;
    color: #fff;
    text-decoration: none;
    border-radius: 5px;
    margin-top: 10px;
}

/* Add hover effect to the button */
.btn:hover {
    background-color: #0056b3;
}
Enter fullscreen mode Exit fullscreen mode

Example 3: Creating a Flexible Card Layout

Flexbox is perfect for creating flexible card layouts. In this example, we will use Flexbox to create a responsive card layout with variable card heights. We will employ the flex-wrap and justify-content properties to ensure that the cards resize and wrap properly on different screen sizes.

<div class="card-container">
  <div class="card">
      <h2>Card 1</h2>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
  </div>
  <div class="card">
      <h2>Card 2</h2>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed euismod, diam ut aliquam elementum.</p>
  </div>
  <div class="card">
      <h2>Card 3</h2>
      <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed euismod, diam ut aliquam elementum, libero enim.</p>
  </div>
</div>
Enter fullscreen mode Exit fullscreen mode
.card-container {
    display: flex;
    flex-wrap: wrap; /* Allow cards to wrap to the next row */
    justify-content: space-between; /* Distribute space between cards */
    max-width: 800px; /* Adjust as needed */
    margin: 0 auto; /* Center the container horizontally */
}


/* Style the individual cards */
.card {
    background-color: #f2f2f2;
    border: 1px solid #ccc;
    padding: 20px;
    margin: 10px;
    flex: 1 0 calc(33.33% - 20px); /* Calculate width for three cards per row */
    min-width: 250px; /* Minimum card width */
    max-width: 350px; /* Maximum card width */
    display: flex;
    flex-direction: column;
}

/* Style card content */
.card h2 {
    font-size: 18px;
    margin-bottom: 10px;
}

.card p {
    flex-grow: 1;
    margin-bottom: 10px;
}
Enter fullscreen mode Exit fullscreen mode

These examples demonstrate just a fraction of what you can achieve with CSS Flexbox. The flexibility and versatility of Flexbox make it an essential tool for responsive web design and creating visually appealing layouts.

Conclusion
CSS Flexbox is a powerful layout tool that simplifies the process of creating flexible and responsive designs. By understanding its core concepts and exploring its properties through real-world examples, you can master Flexbox and unlock its full potential in your web projects. With the ability to align, distribute, and organize elements within a container, CSS Flexbox opens up new possibilities for crafting intuitive and dynamic user interfaces. So, embrace Flexbox and take your layout skills to the next level!

Enjoyed reading this ? Please share it with others.

Thanks for the read. Cheers!!!. You are Awesome !

Sharing is Caring. So Share as much as possible ;-)

Follow me on twitter/X
Follow me on linkedIn
Check out My portfolio

Top comments (0)