Introduction to CSS: Selectors, Rules and Safe Practice
CSS stands for Cascading Style Sheets. It controls how HTML content is presented, including colours, spacing, typography and layout. HTML provides structure; CSS describes presentation. The MDN introduction to CSS is a useful learning reference.
The shape of a rule
A selector identifies the elements to style. Inside braces, each declaration pairs a property with a value. This example styles elements with a particular class:
.notice {
color: navy;
padding: 1rem;
}The full stop belongs to the class selector. The colon separates property and value, and semicolons separate declarations. Keep punctuation and braces consistent so errors are easier to spot.
Class selectors and ID selectors
A class can be reused on several elements. An ID should identify one element within the document. These examples show the difference:
.project-note {
margin-block: 1.5rem;
}
#enquiry-summary {
padding: 1rem;
}In HTML, the corresponding attributes might be class="project-note" and id="enquiry-summary". Choose names that describe a purpose rather than the current colour, so they still make sense when the design changes.
Grouping and the universal selector
Separate selectors with commas when they share declarations. The universal selector matches elements broadly and should be used deliberately.
h2, h3 {
line-height: 1.2;
}
* {
box-sizing: border-box;
}Grouping does not mean the elements become a single selector. Check punctuation carefully: a comma, a space and no space can describe different matches.
Where CSS can be written
An external stylesheet keeps styles in a separate file. An internal style block places rules inside an HTML document. An inline style is an attribute on an individual element. Your website software may provide an editor or a theme setting for the relevant stylesheet; the exact route depends on the setup.
A stylesheet link in a learning example looks like this. The angle brackets are shown as code, not instructions to paste an entire document into an article editor:
<link rel="stylesheet" href="example.css">Colour and typography
Colours can be represented in several ways. The following keeps the hexadecimal example from this tutorial:
.sample-label {
color: #ff6347;
font-family: Georgia, serif;
font-weight: bold;
}This is a syntax example, not a recommendation that this colour is readable against every background. Check the actual contrast and text size in the finished design. Use suitable font fallbacks and avoid shrinking text solely to make a layout fit.
Why one rule may not appear to work
Check that the stylesheet loaded, the selector matches and the declaration is valid. Other rules, inheritance and the cascade can affect the final result. Inspect the element’s computed styles before adding repeated overrides or !important to every declaration.
Practise safely
- Keep the original stylesheet or a usable backup.
- Make one clear change and record its purpose.
- Check narrow screens and ordinary laptop widths.
- Try increased text size and keyboard navigation.
- Retest menus, forms and sticky controls.
For a production website, agree the change and test it on a current development copy first. Our Liverpool web design team can review a layout problem without applying a broad CSS rule to unrelated pages.
