CS Engineering Gyan

CSS Flexbox

CSS Flexbox, short for Flexible Box Layout, is a modern layout system designed to arrange elements efficiently within a container. It simplifies the process of creating responsive and well-aligned webpage layouts without relying on older techniques such as floating elements or complex positioning.

Modern websites must adapt to different screen sizes, including desktops, tablets, laptops, and smartphones. Flexbox provides a flexible way to distribute available space, align items, and organize webpage components automatically, making responsive design much easier.

Whether you are building a navigation menu, pricing section, image gallery, dashboard, or card layout, Flexbox offers powerful alignment and spacing capabilities that reduce the amount of CSS required while improving readability and maintainability.

In this tutorial, you will learn the fundamental concepts of CSS Flexbox, understand how flex containers and flex items work, and explore practical examples that help you build professional and responsive web layouts.

What is CSS Flexbox?

CSS Flexbox is a one-dimensional layout model that arranges elements either horizontally or vertically. It allows developers to control the size, alignment, spacing, and order of items inside a container without writing complicated layout code.

Unlike traditional layout methods, Flexbox automatically adjusts the available space between elements. This makes it ideal for designing responsive interfaces where components need to resize or reposition based on the screen size.

A Flexbox layout always consists of two main parts:

Once an HTML element becomes a flex container, all of its direct child elements automatically become flex items and follow the Flexbox layout rules.

Why is CSS Flexbox Important?

Before Flexbox was introduced, developers often used floats, inline-block elements, and complicated positioning techniques to create webpage layouts. These methods required extra code and often produced alignment problems.

Flexbox solves these issues by providing built-in alignment, spacing, ordering, and sizing features. It reduces development time while making layouts easier to understand and maintain.

Benefits of CSS Flexbox

What is a Flex Container?

A Flex Container is the parent element that controls the layout of all its direct child elements. To create a Flex Container, the CSS property display: flex; is applied to the parent element.

After this property is applied, every direct child automatically becomes a Flex Item and follows the layout rules defined by the Flexbox model.

Syntax


.container{

display:flex;

}

Example


<div class="container">

<div>Item 1</div>

<div>Item 2</div>

<div>Item 3</div>

</div>

By default, all three items will be displayed in a horizontal row because the parent element is a Flex Container.

What are Flex Items?

Flex Items are the direct child elements inside a Flex Container. These elements automatically participate in the Flexbox layout and can be aligned, resized, reordered, or spaced using various Flexbox properties.

Each Flex Item can grow, shrink, or occupy available space depending on the layout rules defined for the container or the individual item.

Example


<div class="container">

<div>HTML</div>

<div>CSS</div>

<div>JavaScript</div>

</div>

Here, the three child elements become Flex Items because they are placed directly inside the Flex Container.

Main Axis and Cross Axis

Flexbox organizes items using two imaginary axes that determine the direction of alignment and spacing.

By default, the Main Axis runs from left to right, while the Cross Axis runs from top to bottom. These directions can change depending on the flex-direction property.

Diagram


Cross Axis

     ↑

     |

+-----------------------------+

| Item1 Item2 Item3 Item4 |

+-----------------------------+

------------→ Main Axis

Understanding these two axes is essential because most Flexbox properties work by controlling alignment along either the Main Axis or the Cross Axis.

The display: flex Property

The display: flex; property converts a normal HTML container into a Flex Container. This single property activates the Flexbox layout model and allows all direct child elements to be arranged using Flexbox rules.

Example


.container{

display:flex;

background:#f5f5f5;

padding:20px;

}

Once this property is applied, the browser places all direct child elements in a flexible horizontal layout unless another direction is specified.

First CSS Flexbox Example

The following example demonstrates how a simple Flexbox layout arranges multiple boxes in a single horizontal row.

HTML


<div class="container">

<div>HTML</div>

<div>CSS</div>

<div>JavaScript</div>

</div>

CSS


.container{

display:flex;

gap:15px;

}

.container div{

padding:20px;

background:#dbeeff;

border:1px solid #0077cc;

}

