By WebNest Studio Engineering Team
CSS Tutorial
The CSS display Property
The display property determines the fundamental layout behavior of an element — whether it starts on a new line, whether it accepts a width and height, and how it interacts with the elements around it. It is one of the most frequently used and most consequential properties in CSS, because changing an element's display value can completely change how a layout behaves.
The most common values are block, inline, inline-block, and none, and understanding the practical difference between them clears up a lot of early CSS confusion, such as why a width declaration seems to have no effect on some elements.
Why it matters
The display property decides how an element takes part in layout: whether it starts a new line, whether it accepts a width, and whether it is shown at all. Width and margin that seem to be ignored are usually a sign that the element is inline. It is the property underneath every layout technique that follows.
block
Block-level elements (like <div>, <p>, and <h1> by default) always start on a new line and expand to fill the full width of their container unless a width is specified. They accept width, height, margin, and padding on all sides normally. Stacking block elements vertically is the natural, default flow of an HTML document.
inline
Inline elements (like <span>, <a>, and <strong> by default) flow within the surrounding text and do not start on a new line. Critically, they ignore width and height entirely, and vertical margin/padding is not respected for layout purposes (though it may render visually, it won't push surrounding content). Only horizontal padding and margin reliably affect layout on inline elements.
inline-block and none
inline-block combines both worlds: the element flows inline with surrounding content like an inline element, but it accepts width, height, and full margin/padding like a block element — commonly used for button-like elements that need to sit next to text but also need a defined size. display: none removes the element entirely from the rendered layout — it takes up no space at all and isn't visible, as opposed to visibility: hidden, which hides an element but preserves its layout space.
Examples
block, inline, and inline-block behaving differently in the same context
<!--
<span class="block-span">Block</span>
<span class="inline-span">Inline</span>
<span class="inline-block-span">Inline-block</span>
-->
.block-span { display: block; width: 200px; background: #eee; }
.inline-span { display: inline; width: 200px; background: #ddd; } /* width is ignored */
.inline-block-span { display: inline-block; width: 200px; background: #ccc; }
The block span forces a line break and honors its 200px width; the inline span sits within text flow but ignores the width entirely; the inline-block span flows next to surrounding inline content while still honoring the 200px width.
Toggling visibility with display: none versus visibility: hidden
.hidden-none { display: none; } /* removed from layout entirely */
.hidden-invisible { visibility: hidden; } /* still occupies its space */
The element with display: none disappears completely and surrounding elements collapse into its space, while the visibility: hidden element becomes invisible but leaves an empty gap where it used to be.
Common Mistakes
- Setting width or height on a naturally inline element (like a <span> or <a>) and being confused when it has no effect, without first changing display to inline-block or block.
- Using display: none when the intent was only to visually hide an element while keeping its layout space, when visibility: hidden was the correct choice.
- Assuming vertical margin/padding on inline elements behaves the same as on block elements for layout purposes.
- Forgetting that display: none also removes an element from screen-reader and keyboard-navigation flow, which has accessibility implications beyond visual layout.
Exercise
Give the heading a yellow background and make it only as wide as its text while still accepting padding. Make the button stretch to the full width of its container. Hide the paragraph in a way that keeps its empty space on the page.
Expected result: a yellow box hugging the heading text, an empty gap where the paragraph was, and a full-width button.
/* The page contains: <main> with an <h1>, a <p> and a <button> */
h1 {
background: yellow;
padding: 10px;
/* TODO: shrink to the width of the text */
}
p {
/* TODO: invisible, but still taking up space */
}
button {
/* TODO: full width */
}
Show hints
inline-blockshrinks to its content like an inline element but accepts padding, width and height like a block.visibility: hiddenhides an element and keeps its space;display: noneremoves it from the layout.
Show solution
/* The page contains: <main> with an <h1>, a <p> and a <button> */
h1 {
background: yellow;
padding: 10px;
display: inline-block;
}
p {
visibility: hidden;
}
button {
display: block;
width: 100%;
}
Quick quiz
1. Which statement about an inline element, such as span, is true?
- A. It always starts on a new line
- B. It fills the full width available
- C. It cannot contain text
- D. width and height have no effect on it
Show answer
D. An inline element is sized by its content and flows within a line of text.
2. What is the difference between display: none and visibility: hidden?
- A. There is none
- B. display: none removes the element from the layout; visibility: hidden hides it but keeps its space
- C. visibility: hidden removes the element from the layout
- D. display: none only hides the text
Show answer
B. Both also hide the element from screen readers.
3. Which is a block-level element by default?
- A. span
- B. div
- C. a
- D. img
Show answer
B. A div starts on a new line and takes the full width available.
Interview questions
What is the difference between block, inline and inline-block?
Show answer
A block element starts on a new line, takes the full width available, and respects width, height and all margins. An inline element flows within a line of text, is as wide as its content, and ignores width, height and vertical margins. An inline-block element flows inline like text but is treated as a block inside, so it accepts width, height, padding and margins on every side.
What are the ways to hide an element, and how do they differ?
Show answer
display: none removes the element from the layout and from the accessibility tree. visibility: hidden hides it and removes it from the accessibility tree but keeps its space. opacity: 0 makes it transparent while it still occupies space, can be clicked and is read by screen readers. To hide something visually while keeping it available to screen readers, a "visually hidden" utility class that clips the element to one pixel is used.
Key Points to Remember
- block elements start on a new line and accept full width/height and margin/padding; inline elements flow with text and ignore width/height.
- inline-block merges both: it flows inline but honors width, height, and margin/padding like a block element.
- display: none removes an element from the rendered layout entirely, unlike visibility: hidden, which preserves its space.
- Choosing the right display value is often the fix for layouts that "don't respond" to size or spacing properties.
Practice in the playground
Write and run your own code for this topic, or start from the exercise or an example. Shows a live preview of your HTML, CSS and JavaScript. Your code for each topic is kept in this browser.
Run the examples
Change an input, predict the result, then compare it with the output. Explain why the result changes.
Written by
WebNest Studio Engineering Team
Practical lessons from developers working with Java, Python, React, databases and production systems.
Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering