Div, Span and Display
What Is It?
Div groups a larger piece of content. Span groups a small piece inside a line of text.
What the Tags Mean
div: a general container that normally starts on a new line.span: a general container that normally stays in the same line.class: names a group for styling.
Example 1
Save each example as index.html and open it in your browser. Each code block is a complete page.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Div, Span and Display</title>
</head>
<body>
<div>
<h2>My card</h2>
<p>A short description.</p>
</div>
</body>
</html>Browser preview
What you see: The heading and paragraph belong to one container.
Example 2: Another Use
Use span when only a few words need a different style.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Div, Span and Display</title>
</head>
<body>
<p>My favourite color is <span style="color: blue">blue</span>.</p>
</body>
</html>Browser preview
Simple Tip
Use a meaningful tag such as nav or main when it describes the content better than div.
Try It Yourself
Use span to color one word in a sentence.