In this example, all three boxes are displayed in a horizontal row with equal spacing between them. This simple layout demonstrates the power of Flexbox, where only a few CSS properties are required to create an organized and responsive design.

flex-direction Property

The flex-direction property determines the direction in which Flex Items are arranged inside a Flex Container. By changing this property, developers can display items horizontally, vertically, or in reverse order without modifying the HTML structure.

The default value is row, which places all items from left to right. Other values allow greater flexibility when designing responsive layouts.

Syntax


.container{

display:flex;

flex-direction:value;

}

Possible Values

Value Description
row Items are displayed horizontally from left to right.
row-reverse Items are displayed horizontally from right to left.
column Items are displayed vertically from top to bottom.
column-reverse Items are displayed vertically from bottom to top.

Example


.container{

display:flex;

flex-direction:column;

}

In this example, all Flex Items will be arranged vertically instead of horizontally.

flex-wrap Property

By default, Flexbox tries to fit all Flex Items into a single row. When there are many items, they may become too small or overflow outside the container. The flex-wrap property allows items to move onto additional rows or columns whenever necessary.

Syntax


.container{

display:flex;

flex-wrap:wrap;

}

Values

Value Description
nowrap Default value. Items remain in one line.
wrap Items move to the next line if required.
wrap-reverse Items wrap in the reverse direction.

Example


.container{

display:flex;

flex-wrap:wrap;

gap:15px;

}

Using wrap creates responsive layouts that automatically adjust to different screen sizes.

flex-flow Property

The flex-flow property is a shorthand that combines the flex-direction and flex-wrap properties into a single declaration. It makes CSS shorter and easier to read.

Syntax


.container{

flex-flow:row wrap;

}

Example


.container{

display:flex;

flex-flow:column wrap;

}

This example displays items vertically while allowing them to wrap whenever additional space is needed.

justify-content Property

The justify-content property aligns Flex Items along the Main Axis. It controls how extra horizontal or vertical space is distributed between items.

Common Values

Value Description
flex-start Items begin from the start.
flex-end Items move to the end.
center Items appear in the center.
space-between Equal space between items.
space-around Equal space around every item.
space-evenly Equal spacing throughout the container.

Example


.container{

display:flex;

justify-content:space-between;

}

This property is widely used in navigation menus, headers, footers, and responsive layouts.

align-items Property

The align-items property aligns Flex Items along the Cross Axis. It determines how items are positioned vertically when the default row direction is used.

Values

Value Description
stretch Default value. Items stretch to fill the container.
flex-start Items align at the beginning.
flex-end Items align at the end.
center Items align in the middle.
baseline Items align according to text baselines.

Example


.container{

display:flex;

align-items:center;

height:250px;

}

This property is useful for vertically centering content inside cards, banners, and sections.

align-content Property

The align-content property controls the spacing between multiple rows of Flex Items. It works only when items wrap onto more than one line using the flex-wrap property.

Example


.container{

display:flex;

flex-wrap:wrap;

align-content:space-around;

height:350px;

}

This property helps distribute multiple rows evenly inside larger containers.

gap Property

The gap property creates consistent spacing between Flex Items without using individual margins. It produces cleaner code and improves layout consistency.

Syntax


.container{

display:flex;

gap:20px;

}

Example


.container{

display:flex;

gap:25px;

}

The gap property is commonly used in card layouts, image galleries, navigation menus, and responsive grids.

Practical Flexbox Example

The following example combines several Flexbox properties to create a responsive layout.

HTML


<div class="container">

<div>HTML</div>

<div>CSS</div>

<div>JavaScript</div>

<div>Bootstrap</div>

</div>

CSS


.container{

display:flex;

flex-wrap:wrap;

justify-content:center;

align-items:center;

gap:20px;

padding:20px;

}

.container div{

padding:20px;

background:#e8f4ff;

border:1px solid #0077cc;

width:180px;

text-align:center;

}

This layout automatically adjusts the position of the boxes based on the available screen width. The items remain evenly spaced, properly aligned, and responsive without requiring complex CSS code.

order Property

