Sub topic 1
Creating a flex container with display: flex
A flex container is an element whose direct children are laid out by the Flexbox algorithm instead of normal block or inline flow. This chapter covers how to create one, what changes for its children, and the errors that most often confuse people on first use.
Prerequisites
This chapter assumes you already know the following:
- Basic HTML structure and elements, including nesting.
- CSS selectors and the cascade, so you can tell which rule wins.
- The CSS box model: content, padding, border, and margin.
- CSS length units such as
px,rem, and percentages.
You do not need any earlier Flexbox knowledge. Later chapters cover the axes, alignment, and sizing in detail.
The working model
Flexbox has two roles. The element that has display: flex is the flex container. Each of its direct children is a flex item. You turn the container on with one declaration, and the browser then lays out the items along a single line (the main axis) instead of stacking them as blocks.
<ul class="menu">
<li>Home</li>
<li>Docs</li>
<li>Blog</li>
<li>Contact</li>
</ul>.menu {
display: flex;
}Without the CSS rule, each <li> is a block-level list item and they stack vertically. With it, the four items sit side by side in one row, each as wide as its content.
Nothing else was needed. The defaults of a flex container are flex-direction: row, flex-wrap: nowrap, justify-content: flex-start, and align-items: stretch. Those defaults explain most of what you see in the first minute of using Flexbox.
What changes for the children
When a parent becomes a flex container, its direct children are blockified. This means each child computes to a block-level box type, even if it was an inline element such as a <span> or <a>. You no longer need display: block on them.
Several other rules change at the same time:
floatandclearhave no effect on flex items.vertical-alignhas no effect on flex items.- Margins of adjacent flex items do not collapse.
- Whitespace-only text between items is ignored and does not create gaps.
- Text directly inside the container, with no wrapping element, becomes an anonymous flex item.
- Absolutely positioned children are taken out of flex layout and do not take part in it.
The most common mistake follows from the direct-children rule. Only the immediate children are flex items. Grandchildren are laid out as usual.
<nav class="bar">
<div class="links">
<a href="/">Home</a>
<a href="/docs">Docs</a>
</div>
</nav>.bar {
display: flex;
}Here .links is the only flex item. The two links inside it remain inline elements in ordinary flow. To lay out the links with Flexbox, you must also set display: flex on .links. Nesting flex containers is normal and often necessary.
display: flex versus display: inline-flex
There are two values that create a flex container. They differ in how the container itself behaves among its siblings.
| Value | Container's outer behavior | Typical use |
|---|---|---|
| `display: flex` | Block-level. It takes the full available width and starts on a new line. | Page sections, navigation bars, card rows |
| `display: inline-flex` | Inline-level. It is only as wide as its content and sits in a line of text. | A button with an icon, a badge inside a sentence |
Both values lay out their children the same way. The difference is only how the container sits in its own parent.
.button {
display: inline-flex;
align-items: center;
gap: 0.5rem;
}This is a button-like element holding an icon and a label. It stays as narrow as its content and can sit in the middle of a paragraph.
A worked example
Start with three boxes in a wrapper.
<div class="row">
<div class="box">One</div>
<div class="box">Two with more text</div>
<div class="box">Three</div>
</div>.row {
display: flex;
border: 2px solid #444;
}
.box {
padding: 1rem;
background: #cde;
border: 1px solid #357;
}You can observe three things in the browser:
- The boxes are in one row, not stacked.
- Each box is as wide as its text plus padding, not as wide as the container. The container itself still fills the page width, so free space remains at the right.
- All three boxes have the same height. The tallest one decides the height, and the others stretch to match. This comes from the default
align-items: stretch.
Now add a long line to the middle box. The boxes stay in one row. They shrink to fit and the text wraps inside them. If they cannot shrink further, they overflow the container. The flex-wrap and flex-shrink chapters explain why.
Common errors
- Setting
display: flexon the wrong element. If nothing moves, check that the rule targets the parent of the items you want to arrange, not the items themselves. - Expecting grandchildren to be items. Apply
display: flexagain on the inner wrapper. - Expecting a column. The default direction is a row. Stacked layouts need
flex-direction: column, which a later chapter covers. - Using
floaton items to position them. It is ignored. Use the flex properties. - Using
display: flexwhereinline-flexis meant. A block-level container will push to its own line and fill the width. - Relying on a pseudo-element's absence.
::beforeand::afteron the container also become flex items. They can add an unexpected extra item, and they matter if you usejustify-content: space-between.
Verifying in the browser
Browser developer tools help here. In Chrome, Edge, and Firefox, an element with display: flex shows a flex badge in the Elements or Inspector panel. Clicking it overlays the container and its items. Use this to confirm which elements are items before you adjust any other property.
Exercise
A page has <header class="top"><div class="brand">Logo</div><div class="actions"><a href="/login">Log in</a><a href="/join">Join</a></div></header>. You write .top { display: flex; }. Which elements are now flex items, and what must you add to lay out the two links in a row with Flexbox?
Show answer
The flex items are .brand and .actions, because they are the direct children of .top. The two <a> elements are not flex items. To lay them out with Flexbox, add .actions { display: flex; }, which makes .actions a flex container for the links.
Exercise
You need a small tag element that holds an icon and a label, centered together, and that sits inside a line of running text without breaking onto its own line. Which display value do you use on the tag?
Show answer
Use display: inline-flex. It creates a flex container that is inline-level, so it stays in the text line and is only as wide as its content. display: flex would make it block-level and move it onto its own line.