DailyDevDiet

logo - dailydevdiet

Learn. Build. Innovate. Elevate your coding skills with dailydevdiet!

What is CSS Flexbox?: A Beginner’s Guide to Responsive Layouts

what is css flexbox - dailydevdiet

CSS Flexbox is one of the most powerful and flexible layout models in web development today. It allows developers to create dynamic, responsive layouts with ease. Whether you’re designing complex web pages or aligning simple elements, CSS Flexbox offers a streamlined solution that simplifies many of the challenges associated with traditional layout techniques like floats or inline-block elements.

In this comprehensive Flexbox tutorial, we will explore what is CSS Flexbox, how it works, and how you can use it to create responsive layouts. If you’re a beginner in web development, mastering CSS layout with Flexbox is an essential skill that will enable you to build websites that look great on any device.

What is CSS Flexbox?

CSS Flexbox commonly known as Flexible Box Layout. It is a CSS web layout model that provides an easy way to arrange items inside a container, distributing space dynamically between them. It is particularly effective for building responsive layouts where elements should adapt to different screen sizes.

The Flexbox mainly helps you to align and distribute space among items in a container. The container is called a flex container, whereas its children are called flex items. With CSS Flexbox, items can automatically adjust their size and position, offering a more intuitive approach to layout design.

Key Concepts in Flexbox

Before diving into practical examples, it’s important to understand some key terms in CSS Flexbox:

  • Flex Container: It is the parent element that contains flex items. An element can be declared as a flex container by applying the display: flex; property to the container.
  • Flex Items: The child elements of the flex container that will be aligned and sized by Flexbox properties.
  • Main Axis and Cross Axis: Flexbox works by aligning items along two axes: the main axis (horizontal by default) and the cross axis (vertical by default).

How to Use CSS Flexbox

Let’s start with some basic code to set up a CSS layout with Flexbox.

Step 1: Defining the Flex Container

To turn any container into a flex container, use the display: flex; property. It transforms the behavior of the container’s children, allowing them to be aligned along the main axis.

.container {
  display: flex;
}

Once the container is set to display: flex;, it becomes the flex container and  all the direct child elements become flex items.

Step 2: Aligning Flex Items

One of the biggest advantages of CSS Flexbox and which makes it so easy is its ability to align items easily. Here are some common alignment properties:

  • justify-content: This property basically aligns all the items in a container along the main axis (horizontally by default). Some options include:
    • flex-start (default)
    • center
    • stretch
    • baseline
.container {
  display: flex;
  justify-content: center; /* aligns items to the center horizontally */
}
  • align-items: This property helps you align items along the cross axis (vertically by default). Some options include:
    • flex-start
    • center
    • stretch (default)
    • baseline
.container {
  display: flex;
  align-items: center; /* aligns items to the center vertically */
}

Step 3: Flex Direction

By default the flex direction is row i.e. flex items are arranged in a row (horizontally), which can be changed using the flex-direction property. This is useful for responsive layouts where you may want items stacked in a column on smaller screens.

  • row (default)
  • row-reverse
  • column
  • column-reverse

Building a Responsive Layout with Flexbox

Now that you understand the basics, let’s create a Flexbox responsive layout. In this example, we will build a simple three-column layout that adjusts its arrangement based on screen size. We’ll continue our discussion on what is CSS Flexbox and try to understand it working with the help an example.

HTML Structure

<div class="container">
  <div class="item">Item 1</div>
  <div class="item">Item 2</div>
  <div class="item">Item 3</div>
</div>

CSS Styles

.container {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
}

.item {
  background-color: #f0f0f0;
  padding: 20px;
  margin: 10px;
  flex: 1 1 30%;
  text-align: center;
}

Explanation of CSS

  • display: flex;: Declare the parent container as a flex container.
  • justify-content: space-between;: Distributes the available space between all the flex items.
  • flex-wrap: wrap;: Allows items to wrap to the next line if necessary.
  • flex: 1 1 30%;: Defines the flexibility of each item, allowing them to take up 30% of the available space.

If you want to learn CSS Flexbox in a fun and interactive way, visit FlexboxFroggy.

Making the Layout Responsive

To ensure the layout adapts to smaller screens, use media queries to adjust the flex direction or item size.

@media (max-width: 768px) {
  .item {
    flex: 1 1 100%; /* Stacks items vertically on smaller screens */
  }
}

With this simple adjustment, the layout will adapt to mobile devices, stacking the items in a single column.

Advanced Flexbox Techniques

Now that you have the basics of CSS Flexbox down, let’s explore some more advanced techniques that can further enhance your responsive layouts.

1. Flexbox Alignment

Apart from justify-content and align-items properties for alignment, you can also make use of:

  • align-self: Which allows individual flex items to override the alignment set by align-items.
.item {
  align-self: flex-end; /* Aligns this particular item to the end of the container */
}

2. Flexible Sizing with Flex Grow, Shrink, and Basis

  • flex-grow: Allows you control how much an item should grow relative to other items in the container.
  • flex-shrink: This property helps you control how much an item should shrink relative to other items when there’s not enough space.
  • flex-basis: You can set the initial size of a flex item using flex-basis before any growing or shrinking occurs.
.item {
  flex-grow: 2; /* This item will grow twice as much as other items */
  flex-shrink: 1;
  flex-basis: 200px; /* The initial size is 200px */
}

3. Using Order to Re-arrange Items

Flexbox allows you to change the order of items without modifying the HTML structure. This is useful for responsive designs where you want to display content differently on mobile.

.item:nth-child(3) {
  order: -1; /* Moves the third item to the first position */
}

Common Flexbox Mistakes to Avoid

While CSS Flexbox is a powerful tool, beginners often make a few common mistakes:

  • Not Understanding the Axis System: Flexbox aligns items along two axes: the main axis and the cross axis. Once you fully understand these two axes, you can easily control the layout. 
  • Forgetting flex-wrap: By default, Flexbox does not allow items to wrap onto new lines. Always use flex-wrap: wrap; for responsive designs.
  • Overusing Flex Properties: Not every layout requires Flexbox. Use it where it simplifies your layout, but don’t over-complicate simple designs.

Conclusion

Hopefully, the above article answer the question what is CSS Flexbox? The rich features discussed above make the CSS Flexbox an essential tool for every modern web developer, offering a flexible and efficient way to build responsive layouts. By mastering CSS layout with Flexbox, you can create designs that are both powerful and adaptable to various screen sizes.

Whether you’re a beginner or an experienced developer, this Flexbox tutorial provides the foundation you need to start building responsive layouts today. So dive into CSS Flexbox, experiment with its properties, and transform your web layouts into dynamic, mobile-friendly designs.

Remember, Flexbox is all about flexibility — once you understand how to align, distribute, and size elements, you’ll be well on your way to becoming a Flexbox pro!

P.S.: Centering an element inside a div has always been tricky for a developer. We have discussed most commonly used tricks to center an element using CSS here.

Scroll to Top