HTML

HTML

HTML Block and Inline Elements

HTML block and inline elements shape web page layout. Block elements start on a new line and fill the container's full width. Inline elements stay within the text flow and use only the space needed. This tutorial covers both types and shows how they control spacing and alignment.

Learning Outcomes

After reading this tutorial, you will be able to:

  • Identify block-level elements and inline elements.
  • Explain the differences in layout behavior between block and inline elements.
  • Create clear page sections using block-level elements.
  • Insert inline elements within text without breaking flow.
  • Use inline-block properties to align items side-by-side.
  • Apply CSS styles to both block and inline elements.
  • Group content with <div> and <span> elements.
  • Understand HTML5 definitions for flow and phrasing content.

Block-Level Elements

Block-level elements begin on a new line and stretch across the full width of their container. They are used to group related content and create distinct sections on a page. Since they always start on a new line, they help structure the layout of a webpage. Additionally, block-level elements can contain both other block-level elements and inline elements, allowing for flexible and organized content grouping.

HTML Block-Level Elements

<address> <article> <aside> <blockquote>
<canvas> <dd> <div> <dl>
<dt> <fieldset> <figcaption> <figure>
<footer> <form> <h1>-<h6> <header>
<hr> <li> <main> <nav>
<noscript> <ol> <p> <pre>
<section> <table> <tfoot> <ul>
<video>

Example:

<div style="border: 1px solid #333; padding: 10px;">
  This is a div element.
</div>
<p style="border: 1px solid #333; padding: 10px;">
  This is a paragraph element.
</p>

Explanation: The <div> and <p> elements start on new lines. They fill the full width of their container. They stack vertically.

The <div> Element

The <div> element is a simple container. It groups content and helps apply CSS styling. It has no inherent meaning. The <div> element starts on a new line and fills the container width. Developers use <div> to wrap related content, including headings, paragraphs, and inline elements.

For example:

<div style="border: 1px solid #333; padding: 10px;">
  <h2>Section Title</h2>
  <p>
    This is a paragraph inside a div element. The div groups the content and applies common styling.
  </p>
</div>

This example shows how <div> organizes content into one block. It creates a clear section on the page.

Output:

Section Title

This is a paragraph inside a div element. The div groups the content and applies common styling.

Inline Elements

Inline elements do not start on a new line. They appear within the text and use only the necessary space. Inline elements stay on the same line as surrounding text. They cannot contain block-level elements.

HTML Inline Elements

<a> <abbr> <acronym> <b>
<bdo> <big> <br> <button>
<cite> <code> <dfn> <em>
<i> <img> <input> <kbd>
<label> <map> <object> <output>
<q> <samp> <script> <select>
<small> <span> <strong> <sub>
<sup> <textarea> <time> <tt>
<var>

Example:

<p>
  This is a paragraph with an <a href="#">inline link</a> and a
  <span style="background-color: #ff0;">highlighted text</span>.
</p>

In this example, the <a> and <span> elements appear within the paragraph. They take only as much space as needed.

The <span> Element

The <span> element is a simple inline container. It groups small portions of text or other inline content. It does not force a new line.

Example:

<p>
  This is a paragraph with an <a href="#">inline link</a> and a
  <span style="background-color: #ff0;">highlighted text</span>.
</p>

The <span> element wraps only the text that needs styling. It does not break the flow of the text.

Output:

This is a paragraph with an inline link and a highlighted text.

The Inline-Block Model

Inline-block elements blend features of inline and block elements. They stay on the same line but allow styling with width, padding, and margins.

Example:

<div style="display: inline-block; width: 150px; padding: 10px; border: 1px solid #333;">
  Inline-block 1
</div>
<div style="display: inline-block; width: 150px; padding: 10px; border: 1px solid #333;">
  Inline-block 2
</div>

Explanation: Two <div> elements use inline-block. They align horizontally and allow block styling.

Output:

Inline-block 1
Inline-block 2

Divs and Spans

Both <div> and <span> are generic containers. The <div> element is block-level and groups larger sections. The <span> element is inline and styles parts of text.

Consider this example:

<div style="border: 1px solid #333; padding: 20px;">
  <h2>Section Title</h2>
  <p>
    This paragraph is inside a div. Here is a
    <span style="color: blue; font-weight: bold;">styled span</span> within the paragraph.
  </p>
</div>

This example shows that <div> creates a clear section. The <span> applies style without breaking the text flow.

Output:

Section Title

This paragraph is inside a div. Here is a styled span within the paragraph.

FAQs on HTML Block and Inline Elements

What are block and inline elements in HTML?

Block elements start on a new line and fill the container width. Inline elements appear within text and use only the space needed.

Is an <h1> a block or inline element?

<h1> is a block-level element.

What are the two main types of HTML elements based on display behavior?

The two main types of HTML elements based on display behavior are block-level elements and inline elements. Block-level elements start on a new line and take up the full width of their container, while inline elements flow within the content and only take up as much width as necessary.

Is a <button> block or inline?

A <button> is inline by default. It can be styled as a block element.

Is a <div> a block element?

Yes, the <div> tag is a block-level element. It starts on a new line and stretches across the full width of its container. It is commonly used to group other elements together and structure content on a webpage.

Subscribe to our newsletter

Get the freshest news and resources for developers, designers and digital creators in your inbox each week

© 2000 – 2025 SitePoint Pty. Ltd.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.