Display contents and hidden boxes
Definition and purpose
The DOM tree and generated box tree are distinct. For ordinary elements, display: contents omits the element’s own box but leaves descendants available for layout; display: none omits its entire subtree from the box tree.
Remove only a wrapper box
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style>#a { display: contents; color: blue; }</style> </head> <body> <div id="a"> <span id="child">Child</span> </div> <div id="b">Next</div> </body></html>The wrapper has no box, but its child inherits blue.
Syntax and mental model
visibility: hidden normally retains layout space while suppressing painting; descendants can override visibility. opacity: 0 makes a box transparent but does not itself remove interaction. With contents, inherited styles still follow the DOM. Replaced and unusual elements have special rules.
Compare hidden states
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style> #a { display: none; } #b { visibility: hidden; height: 40px; } </style> </head> <body> <div id="a"> <span id="child">Child</span> </div> <div id="b">Next</div> </body></html>B keeps its 40px height; A has no layout box.
Common mistakes
Do not use display: contents on important semantic wrappers without testing accessibility in your target browsers. Do not hide focusable controls using only opacity; transparent controls may still receive focus.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: Floats and clear. Next lesson: Static and relative positioning.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Display contents and hidden boxes — knowledge check
Five questions on display contents and hidden boxes.
What does display: contents omit for an ordinary wrapper?
Which normally retains layout space while hiding paint?
Does opacity: 0 automatically prevent focus?
Where does color inheritance follow?
What should be checked before using contents on semantic wrappers?