Div & Span

The div Element

Example

<div> is a generic block container, used to group other elements so they can be styled or laid out together:

<!DOCTYPE html>
<html>
<head>
  <title>Page Title</title>
</head>
<body>

<div style="background:#282A35; color:white; padding:16px; border-radius:6px;">
  <h2>London</h2>
  <p>London is the capital city of England, with more than 9 million inhabitants.</p>
</div>

</body>
</html>

Result

The span Element

Example

<span> is a generic inline container, used to style part of a text:

<!DOCTYPE html>
<html>
<head>
  <title>Page Title</title>
</head>
<body>

<p>My mother has <span style="color:#1971c2; font-weight:bold;">blue</span> eyes and my father has <span style="color:#2f9e44; font-weight:bold;">green</span> eyes.</p>

</body>
</html>

Result

When to Use Them

Tip: Use <div> and <span> only when no element with a meaning fits. For a page header use <header>, for an article use <article>, for emphasis use <em> (see Semantic Elements).