designwithfocus

CSS

How to Use CSS Display Flex on a Real Page

By Design With Focus Team 7 min read
A row of three boxes inside a flex container with direction and wrap labels

Short answer

Set display: flex on the parent. Its direct children become flex items and line up in a row, because the initial flex-direction is row and flex-wrap is nowrap. MDN calls flexbox a one-dimensional layout: one axis at a time, either a row or a column. Grid is the two-dimensional alternative. To change the used value from script, assign element.style.display. That writes an inline style.

Last verified against the sources listed below.

Key takeaways

  • display: flex on a parent turns its direct children into flex items.
  • The initial direction is row, and the initial wrap is nowrap.
  • flex-wrap: wrap moves extra items to another line instead of only shrinking them.
  • display: grid is a separate layout. It controls rows and columns together.
  • element.style.display sets the inline display value from JavaScript.

How to use CSS display flex starts with the parent, not the children. Set display to flex on the container. The direct children become flex items.

MDN’s flexbox guide calls this a one-dimensional layout model for distributing space and aligning items. One-dimensional means one axis at a time, a row or a column. CSS Grid is the contrast: it controls columns and rows together.

You need a parent element, at least two children, and a stylesheet the page actually loads. A browser is enough. No library is required.

The parent is still an HTML element. The Living Standard groups most body elements, including nav and div, as flow content. display: flex does not move them into a different HTML category.

On a small screen, the viewport meta tag matters. Google’s meta tag page says that tag tells the browser how to render the page on a mobile device, and that its presence is what Google reads as a sign the page is mobile friendly. A nowrap row can still overflow if the items cannot shrink.

Those children are nodes the HTML parser places in a document.

The DOM standard describes that tree. display then chooses the box, which is a CSS step after the tree exists.

The CSS box model guide is the sizing context under this layout. Flex does not replace width, padding, or margin.

How do you set display flex?

Declare display: flex on the parent. MDN’s display page says the property sets the outer and inner display types. The outer type is the element’s role in flow layout.

The inner type is the layout of its children. flex is the inner type that creates a flex container. inline-flex is the inline version.

Do this on a real navigation:

  1. Mark up a nav whose direct children are the links or buttons.
  2. In CSS, select that nav and set display: flex.
  3. Leave flex-direction unset if you want the initial value, which is row.
  4. Set a gap or margins if the items need space between them. Gap is not required for flex to work.
  5. Open the page and confirm the children sit on one line, starting at the start edge.
nav {
  display: flex;
}

MDN lists the initial behavior. Items display in a row. They start at the start edge of the main axis.

They do not grow (flex-grow is 0) but they can shrink (flex-shrink is 1). They stretch on the cross axis because align-items starts at stretch. flex-basis starts at auto.

flex-wrap starts at nowrap, so a row that is too wide overflows if the items cannot shrink enough.

Flexbox does not assume left-to-right English. In a horizontal writing mode, row starts on the left in English and on the right in Arabic. Think about start and end, not only left and right.

What CSS is for, beyond this one property, is the subject of the CSS guide.

How do you change direction and wrap?

Set flex-direction and flex-wrap on the same container. Direction picks the main axis. Wrap decides whether a second line is allowed.

flex-direction has four values on MDN’s list: row, row-reverse, column, and column-reverse. row and row-reverse run along the inline direction. column and column-reverse run in the block direction, top to bottom in a horizontal writing mode.

Reverse swaps the start and end edges.

flex-wrap: wrap lets items move to another line when they do not fit. nowrap, the initial value, keeps one line. MDN notes that items with the initial shrink factor will shrink to fit under nowrap.

If they cannot shrink small enough, they overflow. The shorthand flex-flow combines direction and wrap.

justify-content aligns items on the main axis. The initial value is flex-start. flex-end and center are the other values MDN highlights for that axis.

align-items aligns on the cross axis.

Lengths inside those items still use CSS units. A CSS pixel is 1/96 of a CSS inch. A flex item set to 96px is one CSS inch wide under that definition, not one device pixel on a high-resolution screen.

A flexbox playground lets you change direction, wrap, and alignment and copy the CSS.

How to use CSS display grid?

Set display: grid when the region needs rows and columns together. That is a different layout from flex, not a synonym.

