How Can We Help?
< All Topics

Flexbox

CSS Flexbox (Flexible Box Layout) is a powerful layout system that allows you to create flexible and responsive page layouts. It provides a convenient way to distribute space among items in a container, align them, and control their order. Let’s explore CSS Flexbox and how you can use it to create flexible and dynamic layouts.

Container and Items

To use Flexbox, you need a container element that acts as a flex container and one or more child elements within it that become flex items. The container is defined using the display property with the value flex or inline-flex. Here’s an example:

.container {
  display: flex;
}

Main Axis and Cross Axis

Flexbox introduces two axes: the main axis and the cross axis. By default, the main axis runs horizontally, from left to right, and the cross axis runs vertically, from top to bottom. The flex-direction property can be used to change the direction of the main axis. Here’s an example:

.container {
  flex-direction: row; /* default: horizontal layout */
}

Justify Content

The justify-content property allows you to control the alignment and distribution of flex items along the main axis. It offers various values like flex-start, flex-end, center, space-between, space-around, and space-evenly. Here’s an example:

.container {
  justify-content: center; /* items are centered along the main axis */
}

Align Items

The align-items property controls the alignment of flex items along the cross axis. It offers values like flex-start, flex-end, center, baseline, and stretch. Here’s an example:

.container {
  align-items: center; /* items are centered along the cross axis */
}

Flexbox Ordering

Flexbox allows you to change the order of flex items using the order property. By default, items have an order of 0. You can assign positive or negative values to change their order. Here’s an example:

.item {
  order: 1; /* item appears after other items */
}

Flexbox Responsive Layouts

Flexbox is particularly useful for creating responsive layouts. By adjusting the flex properties, you can control how items grow, shrink, and wrap within the container. For example, the flex-grow property determines how flex items grow to fill available space. The flex-wrap property controls whether items should wrap to the next line when they exceed the container width. Here’s an example:

.container {
  flex-wrap: wrap; /* items wrap to the next line */
}

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

CSS properties related to Flexbox

Property Description
display Defines the element as a flex container.
flex-direction Specifies the direction of the main axis and the order of flex items.
flex-wrap Controls whether flex items should wrap to the next line when they exceed the container width.
justify-content Aligns flex items along the main axis and controls the distribution of space between them.
align-items Aligns flex items along the cross axis within the flex container.
align-content Aligns multiple lines of flex items along the cross axis when they wrap, and controls their spacing.
flex-grow Determines how flex items grow to fill available space.
flex-shrink Determines how flex items shrink when there is not enough space available.
flex-basis Specifies the initial size of flex items before they are flexed.
flex Shorthand property for flex-grow, flex-shrink, and flex-basis.
order Controls the order of flex items within the flex container.
align-self Allows individual flex items to override the align-items property.
flex-flow Shorthand property for flex-direction and flex-wrap.
gap or row-gap column-gap Sets the gap between flex items along the row and column axes.
justify-self Allows individual flex items to override the justify-content property.

These properties can be applied to the flex container (parent element) and flex items (child elements) to create flexible and responsive layouts using Flexbox. Use these properties in combination to control the layout, alignment, and sizing of flex items within the flex container.

Conclusion

CSS Flexbox provides a flexible and powerful layout system for creating responsive and dynamic page layouts. By using the flex container and flex items, along with properties like justify-content, align-items, order, and responsive flex properties, you can create sophisticated and adaptive layouts. Experiment with Flexbox and combine it with other CSS properties to achieve the desired layout and design for your web pages.

Table of Contents