On this page
Display & Inline-block
CSS Display Property and Inline-block: Complete Guide
Focus keyword: CSS display property. The display property is the most important layout switch in CSS. It decides how an element generates boxes, how it sits next to other elements, and how its children are laid out. In this lesson you will learn block, inline and inline-block, display none versus visibility hidden, flow-root, display contents, and the modern flex and grid values. Every code block is editable, so change values and watch the preview.
Trending keywords covered in this lesson
display block vs inline, inline-block vs inline, display none vs visibility hidden, display flow-root, display contents, inline-block whitespace gap, vertical-align, CSS box model, outer and inner display type, display flex, display grid, hidden attribute, accessibility tree, CSS reset, layout basics for beginners.
1. Outer and Inner Display Type
Every element has two display roles. The outer display type controls how the element behaves in the flow of its parent (block or inline). The inner display type controls how its children are laid out (flow, flex, grid). Modern CSS lets you write both values, for example display: block flex equals display: flex, and display: inline flex equals display: inline-flex. You will mostly use the single keyword form.
2. display: block
- Starts on a new line and stretches to fill the full available width of its parent.
- Respects
width,height,marginandpaddingon all four sides. - Default for
div,p,h1toh6,section,article,header,footer,ul,ol,li(list-item) andform. - Vertical margins of neighbouring blocks can collapse into one margin.
3. display: inline
- Flows inside a line of text and only takes the width of its content.
- Ignores
widthandheight. - Horizontal margin and padding work. Vertical padding is drawn but does not push other lines away, and vertical margin has no effect.
- Default for
span,a,strong,em,code,img(replaced inline element) andlabel.
4. display: inline-block
Inline-block is the hybrid. The element sits in a line like text, but inside it behaves like a block, so width, height, vertical margin and padding all work. It is perfect for badges, pills, small buttons, tags and image thumbnails inside text.
The whitespace gap problem
Inline-block elements are treated like words, so the space or line break between them in your HTML becomes a visible gap of about 4px. Fixes: use flexbox instead (best), set font-size: 0 on the parent and reset it on children, remove whitespace in HTML, or use negative margin. For real layouts prefer display: flex with gap.
vertical-align
Inline-block boxes align to the text baseline by default, which can create extra space under images. Use vertical-align: middle or top, or make the image display: block to remove the small gap under it.
5. display: none, visibility: hidden and opacity: 0
display: none: element is removed from layout and from the accessibility tree. No space is reserved. Child elements cannot be shown.visibility: hidden: element is invisible but still takes space. Hidden from screen readers and not focusable. Children can be made visible withvisibility: visible.opacity: 0: element is transparent but still takes space, still receives clicks and keyboard focus and is still read by screen readers. Use it only for animations.- The HTML
hiddenattribute appliesdisplay: noneby default. Be careful, your owndisplayrule can override it. - For screen-reader-only text use a visually hidden utility class (clip and 1px size), not display none.
6. display: flow-root
display: flow-root creates a new block formatting context. It makes a parent contain its floated children and stops margin collapse between parent and child. It is the modern, clean replacement for the clearfix hack.
7. display: contents
display: contents removes the element's own box but keeps its children, so they behave as if they were direct children of the grandparent. Useful for grid and flex layouts with wrapper elements. Do not use it on buttons, links or other interactive elements because some browsers used to drop their semantics.
8. display: flex, grid and inline variants
display: flex and display: grid create block-level containers. display: inline-flex and display: inline-grid create inline-level containers. Their children become flex items or grid items, so properties like float and vertical-align stop working on them. You will study flexbox and grid in detail in later lessons.
9. Other values you will see
list-item: block box with a marker (li).table,table-row,table-cell: make any element behave like table parts (use only for real tabular layouts or legacy work).ruby: for East Asian annotation text.
10. Common Mistakes
- Setting width and height on a
spanand wondering why nothing happens. - Using
opacity: 0to hide content that should be removed for screen readers. - Fighting inline-block gaps instead of switching to flexbox.
- Overriding the
hiddenattribute with a display rule. - Using
display: noneon content that must stay available to assistive technology.
Key Takeaways
Block fills the row, inline flows with text, inline-block gives you inline flow plus box control. Know the difference between display none, visibility hidden and opacity 0, use flow-root to contain floats, and prefer flex and grid for real layouts.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Style the three links so each one is an inline-block with padding 10px 20px, a 120px minimum width, centered text and rounded corners. Remove the whitespace gap between them without changing the HTML.
Press Run to execute.
Show expected output
Three buttons with padding, min-width 120px and no visible gap between themThis is a self-check — compare your result with the expected output above.
Make the second box invisible but keep its space in the layout. Then make a third version that removes both the box and its space.
Press Run to execute.
Show expected output
Box 2 uses visibility: hidden (space kept). Box 3 variant uses display: none (space removed)This is a self-check — compare your result with the expected output above.
Was this page helpful?