MDN’s grid guide says a grid is intersecting horizontal and vertical lines. display: grid or display: inline-grid creates the container, and the direct children become grid items. In MDN’s first example, a grid with no track list is a single column.

You will not see a multi-column layout until you define tracks.

Define columns with grid-template-columns. MDN’s basic example uses 200px 200px 200px, three fixed tracks. The fr unit is a fraction of free space in the grid container.

Tracks can also use other length units, including percentages. Items can overlap, and z-index controls that layering. Placement can use line numbers, names, or areas.

Items without an explicit position follow the grid’s auto-placement.

Use flex inside a grid cell when that cell is a single row of icons and text. Use grid for the page frame when both axes matter at once. MDN says the two combine.

It does not say one replaces the other.

How to change display CSS with JavaScript?

Assign element.style.display. MDN’s HTMLElement.style page says the style property returns a live object for the element’s inline style. You can set a single CSS property on it.

The page’s examples use element.style.color and element.style.backgroundColor. display is a CSS property too, so the same pattern is element.style.display.

const nav = document.querySelector("nav");
nav.style.display = "flex";

The style object only reflects inline styles. A display: flex rule that lives in a stylesheet does not show up as element.style.display. That read returns an empty string until an inline value exists.

To switch a menu from row to column without editing the sheet, set element.style.flexDirection as well, using the camel-case form MDN documents for CSS properties.

Setting the property to "" or null clears that inline declaration. Assigning element.style itself, rather than one property, replaces the whole inline style, because that assignment is equivalent to setting cssText.

display: none is the value that removes the box. MDN’s display page says it also removes the element from the accessibility tree. Use it when the control should be gone, not when it should be invisible but still occupy space.

What fails in the layout?

Most failures are the initial values doing what the spec says, or a selector that never hits the parent.

Symptom Likely cause What to change
Children stay stacked display: flex is missing, or it is on the child Set it on the parent of the items
One long overflowing row flex-wrap is still nowrap Set flex-wrap: wrap
Items stretch taller than their text Initial align-items is stretch Set align-items to flex-start or center
Script seems to do nothing You read style.display and expected the stylesheet Set element.style.display, or read computed style
A hidden item is still announced It is only visually covered display: none removes it from the accessibility tree

Check the selector in devtools before you add more properties. A flex container with zero matching elements looks like ordinary blocks. The fix is the parent rule, then direction, then wrap.

Frequently asked questions

Does display: flex affect grandchildren?

No. MDN says that when you set display to flex, the direct children of that container become flex items. A grandchild stays in normal flow inside its parent unless that parent is also a flex container. If a menu item contains an icon and a label and you want those two side by side, set display: flex on the item, not only on the menu. Nested flex containers are normal. Each one has its own main axis.

Why do items overflow the row?

The initial flex-wrap is nowrap, so items stay on one line. They can shrink, because the initial flex-shrink is 1 and flex-grow is 0. If they cannot shrink enough, MDN says they overflow the container. Set flex-wrap to wrap when a new line is acceptable. justify-content then aligns items on the main axis. Its initial value is flex-start. align-items aligns on the cross axis, and its initial value is stretch.

When should the page use grid instead?

Use grid when the layout needs rows and columns at the same time. MDN calls grid a two-dimensional system and flexbox a one-dimensional model. display: grid or display: inline-grid makes direct children into grid items. Without grid-template-columns, MDN's example stays a single column. Add that property to define tracks, including the fr unit for a fraction of free space. Flex is still a good fit for a single row or column inside one of those tracks.

Will element.style.display replace the stylesheet?

It sets the inline style, which MDN says has the same priority in the cascade as a style attribute. A later stylesheet rule without !important does not override that inline declaration. Assigning the style property itself replaces the whole inline style, because that assignment matches cssText. Prefer setting one property, such as element.style.display, so other inline declarations stay. Set the property to an empty string or null when you want the stylesheet to take over again.

What does display: none do to a flex item?

MDN's display page says a value of none removes the element from the accessibility tree, so assistive technology does not announce it or its descendants. The how-browsers-work guide also says nodes with display: none are left out of the render tree, while visibility: hidden stays in the tree because it still takes space. Hiding a flex item with display: none removes it from the row. The other items reflow into the gap.

Sources