Full answer
display: contents removes the box of an element from the layout, but keeps its children. The children are laid out as if they were direct children of the element's parent. The element itself has no background, border, padding or size, because it has no box. Its inherited properties, such as colour and font, still reach the children.
Main use: let wrapper elements take part in a Grid or Flexbox layout of an ancestor. For example, markup groups three cards inside a div for the code structure, but the design needs all cards in one grid. With display: contents on the wrapper, the cards become grid items of the grid parent. Other uses are a form or fieldset wrapper around items in a layout, and removing a helper wrapper in a component whose markup you cannot change.
Risks:
- Accessibility: browsers have had bugs where elements with
display: contentslost their role or were removed from the accessibility tree, includingbutton,tableandlistelements. Fixes arrived in many browsers, but support differs across browsers and screen readers. Do not use it on interactive or semantic elements without testing with the tools your users have. - The element with no box cannot be used for spacing, a background or a border, and
positionandtransformon it have no effect. - It is easy to forget which element is the grid item. Use the browser tools to check.
Alternatives: subgrid when the wrapper has its own box but its children should follow the parent's tracks, or changing the markup, or a wrapper-free layout with CSS Grid placement.
In the first grid, the three items sit in three columns, one next to the other, because the wrapper has no box. In the second grid, the wrapper is a normal block, so it is one grid item in the first column and the items are stacked inside it.
Use it for structure that has no meaning, and test semantic elements before you rely on it.
<style>
.grid { display: grid; grid-template-columns: repeat(3, 100px); }
.group { display: contents; }
.plain { display: block; }
.grid div.item { background: #cde; }
</style>
<div class="grid" id="g1">
<div class="group" id="grp"><div class="item" id="i1">1</div><div class="item" id="i2">2</div><div class="item" id="i3">3</div></div>
</div>
<div class="grid" id="g2">
<div class="plain" id="grp2"><div class="item" id="j1">1</div><div class="item" id="j2">2</div><div class="item" id="j3">3</div></div>
</div>Checked in Chrome 156 only, by reading the item positions. Screen readers were not run, so the accessibility notes come from the known support differences, not from this test. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- The element has no box, but its children stay.
- Children join the grandparent's grid or flex layout.
- Background, border, padding and size are ignored.
- Accessibility support has been uneven for semantic elements.
- Consider subgrid or changing the markup.
Common mistakes
- Using display: contents on buttons, lists or tables and not testing assistive technology.
- Expecting a background or padding on the element.
- Using it when subgrid would keep the box and the alignment.
Follow-ups
- What is the difference between display: contents and subgrid?
- Why does a background not work on it?
Tips
- Explain it as removing the box but keeping the children.
- State the accessibility risk with semantic elements.