HTML Docs

HTML Block vs Inline

Understanding the difference between block and inline elements in HTML, with examples and use cases.

In HTML, elements are categorized into block and inline elements. Understanding these differences will help you build well-structured web pages.

🧱 Block Elements

Block elements take up the full width available (by default) and start on a new line. These elements are used to structure your webpage layout and can contain other elements inside them.

Examples:

  • <div>: A generic container used to group content.
  • <p>: A paragraph element used for text.
  • <h1> to <h6>: Header tags used for titles of various importance.

Example of Block Elements:

<div>
  <h1>This is a Block Element</h1>
  <p>This paragraph is part of a block-level element.</p>
</div>

Key Features of Block Elements:

  • Takes full width: Block elements stretch across the entire width of the page.
  • Starts on a new line: These elements create a new line before and after.
  • Can contain other block elements: Block elements can nest other block elements.

Block elements are essential for creating the structure of your webpage, like headers, sections, and paragraphs.

🔗 Inline Elements

Inline elements do not start on a new line and only take up as much width as needed. They are mostly used for styling smaller sections of content, like parts of text or links, within a block element.

Examples:

  • <span>: Used for styling a section of text without breaking the flow.
  • <a>: Anchor tag used to create links.
  • <img>: Embeds an image within the content.

Example of Inline Elements:

<p>This is a <span>inline</span> element, and <a href="#">this is a link</a>.</p>

Key Features of Inline Elements:

  • Does not take full width: Inline elements only take up as much width as required for their content.
  • Does not start on a new line: Inline elements appear within the same line, continuing the flow of content.
  • Cannot contain other block elements: Inline elements cannot wrap or contain block-level elements.

Inline elements are great for styling small parts of content or adding interactive elements like links or images without breaking the flow of your page.

⚖️ Key Differences Between Block and Inline Elements

PropertyBlock ElementsInline Elements
Takes full widthYesNo
Starts on a new lineYesNo
Can contain other block elementsYesNo
Examples<div>, <p>, <h1><span>, <a>, <img>

Warning: Mixing block and inline elements without understanding their behavior can lead to unexpected layouts, so it's important to use them appropriately!

🧪 Try Yourself

import "./styles.css";

document.getElementById("app").innerHTML = `
<h1>Hello world</h1>
`;

📝 Conclusion

Understanding the difference between block and inline elements helps you structure and style web pages more effectively. Use block elements for the main structure, and inline elements to handle small sections or content inside block elements.

Pro Tip: Use block elements to create sections like headers and paragraphs, and inline elements to style specific pieces of text or add links.

📚 Resources

On this page