The order property allows developers to change the visual position of Flex Items without modifying the HTML structure. By default, every Flex Item has an order value of 0. Items with smaller values appear before items with larger values.

This property is especially useful in responsive web design where the visual arrangement of elements needs to change for different screen sizes while keeping the HTML clean and accessible.

Syntax


.item{

order:1;

}

Example


.container{

display:flex;

}

.item1{

order:3;

}

.item2{

order:1;

}

.item3{

order:2;

}

The display order becomes Item2 → Item3 → Item1 even though the HTML order remains unchanged.

flex-grow Property

The flex-grow property determines how much a Flex Item should expand when extra space is available inside the Flex Container. Items with higher values occupy more available space than items with lower values.

Syntax


.item{

flex-grow:1;

}

Example


.container{

display:flex;

}

.item1{

flex-grow:1;

}

.item2{

flex-grow:2;

}

In this example, the second item occupies approximately twice as much available space as the first item.

flex-shrink Property

The flex-shrink property specifies how much a Flex Item should reduce in size when there is insufficient space inside the Flex Container.

The default value is 1, meaning every item can shrink proportionally if necessary.

Syntax


.item{

flex-shrink:1;

}

Example


.item1{

flex-shrink:0;

}

.item2{

flex-shrink:1;

}

Here, the first item keeps its original width while the second item becomes smaller when space is limited.

flex-basis Property

The flex-basis property specifies the initial size of a Flex Item before any remaining space is distributed. It provides a flexible alternative to using the width property.

Syntax


.item{

flex-basis:250px;

}

Example


.container{

display:flex;

}

.item{

flex-basis:200px;

}

Each Flex Item begins with a width of 200 pixels before Flexbox calculates additional space distribution.

flex Property

The flex property is a shorthand that combines flex-grow, flex-shrink, and flex-basis into a single declaration. It simplifies CSS and makes layouts easier to manage.

Syntax


.item{

flex:1 1 200px;

}

The shorthand values represent:

align-self Property

The align-self property overrides the align-items value for an individual Flex Item. It allows one specific item to have different alignment from the rest of the items in the container.

Syntax


.item{

align-self:center;

}

Example


.container{

display:flex;

height:250px;

align-items:flex-start;

}

.item2{

align-self:flex-end;

}

Only the second item moves to the bottom of the container while the remaining items stay at the top.

Responsive Design Using Flexbox

One of the biggest advantages of Flexbox is its ability to create responsive layouts with very little CSS. By combining Flexbox properties with media queries, developers can easily adjust layouts for different screen sizes.

Example


.container{

display:flex;

gap:20px;

}

@media(max-width:768px){

.container{

flex-direction:column;

}

}

In this example, items appear in a horizontal row on larger screens and automatically stack vertically on smaller devices, providing a better user experience.

Best Practices for CSS Flexbox

Common Mistakes While Using Flexbox

Advantages of CSS Flexbox

Frequently Asked Interview Questions

1. What is CSS Flexbox?

CSS Flexbox is a one-dimensional layout system used to align, distribute, and organize elements efficiently inside a container.

2. What is the difference between a Flex Container and a Flex Item?

The parent element using display:flex is called the Flex Container, while its direct child elements are called Flex Items.

3. What is the default value of flex-direction?

The default value is row.

4. Which property controls spacing along the Main Axis?

The justify-content property controls spacing and alignment along the Main Axis.

5. Which property changes the display order of Flex Items?

The order property changes the visual order of Flex Items without modifying the HTML source.

Conclusion

CSS Flexbox is one of the most powerful layout systems available in modern web development. It provides an efficient way to arrange, align, and distribute elements while reducing the complexity of traditional CSS layout techniques. Features such as flexible sizing, automatic spacing, and responsive alignment make Flexbox an essential tool for creating modern websites.

After mastering Flexbox, developers can build responsive navigation bars, galleries, forms, dashboards, and card-based layouts with significantly less code. A strong understanding of Flexbox also prepares you for learning advanced layout systems such as CSS Grid and responsive web design.

← Previous: CSS Box Model Next: CSS Grid →
Home Visit Our YouTube Channel