HTML Block and Inline Elements

Beginner
10 min read
Updated: Nov 2026
6 examples
display

Definition and Usage

Every HTML element has a default display that shapes how it sits in the page. Most elements behave as either block-level or inline. A third layout mode, inline-block, is mainly applied with CSS when you want a hybrid box.

What is a block-level element?

A block element starts on a new line and expands to the full available width of its container (unless you constrain it with CSS). Browsers typically add vertical spacing before and after. Typical block tags: <div>, <p>, <h1>–<h6>, <ul>, <ol>, <section>, <header>, <article>, and <form>.

What is an inline element?

An inline element stays in the current line of text. It only occupies as much width as its content needs and does not force a line break. Typical inline tags: <a>, <span>, <strong>, <em>, <img>, <code>, and <label>.

What about inline-block?

display: inline-block keeps the element in the text flow (like inline) while letting you set width, height, padding, and margin like a block box. It is not the default for most tags—you apply it with CSS when you need side-by-side boxes that still respect size.

Default vs CSS.

HTML picks a sensible default. CSS display can change how an element paints, but it does not rewrite nesting rules in the HTML content model.

Do not nest blocks inside paragraphs.

Putting <div>, <ul>, or another <p> inside a <p> is invalid. The browser closes the paragraph early and the layout can look broken.

div vs span.

Prefer a meaningful tag when one exists. Use <div> as a generic block wrapper and <span> as a generic inline wrapper when no better semantic element fits.

Block and Inline Syntax

Block elements stack; inline elements share a line:

layout.html
block → new line, full width (e.g. <div>, <p>)
inline → same line, content width (e.g. <span>, <a>)
inline-block → same line + box sizing via CSS
HTML
<!-- Block: each starts on its own line -->
<div>First block</div>
<p>Second block (paragraph)</p>
<h2>Third block (heading)</h2>

<!-- Inline: share one paragraph line -->
<p>
  Visit <a href="/html">HTML</a> and try
  <strong>bold</strong> or a
  <span style="color:teal">styled span</span>.
</p>

<!-- Inline-block via CSS -->
<style>
  .tile { display: inline-block; width: 120px; padding: 0.5rem; }
</style>
<span class="tile">Tile A</span>
<span class="tile">Tile B</span>

Prefer semantic tags (<section>, <nav>, <strong>) over bare wrappers when the meaning is clear. Reach for <div> / <span> when you only need a hook for layout or styling.

Core HTML Concepts

How the three display modes compare by default:

BehaviorBlockInlineInline-block
Starts on new line?YesNoNo
WidthFull available widthContent widthContent or set width
Set width / height?YesIgnored (mostly)Yes
Vertical margin / paddingFull effectLimited / unevenFull effect
Typical tagsdiv, p, h1–h6, ul, sectiona, span, strong, imgOften via CSS

Common block elements

ElementRole
<div>Generic block container for grouping content
<p>Paragraph of text
<h1>–<h6>Headings that introduce sections
<ul> / <ol>List containers (items are also block-level)
<section>Thematic grouping with its own heading

Common inline elements

ElementRole
<a>Hyperlink in the text flow
<span>Generic inline wrapper for a phrase
<strong> / <em>Important or emphasized text
<img>Image that sits in the line (replaced element)

Nesting rules that matter

PatternOK?
Block inside block (div > p)Yes
Inline inside block (p > span)Yes
Inline inside inline (a > strong)Often yes (follow each tag’s content model)
Block inside p (p > div / ul)No — invalid nesting
Block inside span or aNo for classic inline containers

How to Choose Block vs Inline

Practical habits when structuring markup.

  1. Ask whether the content should start a new section of the page (block) or sit inside a sentence (inline).
  2. Pick a semantic tag first—<p>, <section>, <a>, <strong>—before a generic wrapper.
  3. Use <div> to group related blocks; use <span> to mark a phrase inside text.
  4. Keep paragraphs free of nested blocks; move lists and cards outside the <p>.
  5. When you need side-by-side sized boxes, apply display: inline-block (or modern flex/grid) with CSS.
  6. Remember: changing display does not make invalid nesting valid.
