HTML & CSS · Beginner · 15 min
Style elements with CSS
Use element and class selectors and explain a simple cascade.
Select an element
CSS rules pair a selector with declarations inside braces. A declaration contains a property and a value separated by a colon. A <style> element can hold the rules in these exercises. On a larger site, an external stylesheet usually keeps the CSS easier to maintain.
<style>
p { color: navy; }
</style>
<p>A blue paragraph.</p>Expected appearance
color: rgb(0, 0, 128)
Reuse a class
A class can be shared by several elements. A selector starting with a dot matches that class. For ordinary declarations in the same cascade layer, a class selector is more specific than an element selector. If specificity is equal, the later rule wins.
<style>
p { color: navy; }
.note { color: teal; }
</style>
<p class="note">A note.</p>Expected appearance
color: rgb(0, 128, 128)
Try it yourself
Code runs on this device. When you are signed in, drafts sync to your account.
Edit the page content and include CSS in a style element. The preview supports a limited set of HTML elements. Scripts, external resources, navigation to other sites, and form submission are disabled.
Build and check
Keep the paragraph’s class="note" and text A note. Add CSS that makes its color teal.
Write a .note rule inside <style> and set color: teal.
Show worked solution
<style>
.note { color: teal; }
</style>
<p class="note">A note.</p>Write a .note rule inside <style> and set color: teal.
Page preview
Run to preview your page. Check tests the task requirements, including the requested screen widths. Check results do not replace keyboard and visual review.
Try it yourself
- Add a second paragraph with the same class.
- Predict which rule wins before changing the example’s selectors.