Coding Lesson
CSS: Appearance
Reusable classes
Your goal: Apply one style to several elements.
Learn
A class lets several elements share the same styling rule. You will give two tips the same class, then change one rule to update both. This is easier to maintain than repeating separate styling on every paragraph.
Walkthrough: why the code works
Compare the starter paragraphs
The introductory paragraph has no class. A useful tip has class="highlight", while Another tip has none. In style, .highlight sets a light-grey background. Run the unmodified starter and identify which paragraph receives that background.
Match the HTML name to the CSS selector
Add class="highlight" to the opening tag of the second tip. Write highlight without a dot in HTML; write .highlight with a dot in CSS. The dot means select elements with this class. Keep the spelling exactly the same.
<p class="highlight">A useful tip.</p> <p class="highlight">Another tip.</p>Change the shared rule
Replace the background value and add a text colour in the existing .highlight rule. Both matching paragraphs receive the change. The introduction stays outside this group, so it does not receive this rule.
.highlight { background-color: #dbeafe; color: #172554; }Choose classes for reuse
A class can be used on more than one element. An id identifies a particular element and must be unique in this document. You can put the highlight class on another paragraph or a different element when it serves the same purpose. Choose names that describe a role rather than names such as blue-text that become misleading when the design changes.
Test that the selector is specific enough
Run the code and compare all three paragraphs. Temporarily remove the class from one tip: only that tip should lose the shared style. Put the class back. If you add padding later, change the shared rule once instead of editing every tip.
Expected results
Both tips have the chosen background and text colour.
The introductory paragraph does not acquire the highlight background.
Changing the shared background value once updates both tips after Run Code.
Common mistakes and how to fix them
No tip changes: Look for a missing dot in the CSS selector or a spelling mismatch with the HTML class.
Every paragraph changes: Check that the selector is .highlight rather than p. The p selector targets every paragraph.
The sample answer is confusing to paste: Its CSS belongs in style and its p element belongs in body. Do not paste the mixed snippet into only one of those locations.
View starter code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My hobby page</title>
<style>
.highlight {
background-color: #eee;
}
</style>
</head>
<body>
<h1>My hobby</h1>
<p>I enjoy learning something new.</p>
<p class="highlight">A useful tip.</p>
<p>Another tip.</p>
</body>
</html>Your challenge
Give both tips the highlight class, and change its background and text colour.
Keep developing your hobby page: Download HTML before leaving the sandbox, then choose Open HTML file when returning to your project. Load saved draft restores only this lesson’s draft in this browser.
Practise in the sandboxShow a hint
Add class="highlight" to the second p. Keep the dot in the CSS selector.
Show a sample answer
Try the challenge first. This is one possible answer or a snippet to adapt.
.highlight {
background-color: #dbeafe;
color: #172554;
}
<p class="highlight">Another tip.</p>Check your work
Both tips receive the same style, while the introductory paragraph stays unchanged.
Completion is your own check, not an automatic grade.