GoalPattern
Group a card of content<div class="card">…</div> or <section>
Highlight a word<span class="hl">…</span> or <strong>
Link inside a sentence<p>…<a href="…">…</a>…</p>
List under a heading<h2>…</h2><ul>…</ul> (not inside a p)
Equal-width chips in a rowdisplay: inline-block or flex on a parent

Live Preview

Switch the same content between block, inline, and inline-block to see how layout changes.

Background colors show each box. Only the CSS display value changes.

Rendered page
Alpha Beta Gamma
display: block — each item on its own row, full width

HTML Examples

Six patterns: block elements, inline elements, side-by-side comparison, inline-block CSS, nesting pitfalls, and a complete page.

Example 1 — Common Block Elements

HTML
<div>This is a div (block)</div>
<p>This is a paragraph (block)</p>
<h2>This is a heading (block)</h2>
<ul>
  <li>List item one</li>
  <li>List item two</li>
</ul>
<section>
  <h3>Section heading</h3>
  <p>Section body text.</p>
</section>
Try It Yourself

How It Works

  1. Each block begins on a new line and uses the full row width.
  2. div is a neutral grouping box with no built-in meaning.
  3. p / headings / lists / section add structure and default spacing.
  4. Use for page regions, paragraphs, and stacked content.

Example 2 — Common Inline Elements

HTML
<p>
  Read the
  <a href="/html">HTML tutorial</a>,
  note the <strong>important</strong> tip,
  and wrap a phrase in a
  <span style="background:#fef3c7">highlighted span</span>.
</p>
<p>
  Inline image:
  <img src="/images/codetofun-logo.png" alt="CodeToFun" width="32" height="32">
  sits in the text line.
</p>
Try It Yourself

How It Works

  1. a / strong / span stay inside the paragraph without line breaks.
  2. img is a replaced inline element—it has intrinsic size but still flows with text.
  3. span adds styling without changing meaning.
  4. Use for links, emphasis, and phrase-level markup.

Example 3 — Block vs Inline Side by Side

