Learn HTML and CSS: Div and span
Div and span
Hilary wants the opening hours in a coloured box, and the word "closed" in red. There's no element that means "box" or "red". That's what the two empty boxes are for.
Boxes without meaning
A <div> is a box without meaning. You put other elements in it to style them together: a box around two paragraphs, with one class.
A <span> does the same inside a line of text: a bit that gets its own style, like the word closed.
The difference: a <div> starts on a new line and takes the full width, a <span> simply stays in the line.
The shelter's CSS gives .box a background and .closed a red colour.
<main> <h2>Opening hours</h2> <div class="box"> <p>Open on Wednesdays and Saturdays from 1 pm to 5 pm.</p> <p>On Sundays we're <span class="closed">closed</span>.</p> </div></main>Better an element with meaning
A <div> and a <span> say nothing about what's inside. With CSS you can make a <div> look like a heading, but a screen reader then hears plain text, and Google sees no chapters.
If there's an element that says what something is, use that one: <h2> for a heading, <ul> for a list, <nav> for a menu, <strong> for importance.
A <div> or <span> is for whatever's left: a box that's only there for the CSS. Look at the tree: this heading isn't a heading at all.
<link rel="stylesheet" href="style.css"><main> <div class="title">Our cats</div> <div>Misty and Felix love lying in the sun.</div></main>.title { margin-top: 16px; font-size: 1.5rem; font-weight: bold;}Try it yourself
Put the two paragraphs together in a <div class="box">, and the word closed in a <span class="closed">.
<main> <h2>Opening hours</h2> <p>Open on Wednesdays and Saturdays from 1 pm to 5 pm.</p> <p>On Sundays we're closed.</p></main>body { margin: 0; font-family: system-ui, sans-serif; background: #fffaf3; color: #3d2b1f;}header { padding: 20px 16px 14px; background: #f6c88f;}header h1 { margin: 4px 0 0;}.slogan { margin: 4px 0 10px;}nav ul { display: flex; flex-wrap: wrap; gap: 4px 18px; margin: 0; padding: 0; list-style: none;}nav a { color: #7a3e12; font-weight: bold;}main { padding: 8px 16px 24px;}table { border-collapse: collapse;}th,td { padding: 6px 12px; border: 1px solid #e3c9a8; text-align: left;}th { background: #fbe3c4;}.card { margin: 12px 0; padding: 12px 16px; border-radius: 16px; background: #ffffff;}.card h3 { margin: 4px 0;}.box { padding: 4px 16px; border-radius: 16px; background: #fde9d4;}.breed { color: #b5541c;}.closed { color: #b42318; font-weight: bold;}footer { padding: 12px 16px; background: #f6c88f;}Ready for more?
The full lesson has 5 exercises, from easy to challenging, with hints, points and a streak you can build.
Start the lesson