Web Analytics

HTML Div & Span

Intermediate ~8 min read

Generic Containers

The <div> and <span> elements are generic containers with no semantic meaning. They're used purely for grouping and styling.

The Div Element

<div> is a block-level container used to group larger sections of content:

  • Creates page sections and layouts
  • Groups related content together
  • Applies styles to multiple elements at once
HTML
CSS
JS

The Span Element

<span> is an inline container used to style parts of text:

  • Highlights specific words or phrases
  • Applies styles without breaking text flow
  • Targets specific text with JavaScript
HTML
CSS
JS

Building Layouts with Div

Divs are essential for creating page layouts:

HTML
CSS
JS
Best Practice: Use semantic elements like <header>, <nav>, <main> when possible. Use div/span only when no semantic element fits.

Quick Quiz

Which element should you use to highlight a word within a paragraph?

A
<div>
B
<span>
C
<p>
D
<section>