HTML
<style>
  .box-b { background: #dbeafe; padding: 0.4rem; margin: 0.25rem 0; }
  .box-i { background: #bbf7d0; padding: 0.2rem 0.4rem; }
</style>

<h3>Block (div)</h3>
<div class="box-b">Block one</div>
<div class="box-b">Block two</div>

<h3>Inline (span)</h3>
<p>
  <span class="box-i">Inline one</span>
  <span class="box-i">Inline two</span>
</p>
Try It Yourself

How It Works

  1. divs stack because each is block-level.
  2. spans share one line inside the paragraph.
  3. Backgrounds make the occupied width obvious.
  4. Same idea applies to any default block vs inline tags.

Example 4 — Inline-Block with CSS

HTML
<style>
  .tile {
    display: inline-block;
    width: 100px;
    height: 60px;
    margin: 0.25rem;
    padding: 0.5rem;
    background: #fde68a;
    text-align: center;
    vertical-align: top;
  }
</style>
<span class="tile">A</span>
<span class="tile">B</span>
<span class="tile">C</span>
Try It Yourself

How It Works

  1. inline-block keeps items on one row like text.
  2. width / height work—unlike plain inline.
  3. Useful for chips, badges, and simple tile rows.
  4. Modern layouts often prefer flex or grid for the same job.

Example 5 — Nesting: Wrong vs Right

HTML
<!-- Wrong: block (ul) inside p — browser will fix-up the DOM -->
<!--
<p>Topics:
  <ul><li>HTML</li><li>CSS</li></ul>
</p>
-->

<!-- Right: keep the list outside the paragraph -->
<p>Topics:</p>
<ul>
  <li>HTML</li>
  <li>CSS</li>
</ul>

<!-- Right: only phrasing / inline content inside p -->
<p>Learn <a href="/html">HTML</a> and <strong>practice</strong>.</p>
Try It Yourself

How It Works

  1. p may contain phrasing content (text, links, spans)—not blocks.
  2. Invalid nest causes the browser to close the p early.
  3. Place lists after the paragraph, not inside it.
  4. Validate with DevTools Elements when layout looks “mysterious.”

Example 6 — Complete HTML Page

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Block and Inline Example</title>
  <style>
    .card { background: #f1f5f9; padding: 1rem; margin: 1rem 0; }
    .tag { background: #fde68a; padding: 0.15rem 0.4rem; }
  </style>
</head>
<body>
  <header>
    <h1>City Guide</h1>
  </header>
  <section class="card">
    <h2>London</h2>
    <p>
      London is the capital of England.
      Visit <a href="#">attractions</a> and look for the
      <span class="tag">River Thames</span>.
    </p>
  </section>
  <footer>
    <p>&copy; 2026 City Guide</p>
  </footer>
</body>
</html>
Try It Yourself

How It Works

  1. header / section / footer are block regions of the page.
  2. p holds the narrative; a and span stay inline.
  3. class on section styles a block card without changing display type.
  4. Mix semantics with light CSS for polish.

Edge Cases & Pitfalls

Most “mystery” layout bugs come from invalid nesting or confusing display with content models.

p nest

Block inside a paragraph

A div, ul, or nested p inside p is invalid. The parser closes the paragraph first.

display

CSS display ≠ nesting license

Making a span display: block changes layout only. It still should not wrap block structure like a section.

width

Width on pure inline

Setting width/height on a default inline element usually has no effect. Use block, inline-block, flex, or grid.

img

Images are special

img is inline by default but has intrinsic dimensions (replaced element). Vertical alignment and whitespace can surprise you.

a

Links wrapping blocks

HTML5 allows an a to wrap flow content in some cases, but keep structures simple—avoid nesting interactive elements inside links.

whitespace

Gaps between inline-blocks

Newlines between inline-block tags can create small gaps. Flexbox/grid avoid that quirk more cleanly.

Browser Support

Default block and inline layout for HTML elements is part of the web platform and works in all browsers.

✓ Baseline · Widely available

Block & inline defaults

Every modern browser applies default display values and respects CSS display overrides.

100% display defaults
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
Block / inline layout Universal

Bottom line: Use semantic HTML first; adjust display with CSS when you need a different layout mode.

Key Takeaways

  • Block starts a new line and fills available width; inline stays in the text flow.
  • div / p / headings / lists / section are common blocks; a / span / strong / img are common inline tags.
  • inline-block (via CSS) mixes line flow with box sizing.
  • Never put block elements inside a <p> incorrectly—nesting rules still apply after CSS changes.

One line: Choose block tags for stacked page structure and inline tags for text-level markup—use CSS display only when you need a different box model.

Frequently Asked Questions

A block-level element starts on a new line and stretches to the full width of its parent by default. Common examples are div, p, h1–h6, ul, ol, and section.
An inline element stays in the text flow. It does not force a new line and only takes the width of its content. Common examples are a, span, strong, em, and img.
Block stacks vertically and fills the container width. Inline flows with text and ignores width/height. Inline-block flows like text but accepts width, height, padding, and margin like a box.
No. Do not nest block elements such as div, ul, or another p inside a p. The browser will close the paragraph early and produce unexpected markup. Put inline content (or phrasing content) inside p.
Use div as a generic block container for groups of content. Use span as a generic inline wrapper for a word or phrase you want to style or script without breaking the text line.
No. Setting display: block on a span only changes layout. Nesting rules still follow the HTML content model—an inline element is not allowed to contain block-level structure just because you restyled it.

Practice in the editor

Open a Try It demo and switch between block and inline markup.

Open Try It editor →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful