Skip to content
Tutorials Center
My learning

Topics

Lesson 43 of 122 ยท Page Structure and Layout

Block and Inline Elements

Learn the difference between block and inline elements, how they flow on the page, which elements can nest inside which, and when inline-block is the right choice.

Put a heading, a paragraph and a link on a page, and you'll notice they behave differently. The heading and paragraph each start on a new line and stretch across the page. The link sits inside a line of text, only as wide as its words. This is the difference between block and inline elements, and it shapes every page you build.

Understanding it explains a lot of everyday puzzles: why width does nothing on a <span>, why your margin doesn't push a link down, and why the browser sometimes "moves" your elements around. This lesson covers how both kinds flow, the rules for nesting them and the useful in-between called inline-block.

Two ways to flow

Browsers lay out a page in a normal flow, top to bottom and (in English) left to right. Elements take part in that flow in one of two main ways:

  • Block elements start on a new line, stack vertically and stretch to fill the full width available. Think of them as paragraphs in a book: each one is a box of its own.
  • Inline elements sit inside a line of text, flow along with the words and wrap onto the next line when needed. Think of them as words highlighted with a marker pen.

The outlines in this example make the boxes visible:

Block vs inline boxes

The blue boxes (block) each run the full width of the page. The red boxes (inline) wrap tightly around their words and stay in the line. Make the preview narrower and watch the inline boxes break across lines with the text.

Common block and inline elements

Every element has a default display that the browser's built-in stylesheet gives it.

Usually block Usually inline
<p>, <h1>โ€“<h6> <a>
<div> <span>
<ul>, <ol>, <li>, <dl> <strong>, <em>, <b>, <i>
<blockquote>, <pre>, <hr> <code>, <kbd>, <abbr>, <time>
<header>, <main>, <footer>, <section>, <article>, <nav> <img>, <br>, <sub>, <sup>
<form>, <fieldset>, <figure>, <table> <input>, <button>, <select>, <label>

A few on the right are special. <img>, <input> and <button> sit inline in the text, but they have their own size, so width and height work on them. CSS calls these replaced or inline-block-like elements.

"Block" and "inline" are CSS defaults, not fixed facts. The HTML specification groups elements by content model instead: what they mean and what they may contain. The look comes from CSS, and you can change it with the display property, as you'll see below. What you can't change with CSS are the nesting rules.

What changes between block and inline

Behavior Block Inline
Starts on a new line Yes No
Default width Full width of its container Just wide enough for its content
width and height Work Ignored
Vertical margin Pushes other elements away Ignored
Vertical padding Adds space and pushes content Painted, but doesn't move the lines above or below
Horizontal margin and padding Work Work

That table explains a classic frustration: you set margin-top: 20px on a link and nothing moves. The link is inline, so vertical margins don't apply. Try it:

Sizing block and inline elements

The paragraph becomes a 180-pixel box with space above and below. The span ignores the size and vertical margin completely, and its padding spills over the neighboring lines.

Changing the display with CSS

The CSS display property switches how an element takes part in the flow. The values you'll use most:

  • display: block turns an inline element into a block, for example to make a link fill a menu row.
  • display: inline puts block elements on one line, for example list items in a simple horizontal menu.
  • display: inline-block is the hybrid: the element sits in a line like inline content, but respects width, height and vertical margins like a block.
  • display: none removes the element from the page entirely; it takes up no space and isn't read by screen readers.

Later, you'll meet display: flex and display: grid, which arrange an element's children into rows, columns and grids. They're the foundation of Page Layout.

inline-block tags

The tags stay in the sentence but get proper padding and vertical spacing. Change inline-block to inline to see the difference, then to block.

display changes the look, not the meaning. A <span> with display: block is still a span to screen readers and search engines, and a heading with display: inline is still a heading. Choose the element for its meaning; use CSS for its appearance.

Nesting rules

The rules about what may go inside what come from HTML, not CSS. Most of them follow common sense:

  • Block containers can hold inline content: a paragraph full of links and emphasis is normal.
  • Most block containers can hold other blocks: a <div> or <section> can contain headings, paragraphs and lists.
  • Inline elements should contain only inline content: a <span> or <strong> holds text and other phrasing elements, never a heading or a list.

No blocks inside a paragraph

<p> is the important exception: even though it's a block, it may contain only phrasing (inline-level) content. You can't put a <div>, a list, a heading, a table or another <p> inside a paragraph.

This isn't just a style rule. The HTML parser enforces it. When it meets a block-level start tag inside an open <p>, it closes the paragraph first:

HTML
<!-- What you write -->
<p>Shopping list:
  <ul>
    <li>Lemons</li>
  </ul>
</p>

<!-- What the browser builds -->
<p>Shopping list:</p>
<ul>
  <li>Lemons</li>
</ul>
<p></p>

The list ends up outside the paragraph, and the stray </p> creates an extra empty paragraph. Any CSS aimed at p ul silently stops working. You can see this happen in the Elements panel of your browser's developer tools; the DevTools lesson shows how.

The fix is simple: end the paragraph before the list, or use a <div> as the wrapper.

The <a> element is transparent: it may contain whatever its parent may contain. So a link around a whole card, with a heading and a paragraph inside, is valid HTML, as long as there's no other interactive element (another link or a button) inside it:

HTML
<a href="/trails/cliff-path" class="card">
  <h3>Cliff path</h3>
  <p>7 km ยท moderate ยท sea views all the way</p>
</a>

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which statement about block elements is true?

    Block elements stack vertically and fill their container's width by default. Ignoring width and height is inline behavior.
  2. 2. You set margin-top: 30px on a <span> and nothing moves. Why?

    Inline elements ignore vertical margins. Use display: inline-block or block if you need them.
  3. 3. What does the browser do with <p>Intro <div>Box</div></p>?

    A paragraph can hold only phrasing content, so the parser closes the <p> when the <div> starts. The stray </p> then creates an extra empty paragraph.
  4. 4. Which display value keeps an element in the line of text but lets it have a width, height and vertical margins?

    inline-block flows like inline content while sizing like a block, which makes it ideal for buttons, badges and tags.

Summary

  • Block elements start on a new line and fill the available width; inline elements flow within a line of text.
  • Inline elements ignore width, height and vertical margins.
  • display changes how an element flows: block, inline, inline-block or none, but never its meaning.
  • Inline elements should contain only inline content.
  • <p> may contain only phrasing content; the parser closes it before any block element.
  • <a> is transparent and can wrap blocks, as long as there are no nested interactive elements.

Next lesson: Div and Span.