In HTML, every element is either block-level or inline, which determines how they are displayed in the browser.


1. Block-Level Elements

  • Start on a new line by default.
  • Take up the full width available (stretch from left to right).
  • Can contain other block elements and inline elements.
  • Useful for structuring page layout.

Examples:

<div>, <p>, <h1> to <h6>, <section>, <article>, <header>, <footer>, <nav>, <table>, <form>, <ul>, <ol>, <li>, <blockquote>

Example Code:

<p>This is a paragraph.</p>
<div>This is inside a div block.</div>

Output:
Each appears on its own line.


2. Inline Elements

  • Do not start on a new line.
  • Only take as much width as needed for the content.
  • Usually used to style small portions of text inside a block-level element.
  • Cannot contain block-level elements (only other inline elements).

Examples:

<span>, <a>, <strong>, <em>, <img>, <code>, <label>, <abbr>, <b>, <i>, <mark>, <small>, <sub>, <sup>

Example Code:

<p>This is <span style="color:red;">red text</span> inside a paragraph.</p>

Output:
The text flows in the same line.


3. Key Differences Table

FeatureBlock-LevelInline
Line BreakStarts on a new lineDoes not start on a new line
WidthTakes full width availableTakes only the needed width
ContainmentCan contain block + inline elementsCan contain only inline elements
Examples<div>, <p>, <section><span>, <a>, <img>
PurposeStructure/layoutFormatting small pieces of content

4. Special Case: Inline-Block

  • Acts like inline (no automatic new line).
  • Behaves like block in that you can set width and height.
<span style="display:inline-block; width:100px; height:50px; background:lightblue;">Box</span>

5. Visual Example

Block Element:
[Heading (full width)]
[Paragraph (full width)]

Inline Element:
[Some text [link] more text here]

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *