What is a block vs inline element?

Block-level elements start on a new line and stretch to fill the available width, and they accept width, height and vertical margins (<div>, <p>, <h1>, <section>, <ul>). Inline elements flow within a line of text, take only the width of their content, and ignore width, height and vertical margins (<span>, <a>, <strong>, <em>, <code>). The difference comes from the CSS display property.

Block (display: block)

  • Starts on a new line; following content moves below.
  • Width defaults to 100% of the container.
  • width, height, margin and padding all work as expected.
  • Examples: div, p, headings, ul, ol, li, section, article, header, footer, form, table, blockquote.

Inline (display: inline)

  • Sits next to other inline content and wraps across lines.
  • Width and height are ignored; size comes from content.
  • Horizontal margin and padding apply; vertical padding paints but does not push surrounding lines apart, and vertical margin is ignored.
  • Examples: span, a, strong, em, code, abbr, label.

In between

  • inline-block: flows inline but accepts width, height and vertical spacing (buttons, badges).
  • Replaced inline elements like <img>, <input>, <button> are inline by default but have intrinsic dimensions, so width and height work.

Modern CSS terms

CSS Display Level 3 splits display into an outer type (block or inline) and an inner type (flow, flex, grid): display: inline flex equals inline-flex.

HTML content model

HTML itself now talks about "flow" and "phrasing" content rather than block and inline, but the CSS behavior is what interviewers mean.

Code example

html
1<style>
2 body { font-family: system-ui; }
3 .block { background: #e0e7ff; margin: 8px 0; padding: 8px; }
4 .inline { background: #fde68a; padding: 8px; margin: 20px; width: 300px; height: 100px; } /* width/height ignored */
5 .inline-block { display: inline-block; background: #bbf7d0; width: 120px; height: 40px; margin: 8px; vertical-align: middle; }
6</style>
7
8<div class="block">Block: full width, new line</div>
9<div class="block">Another block below</div>
10
11<p>
12 Text with <span class="inline">an inline span</span> that keeps flowing in the line,
13 ignoring width and height. Vertical padding overlaps neighboring lines.
14</p>
15
16<p>
17 Mixed: <span class="inline-block">inline-block</span> respects size
18 <span class="inline-block">and margins</span> but stays in the line.
19</p>

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the HTML & CSS set.

Open in study app