DEV Community

Cover image for Learn CSS Flexbox: A Simple Guide for Beginners to Design Layouts
Eleftheria Batsou
Eleftheria Batsou

Posted on • Originally published at eleftheriabatsou.hashnode.dev on

Learn CSS Flexbox: A Simple Guide for Beginners to Design Layouts

Introduction

If you've ever struggled with aligning elements or creating responsive designs, Flexbox is here to simplify your life. Introduced to make the layout of web pages easier to manage, Flexbox streamlines what used to be complex CSS layout tasks into manageable, logical steps.

Basic Concepts

Flex Containers and Items: At the heart of Flexbox is the distinction between containers and items.

.container {
    display: flex;
}

Enter fullscreen mode Exit fullscreen mode

This simple declaration transforms .container into a flex container. Everything inside .container becomes a flex item, subject to Flexbox rules.

Key Flex Container Properties

Flex Direction : Flexbox can arrange items in different directions.

.container {
    display: flex;
    flex-direction: row; /* default, but can be column, row-reverse, or column-reverse */
}

Enter fullscreen mode Exit fullscreen mode

Justify Content : This property controls alignment along the main axis.

.container {
    justify-content: center; /* or flex-start, flex-end, space-around, space-between */
}

Enter fullscreen mode Exit fullscreen mode

Align Items : For alignment perpendicular to the main axis.

.container {
    align-items: center; /* or flex-start, flex-end, stretch (default), baseline */
}

Enter fullscreen mode Exit fullscreen mode

Flex Wrap : To manage overflow.

.container {
    flex-wrap: wrap; /* or nowrap */
}

Enter fullscreen mode Exit fullscreen mode

Basic Flex Item Properties

Flex Grow and Shrink : These properties govern how items share space.

.item {
    flex-grow: 1; /* items can grow to fill available space */
    flex-shrink: 1; /* items can shrink if necessary */
}

Enter fullscreen mode Exit fullscreen mode

Flex Basis : Sets the initial main size.

.item {
    flex-basis: 100px; /* initial main size of the item */
}

Enter fullscreen mode Exit fullscreen mode

Practical Examples

Simple Navigation Bar

Here's a practical example of how Flexbox can simplify layout:

<nav class="navbar">
    <a href="#">Home</a>
    <a href="#">About</a>
    <a href="#">Contact</a>
</nav>

Enter fullscreen mode Exit fullscreen mode
.navbar {
    display: flex;
    justify-content: space-around;
    background-color: #333;
}

.navbar a {
    color: white;
    padding: 14px 20px;
    text-decoration: none;
    text-align: center;
}

.navbar a:hover {
    background-color: #ddd;
    color: black;
}

Enter fullscreen mode Exit fullscreen mode

This creates a centered navigation bar where links are evenly spaced.

Card Layout

Flexbox excels at creating responsive card layouts:

<div class="card-container">
    <div class="card">Card 1</div>
    <div class="card">Card 2</div>
    <div class="card">Card 3</div>
</div>

Enter fullscreen mode Exit fullscreen mode
.card-container {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
}

.card {
    flex: 0 0 calc(33.333% - 10px); /* each card takes 1/3 of the width minus some margin */
    margin: 5px;
    border: 1px solid #ccc;
    padding: 10px;
    box-sizing: border-box;
}

Enter fullscreen mode Exit fullscreen mode

Best Practices

  • Semantic HTML: Use appropriate HTML tags for structure.

  • Avoid Overuse: Use Flexbox where it's necessary; simpler layouts might not need it.

  • Order Property: Use order for rearranging items without altering HTML.

When To Use It

  • Responsive Navigation: flex-basis and flex-grow can create mobile-first navigation that expands on larger screens.

  • Equal Height Columns: align-items: stretch naturally aligns columns of different content heights.

Troubleshooting Tips

  • Flex Items Not Displaying: Ensure display: flex; is applied to the parent.

  • Items Not Aligning as Expected: Check if flex-basis or flex-grow might be overriding alignment properties.

Conclusion

Flexbox simplifies layout, making responsive design intuitive. By understanding these basic concepts, you're well on your way to mastering layout in web development. Practice with your own projects, or try online sandboxes to further explore Flexbox's capabilities.

What's Next?

In this article, we've covered the basics of Flexbox, giving you a solid foundation to start experimenting with layouts. But there's more to explore! In our next article, we'll dive deeper into:

  • Advanced Flexbox Techniques: Learn about flex-flow, align-content, and more nuanced uses of flex, align-self.

  • Flexbox with Other Layout Techniques: How Flexbox plays nicely with CSS Grid for even more complex layouts.

  • Real-World Applications: Practical examples in navigation, forms, and responsive images.

  • Troubleshooting and Optimization: Tips on debugging common Flexbox issues and optimizing performance.

Stay tuned for our deep dive into Flexbox, where we'll take your layout skills to the next level!


πŸ‘‹ Hello, I'm Eleftheria, Community Manager, developer, public speaker, and content creator.

πŸ₯° If you liked this article, consider sharing it.

πŸ”— All links | X | LinkedIn

Top comments (0)