The display property is the single most fundamental layout controller in CSS. It defines an element's inner display type (how its child elements are laid out) and its outer display type (how the element itself participates in the parent document flow).
| Value | Default Elements | Starts on New Line? | Respects width & height? | Respects Vertical Margin/Padding? |
|---|---|---|---|---|
| block | <div>, <p>, <h1>, <section> |
Yes (Forces line break; spans 100% parent width) | Yes | Yes |
| inline | <span>, <a>, <strong>, <em> |
No (Flows inline with surrounding text) | No (Ignored; fits content width) | No (Vertical padding overlaps adjacent lines; vertical margin ignored) |
| inline-block | <button>, <input>, <img> |
No (Flows side-by-side on the same line) | Yes! (Allows custom width and height) | Yes! (Respects all margins and padding) |
Interviewers and university exams frequently test the three distinct ways of hiding elements in CSS:
| Technique | Takes Up Space in Layout? | Can User Click It? | Can Be Animated with Transitions? | Screen Reader Accessible? |
|---|---|---|---|---|
display: none; |
No (Completely removed from render tree) | No | No (Instant toggle) | No (Completely ignored) |
visibility: hidden; |
Yes (Empty invisible gap remains) | No | Yes (Visibility toggles at 0% or 100%) | No |
opacity: 0; |
Yes (Empty transparent gap remains) | Yes! (User can still click invisible button!) | Yes! (Smooth fade in/out) | Yes (Screen reader still reads content) |
Question: Why does adding margin-top: 20px; to an <a> tag fail to push content down?
Answer: Because the <a> tag is an inline element by default. Inline elements ignore vertical margins and heights. To fix this, change its display mode: a { display: inline-block; margin-top: 20px; }.
Boost your CSS and front-end development proficiency by exploring these closely connected topics:
Understand margins, borders, padding, content and the universal box-sizing fix.
Master 1D alignment, justify-content, align-items, flex-wrap and responsive flow.
Build powerful two-dimensional grid layouts with template areas and fr units.
Eliminate styling conflicts and master selector weights from 0,0,0,1 to !important.