Divs, Spans, Classes & IDs
Sometimes no HTML element has quite the right meaning for what you need. That's when you use the two general-purpose containers: <div> for blocks and <span> for pieces of text. On their own they mean nothing, so you label them with classes and IDs so CSS can style them and JavaScript can find them.
The div element
<div> (short for division) is a block-level container with no meaning of its own. Use it to group elements together, usually so CSS can lay them out or decorate them as a unit.
<div class="card"> <h3>Weather App</h3> <p>Shows the 5-day forecast for any town in East Africa.</p> <a href="weather.html">View project</a> </div>
Without CSS, this looks exactly like the heading, paragraph and link on their own. The div only becomes visible when you style it, for example with a border and shadow to make a "card".
The span element
<span> is the inline equivalent: a meaningless wrapper for a few words inside a line of text.
<p>Order status: <span class="status-paid">Paid</span></p> <p>Total: KES <span id="total">1,250</span></p>
Here the first span lets CSS colour the word "Paid" green, and the second gives JavaScript a handle for updating the total when the basket changes.
Before reaching for a div or span, check whether a meaningful element fits: <nav>, <article>, <section>, <strong>, <em>, <time>… Use div and span only when nothing else applies. Pages built entirely from divs ("div soup") are hard to read and give screen readers nothing to work with.
The class attribute
A class is a name you invent to label a group of elements. Many elements can share a class, and one element can have several classes separated by spaces.
<div class="card">…</div> <div class="card featured">…</div> <p class="note">…</p>
The second div has two classes, card and featured, so it gets all the card styles plus the extra featured styles. In CSS you select a class with a dot:
.card { border: 1px solid #ccc; padding: 16px; }
.featured { border-color: gold; }
The id attribute
An id is a unique name for exactly one element on the page. No two elements on the same page may share an id, and each element can have only one.
<section id="contact"> <h2>Contact</h2> … </section>
IDs have three main uses:
- Link targets:
<a href="#contact">jumps to this section. - JavaScript:
document.getElementById("contact")finds it instantly. - Connecting elements: a form
<label for="email">points at the input withid="email".
In CSS you select an id with #, such as #contact { … }, but most developers style with classes and keep ids for links, labels and scripts, because id selectors are very specific and hard to override (see Specificity & the Cascade).
class | id | |
|---|---|---|
| How many elements? | Any number | Exactly one per page |
| Several per element? | Yes, space-separated | No, one only |
| CSS selector | .name | #name |
| Typical use | Shared styles and groups | Link targets, labels, JavaScript |
Naming well
- Names are case-sensitive:
Cardandcardare different. Stick to lowercase. - Names can't contain spaces (a space separates two class names). Use hyphens:
project-card. - Describe what something is, not how it looks.
class="error-message"still makes sense if you change the colour from red to orange;class="red-text"doesn't. - Don't start an id with a digit; it makes CSS selectors awkward.
Custom data attributes
To attach your own extra information to an element, use an attribute starting with data-. JavaScript can read it, and it doesn't affect how the page looks.
<button data-product-id="4521" data-price="350">Add to basket</button>
In JavaScript, these become button.dataset.productId and button.dataset.price (hyphenated names turn into camelCase).
Try it
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Divs, spans, classes and ids</title>
<style>
.card { border: 1px solid #ccc; border-radius: 8px; padding: 12px; margin-bottom: 12px; }
.featured { border: 2px solid goldenrod; background: #fff8dc; }
.price { font-weight: bold; color: darkgreen; }
</style>
</head>
<body>
<h1 id="top">Second-hand textbooks</h1>
<div class="card">
<h2>KCSE Chemistry Revision</h2>
<p>Good condition. <span class="price">KES 450</span></p>
</div>
<div class="card featured">
<h2>Oxford Atlas for East Africa</h2>
<p>Like new. <span class="price">KES 900</span></p>
</div>
<p><a href="#top">Back to top</a></p>
</body>
</html>
Both books appear as bordered cards because they share the card class. The second card also has featured, so it gets a gold border and a pale yellow background. Both prices are bold and green thanks to the price class on their spans. Try adding featured to the first card too.
Key points
<div>is a block container and<span>an inline one; neither has meaning, so use them only when no semantic element fits.- A
classcan be shared by many elements, and an element can have several; select it in CSS with.name. - An
idmust be unique on the page; use it for link targets, form labels and JavaScript. - Name classes by purpose, in lowercase with hyphens; store extra data in
data-*attributes.
Tip: use the ← and → keys to move between pages.
