CSS chapters

Chapter 14 of 17

Reusable styles and organisation

Try this in our CSS Compiler →

The idea

CSS files do not provide JavaScript-style module scope by default. Organise rules around components and avoid broad selectors that unexpectedly affect unrelated content. A local @import must appear before ordinary style rules, but a stylesheet link is often simpler for a small project.

Worked example

.study-card { padding: 16px; border: 1px solid teal; }
.study-card__title { margin-top: 0; }

What to check

The naming groups related rules without claiming that they are isolated. The same class name elsewhere in the document still matches.

Practice task

Style two cards with the same class and one unrelated heading. Confirm the unrelated heading remains unchanged.

Check your understanding

Explain the result in your own words before changing the example. Change one thing at a time, compare the result with your prediction, and keep the version that demonstrates the concept clearly.

Try it yourself

Output
Ctrl/Cmd+Enter runs · Ctrl/Cmd+S downloads the project · Local auto-save is always on