Skip to main content

HTML Layout

HTML Div & Span

Written by Updated HTML Div & Span exercises

div and span are HTML's two neutral boxes. They mean nothing on their own; they exist so you can group things for CSS or JavaScript. A div is a block that takes its own line; a span sits inside a line of text.

Use them when no meaningful tag fits. If the content is a section, a list or important text, there is usually a better element. Each has its own lesson: the div tag and the span tag.

The <div> Element

<div> is a block-level container: it starts on a new line and stretches across the full width of its parent.

Div Syntax

html

<div>Content goes here...</div>

Div Example

html

<div style="background-color: #f0f0f0; padding: 10px;">
  <h2>Welcome to HTML</h2>
  <p>This section is wrapped inside a div element.</p>
</div>

The heading and paragraph are grouped in one grey box. The background and padding apply to the div, and everything inside it shares them.

Common Uses of <div>

  • To create page sections like header, footer, sidebar, or content area.
  • To apply CSS styling to grouped elements.
  • To structure layouts for responsive design.
  • To apply JavaScript functions to a specific section.

Example: Page Layout Using <div>

Div Example

html

<div id="header">Header Section</div>
<div id="menu">Menu Section</div>
<div id="content">Main Content Section</div>
<div id="footer">Footer Section</div>

Each part of the page is its own div with an id. Today, most of these would be semantic tags like <header> and <nav>; plain divs are better kept for boxes that have no special meaning, like a card's inner wrapper.

The <span> Element

<span> groups inline elements and text inside a line, so you can style part of a sentence.

Span Syntax

html

<span>Inline text here...</span>

Span Example

html

<p>Welcome to <span style="color: red;">HTML5</span> Tutorial.</p>

Only the word "HTML5" turns red; the rest of the sentence is unchanged.

Difference Between <div> and <span>

Question<div><span>
TypeBlock-levelInline
Starts a new lineYesNo
WidthThe full width of its parentAs wide as its content
Width and height in CSSWorkAre ignored
Typical useGrouping sections and layout boxesStyling words inside text

Combining <div> and <span>

Combined Example

html

<div style="background: #eef; padding: 10px;">
  <h3>User Details</h3>
  <p>Name: <span style="color: blue;">Amit Sharma</span></p>
  <p>Role: <span style="font-weight: bold;">Developer</span></p>
</div>

The div groups the user details, and spans pick out the name and email inside the paragraphs.

Styling <div> and <span> with CSS Classes

Div & Span Style Example

html

<style>
  .box { background: lightgray; padding: 10px; margin: 5px; }
  .highlight { color: green; font-weight: bold; }
</style>

<div class="box">
  <p>This is a <span class="highlight">highlighted</span> word inside a div.</p>
</div>

Classes keep the styling in one place: change .highlight once, and every highlighted span changes.

Practical Example: Layout with Div & Span

Practical Example

html

<div style="border: 1px solid #ccc; padding: 10px;">
  <h2>Product Information</h2>
  <p>Product Name: <span style="color: brown;">Wireless Mouse</span></p>
  <p>Price: <span style="color: green;">$25</span></p>
  <p>Description: <span>A smooth and precise wireless mouse for everyday use.</span></p>
</div>

Browser Support

Chrome
Edge
Firefox
Safari
Opera
IE9+
YesYesYesYesYesYes

Good to know

  • Do not put a div inside a span or a p. A block inside an inline element is invalid, and browsers will move it.
  • A page made only of divs works, but it gives screen readers nothing to navigate by. Use semantic tags for the main areas.

HTML Div & Span exercises

Group two paragraphs

Put two paragraphs, First and Second, into one div.

Result

Check your answer to see it here.