CSS Flexbox: A Beginner's Guide to Properties, Examples, and Layouts

flexbox
Web Development

Creating a website layout that looks good across different screen sizes can be challenging. Elements need to align, resize, and adjust to available space. CSS Flexbox makes this easier by providing a flexible way to arrange and distribute elements within a container.

If you're wondering what is Flexbox and how it helps with website layouts, it's a CSS layout model that allows developers to arrange elements in rows or columns. It is especially useful when creating navigation menus, card sections, and responsive website designs.

Whether you're learning CSS or planning a business website, understanding Flexbox can help you create better layouts. You can also explore our website development services to learn how professional websites are designed and developed around business goals.

In this guide, you'll learn what Flexbox is, how its properties work, and how to use practical examples to create responsive website layouts.

What Is CSS Flexbox?

CSS Flexbox, also known as the CSS Flexible Box Layout Module, is a one-dimensional layout system used to arrange elements in a row or column. It helps developers distribute available space, align items, and manage their sizes within a container.

Flexbox is useful for:

  • Aligning elements horizontally or vertically.
  • Distributing space between items.
  • Creating responsive navigation menus.
  • Building flexible card layouts.
  • Centering content within a container.

If you want to understand what is Flexbox in more technical detail, read MDN's guide to CSS Flexible Box Layout.

Flexbox Terminology: Container, Items, and Axes

Before exploring Flexbox properties, understand these three basic terms:

1. Flex container: The parent element that holds the items you want to arrange. You create one using display: flex or display: inline-flex.

2. Flex items: The direct children of a flex container. Flexbox properties control how these items are positioned and sized.

3. Main axis and cross axis: Flexbox uses two axes to control item placement.

  • Main axis: The primary direction in which flex items are arranged. Its direction depends on flex-direction.
  • Cross axis: The axis perpendicular to the main axis.

With flex-direction: row, the main axis usually follows the horizontal inline direction in left-to-right writing systems. With flex-direction: column, the directions are reversed. These axes depend on the writing mode, so they are not always equivalent to horizontal and vertical.

You can learn more about these terms in MDN's guide to basic concepts of Flexbox.

How to Create a Flexbox Layout

Creating a basic Flexbox layout requires only a few lines of CSS. Define a parent container and apply display: flex.

HTML code:

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

CSS code:

.container {
  display: flex;
  justify-content: space-between;
  gap: 20px;
}
.item {
  padding: 20px;
  background-color: lightblue;
}

In this example, display: flex turns the parent container into a flex container. Its three child elements automatically appear in a row.

The gap property adds space between the items.

This simple structure is the foundation of many Flexbox website examples, from navigation menus to content sections.

CSS Flexbox Properties

Flexbox properties are divided into two groups:

  • Container properties: Control the arrangement and alignment of all flex items.
  • Item properties: Control the size, position, and behavior of individual flex items.

1. Flex Container Properties

These properties are applied to the parent container.

Display

The display property activates Flexbox.

.container {
  display: flex;
}

Flex Direction

The flex-direction property determines the direction in which items are arranged.

Common values include:

  • row: Arranges items horizontally from left to right.
  • row-reverse: Arranges items horizontally in reverse order.
  • column: Arranges items vertically from top to bottom.
  • column-reverse: Arranges items vertically in reverse order.
.container {
  display: flex;
  flex-direction: column;
}

Flex Wrap

The flex-wrap property controls whether items stay on one line or move onto additional lines when there isn't enough space.

Its main values are:

  • nowrap: Keeps all items on a single flex line. Items can shrink to fit the available space, but they may still overflow if their minimum size prevents them from shrinking enough.
  • wrap: Allows items to move onto multiple lines.
  • wrap-reverse: Allows items to wrap onto multiple lines in reverse cross-axis order.
.container {
  display: flex;
  flex-wrap: wrap;
}

This property is particularly helpful when creating a responsive website using Flexbox, as it allows items to move onto new lines when the available screen space becomes limited.

For a more detailed explanation, see MDN's guide to wrapping Flexbox items.

Flex Flow

The flex-flow property is a shorthand for combining flex-direction and flex-wrap.

.container {
  display: flex;
  flex-flow: row wrap;
}

This arranges items in a row and allows them to wrap when needed.

Justify Content

The justify-content property controls how items are distributed along the main axis.

Common values include:

  • flex-start: Places items at the beginning.
  • flex-end: Places items at the end.
  • center: Center items.
  • space-between: Distributes items with equal space between them.
  • space-around: Adds space around each item.
  • space-evenly: Distributes equal space between and around items.
.container {
  display: flex;
  justify-content: space-between;
}

Align Items

The align-items property controls how items are aligned along the cross axis on each flex line.

Common values include stretch, flex-start, flex-end, center, and baseline.

.container {
  display: flex;
  align-items: center;
}

Align Content

The align-content property controls the spacing between multiple flex lines along the cross axis.

It works when items wrap onto more than one line and there is extra space available along the cross axis.

Common values include stretch, flex-start, flex-end, center, space-between, space-around, and space-evenly.

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

Gap

The gap property adds space between flex items without requiring margins on individual elements.

.container {
  display: flex;
  gap: 20px;
}

2. Flex Item Properties

These properties are applied to individual items inside a flex container.

Flex Grow

The flex-grow property determines how much an item can grow when extra space is available in the container.

.item {
  flex-grow: 1;
}

When multiple flex items have positive growth factors, they share the available extra space in proportion to their growth values, subject to their sizing constraints.

Flex Shrink

The flex-shrink property determines how much an item can shrink when there isn't enough space in the container.

.item {
  flex-shrink: 1;
}

A value of 1 allows the item to shrink. A value of 0 prevents it from shrinking because of the flex-shrink calculation.

Flex Basis

The flex-basis property sets an item's initial main-axis size before the remaining space is distributed.

.item {
  flex-basis: 200px;
}

In this example, the item starts with a main-axis size of 200 pixels. Its final size may change depending on other Flexbox settings.

Flex

The flex property is a shorthand for flex-grow, flex-shrink, and flex-basis.

.item {

  flex: 1;

}

This allows an item to grow and share available space with other flexible items. It is commonly interpreted as flex: 1 1 0%, where the values represent flex-grow, flex-shrink, and flex-basis, respectively.

Align Self

The align-self property lets you change the cross-axis alignment of one specific item without affecting the other items.

.item {
  align-self: center;
}

Order

The order property changes the visual order of flex items without changing their original HTML order.

.item:first-child {
  order: 2;
}

Use this property carefully because changing visual order does not change the DOM order or the keyboard navigation order.

Practical CSS Flexbox Examples

Now that you understand the main properties, let's see how Flexbox works in everyday website layouts. These Flexbox website examples show how you can use different properties to create practical layouts.

Example 1: Creating a Basic Horizontal Layout

.container {
  display: flex;
  gap: 15px;
}

This places the child elements in a horizontal row with 15 pixels of space between them.

Example 2: Creating a Responsive Navigation Bar

<nav class="navbar">
  <a href="/home">Home</a>
  <a href="/about">About</a>
  <a href="/services">Services</a>
</nav>
.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 15px;
}

This creates a navigation bar with evenly distributed links. The items can wrap onto another line when the available space becomes limited. Replace the example links with your actual website URLs.

Example 3: Creating Flexible Cards

.card-container {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
}
.card {
  flex: 1 1 250px;
  min-width: 0;
  padding: 20px;
  box-sizing: border-box;
}

This layout creates flexible cards that can grow, shrink, and wrap onto new lines depending on the available space. It is a practical example of how to create a responsive website using Flexbox.

Example 4: Centering Content

.container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 300px;
}

This centers an element horizontally and vertically within its flex container.

.footer-links {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 15px;
}

This helps arrange footer content neatly while allowing it to wrap when the screen becomes smaller.

CSS Flexbox vs. CSS Grid: What's the Difference?

Both Flexbox and CSS Grid help create website layouts, but they work differently.

FeatureCSS FlexboxCSS Grid
Layout directionOne-dimensionalTwo-dimensional
Main purposeArranging items in a row or columnArranging items in rows and columns
Best forNavigation bars, buttons, and card groupsPage layouts, dashboards, and complex sections
AlignmentAlong main and cross axesAlong rows and columns
Responsive layoutsSupports flexible sizing and wrappingSupports flexible rows and columns

Which one should you choose?

Use Flexbox when you need to arrange elements in one direction, such as a navigation menu or a row of cards. Choose CSS Grid when you need to control both rows and columns in a larger layout.

You can also use both together to create more flexible website designs. For a deeper comparison, explore MDN's guide to the relationship between Flexbox and other layout methods.

Common CSS Flexbox Mistakes to Avoid

Beginners sometimes face layout issues when using Flexbox. Here are a few common mistakes to watch out for:

  • Forgetting display: flex: Flexbox properties won't work as expected if the parent element isn't a flex container.
  • Confusing the axes: Remember that the main and cross axes change when you switch between row and column.
  • Using align-content on a single line: This property is useful for multiple flex lines, not for aligning items on one line.
  • Ignoring wrapping: Without flex-wrap: wrap, items may shrink or overflow instead of moving onto another line.
  • Overusing order: Changing visual order can create confusion for keyboard and screen reader users if the HTML order doesn't match the visual layout.

Conclusion

CSS Flexbox is a useful layout tool for developers who want to create clean, flexible, and responsive website designs. By understanding its container and item properties, you can control how elements are arranged, aligned, and sized.

Start with simple layouts, practice the properties using small examples, and gradually apply them to real website sections. Once you understand what is Flexbox and how its properties work, creating a responsive website using Flexbox becomes much easier.

If you're interested in learning more about website development, explore our web development roadmap to understand the skills and technologies involved in building websites.

Frequently Asked Questions

CSS Flexbox is used to arrange, align, and distribute elements inside a container. It is commonly used for navigation menus, card layouts, buttons, and responsive website sections.

Keep exploring

Discover more insights, practical guidance, and ideas from our latest articles.

Subscribe Newsletter

Stay Updated with
the Latest News!