🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEcss

css Documentation

LOADING ENGINE...

Display: flex

AI & DATA SCIENCE // display-flex

Setting display: flex turns an element into a flex container, laying out its direct children as flex items along a single row or column with powerful alignment and distribution controls.

Syntax

display: flex;

Deep Dive Course

Once an element has display: flex, its direct children automatically become flex items, arranged by default in a single horizontal row, each sized based on its content unless told otherwise, with the parent's other flex properties, like flex-direction, justify-content, and align-items, controlling their exact direction, alignment, and spacing. This is fundamentally different from float-based layouts, since flex items participate in a purpose-built one-dimensional layout algorithm designed specifically for distributing space and aligning items along a single axis, rather than relying on the side effects of floating and clearing elements out of normal flow.

1Understanding Display: flex

Once an element has display: flex, its direct children automatically become flex items, arranged by default in a single horizontal row, each sized based on its content unless told otherwise, with the parent's other flex properties, like flex-direction, justify-content, and align-items, controlling their exact direction, alignment, and spacing. This is fundamentally different from float-based layouts, since flex items participate in a purpose-built one-dimensional layout algorithm designed specifically for distributing space and aligning items along a single axis, rather than relying on the side effects of floating and clearing elements out of normal flow.

💡

Setting display: flex only affects the flex container's direct children — grandchildren and deeper descendants are unaffected as flex items unless they themselves are also inside their own separate flex container.

editor.html
.navbar {
  display: flex;
  justify-content: space-between;
}
localhost:3000

2Practical Example

Here is a real-world application of Display: flex showing how it is used in production CSS code.

editor.html
.card {
  display: flex;
  align-items: center;
  gap: 12px;
}
localhost:3000

3Best Practices

Follow these guidelines when working with Display: flex:

1. Reach for display: flex as the default modern approach for one-dimensional layouts, rows or columns of items needing alignment or space distribution, rather than float-based techniques

2. Remember flex properties on a container only affect its direct children, not deeper-nested descendants, unless those descendants are themselves flex containers too

3. Combine display: flex with justify-content and align-items as the standard trio for controlling both horizontal and vertical alignment of items within the container

⚠️

Tip: Setting display: flex only affects the flex container's direct children — grandchildren and deeper descendants are unaffected as flex items unless they themselves are also inside their own separate flex container.

editor.html
.navbar {
  display: flex;
  justify-content: space-between;
}
localhost:3000

Examples

Example 01Basic Usage
.navbar {
  display: flex;
  justify-content: space-between;
}
Example 02Advanced Example
.card {
  display: flex;
  align-items: center;
  gap: 12px;
}

Best Practices

  • Reach for display: flex as the default modern approach for one-dimensional layouts, rows or columns of items needing alignment or space distribution, rather than float-based techniques
  • Remember flex properties on a container only affect its direct children, not deeper-nested descendants, unless those descendants are themselves flex containers too
  • Combine display: flex with justify-content and align-items as the standard trio for controlling both horizontal and vertical alignment of items within the container

Interview Question

Why does setting display: flex on a container only change the layout behavior of its direct children, and not its grandchildren nested further down?

Hint: Think about what exactly becomes a 'flex item' when a container becomes a flex container, and whether that designation cascades further down the tree automatically.

display: flex specifically establishes the element it's applied to as a flex container, and the flex layout algorithm specifically only applies its item-arranging behavior, direction, alignment, wrapping, and space distribution, to that container's immediate, direct children, which become flex items as a direct, one-level-deep consequence of their parent becoming a flex container. Grandchildren, nested one level further down inside one of those flex items, are simply normal children of whatever element contains them, that flex item, and are laid out according to that flex item's own display value and layout rules, completely independent of the original outer flex container's settings, unless that flex item itself has also separately been given its own display: flex to make its own children into a nested set of flex items. This one-level-deep scoping is a deliberate, core part of how flexbox is designed to work, letting each flex container independently control just its own direct children's arrangement, rather than some layout setting cascading indefinitely and unpredictably through every level of nesting.

Exercises

MediumPractice using Display: flex in a real scenario.
View Solution
.navbar {
  display: flex;
  justify-content: space-between;
}

Frequently Asked Questions

Why does setting display: flex on a container only change the layout behavior of its direct children, and not its grandchildren nested further down?

display: flex specifically establishes the element it's applied to as a flex container, and the flex layout algorithm specifically only applies its item-arranging behavior, direction, alignment, wrapping, and space distribution, to that container's immediate, direct children, which become flex items as a direct, one-level-deep consequence of their parent becoming a flex container. Grandchildren, nested one level further down inside one of those flex items, are simply normal children of whatever element contains them, that flex item, and are laid out according to that flex item's own display value and layout rules, completely independent of the original outer flex container's settings, unless that flex item itself has also separately been given its own display: flex to make its own children into a nested set of flex items. This one-level-deep scoping is a deliberate, core part of how flexbox is designed to work, letting each flex container independently control just its own direct children's arrangement, rather than some layout setting cascading indefinitely and unpredictably through every level of nesting.

Related Functions

Flex-directionJustify-contentAlign-items