HTML Block and Inline Elements

By Published Updated 3 min read

In HTML, elements are categorized into two main types: block-level elements and inline elements. These distinctions determine how elements are displayed on a web page and how they interact with other elements. Let’s explore each type:

Block-level elements:

Block-level elements are HTML elements that typically create a block or a rectangular container on the web page. They usually start on a new line and take up the full available width of their parent container. Here are some common block-level elements:

1. <div>: The <div> element is a generic container used to group and structure content on a web page. It doesn’t have any inherent styling and is often used for layout purposes.

<div style="background-color: lightgray; padding: 10px;">
    This is a block-level <div> element.
</div>
Code language: HTML, XML (xml)

2. <p>: The <p> element represents a paragraph of text. Paragraphs are displayed as block-level elements, and each new paragraph typically appears on a new line.

<p>This is a paragraph of text. It starts on a new line and has some space above and below it.</p>
Code language: HTML, XML (xml)

3. <h1>, <h2>, <h3>, <h4>, <h5>, <h6>: These are header elements used for headings of different levels. They are block-level elements and often used to organize content hierarchically.

<h1>Main Heading</h1>
<h2>Subheading</h2>
Code language: HTML, XML (xml)

4. <ul> and <ol>: These are used to create unordered (bulleted) and ordered (numbered) lists, respectively. List items (<li>) inside these elements are also block-level.

<ul>
    <li>Item 1</li>
    <li>Item 2</li>
</ul>
Code language: HTML, XML (xml)

5. <table>: The <table> element is used to create tables, with rows (<tr>) and cells (<td> or <th>) inside. Tables are displayed as block-level elements.

Inline elements:

Inline elements, on the other hand, are HTML elements that do not create a new block or line. They flow within the content and only take up as much width as necessary. Here are some common inline elements:

1. <span>: Similar to the <div> element, the <span> element is a generic container, but it’s an inline element and doesn’t create new blocks. It’s often used for applying CSS styles or scripting to specific parts of text.

This is a <span style="color: blue;">blue</span> word in the sentence.
Code language: HTML, XML (xml)

2. <a>: The <a> element is used to create hyperlinks. It allows you to link to other web pages or resources. The content inside the <a> tag is typically inline.

<p>Visit our <a href="https://example.com">website</a> for more information.</p>
Code language: HTML, XML (xml)

3. <strong> and <em>: These elements are used to apply strong and emphasis emphasis, respectively, to text. They are often displayed in a bold or italicized format.

<p>This is <strong>important</strong> information, and this is <em>emphasized</em> text.</p>
Code language: HTML, XML (xml)

4. <img>: The <img> element is used to display images on a web page. It’s an inline element, and text can flow around it.

<p>This is an <img src="image.jpg" alt="A beautiful image"> image.</p>
Code language: HTML, XML (xml)

5. <br>: The <br> element is used for line breaks within text. It’s an inline element that forces text to break and continue on the next line.

<p>This is some text.<br>And this is on a new line.</p>
Code language: HTML, XML (xml)

Understanding the difference between block-level and inline elements is essential for structuring and styling web content effectively. Block-level elements are typically used for larger content structures, while inline elements are used for small, inline modifications or additions within the text. This distinction helps web developers control the layout and appearance of web pages.

Khan Waseem is a tech lead and full-stack engineer in Bengaluru, building hospitality and SaaS software with Node.js, React and React Native. See the resume