Sub topic 4
Connect a stylesheet and write your first rules
This chapter assumes you have the HTML skeleton from the earlier chapters saved as index.html, with a <head>, a <main> and a few sections. You should be able to edit files in a text editor and reload the page in a browser.
How a stylesheet reaches the page
CSS lives in a separate text file. The browser loads the HTML, finds a <link> element in the <head>, fetches the file it names, and applies the rules to the matching elements.
Create styles.css in the same folder as index.html. Then add this line inside the <head>:
<link rel="stylesheet" href="styles.css">The rel attribute tells the browser what kind of file this is. The href attribute is a path relative to the HTML file. If you put the stylesheet in a folder called css, the value becomes css/styles.css.
The anatomy of a rule
A CSS rule has a selector and a declaration block. Each declaration inside the braces is a property, a colon, a value and a semicolon.
body {
margin: 0;
font-family: system-ui, sans-serif;
line-height: 1.6;
}Here body is the selector. The block sets three properties on the <body> element. Many of these values are inherited by the elements inside it, a topic a later chapter covers.
Choosing elements with selectors
A selector decides which elements a rule applies to. These four cover most of a one-page site.
| Selector | Matches | Example |
|---|---|---|
| Type | Every element of that tag | `h2` |
| Class | Elements with that `class` value | `.project-list` |
| ID | The one element with that `id` | `#about` |
| Descendant | An element inside another | `nav a` |
You can also group selectors with a comma, so one block styles several targets:
h1,
h2 {
margin-top: 0;
color: #1f2933;
}
nav a {
text-decoration: none;
}
#about {
padding: 2rem 0;
}Prefer classes for most styling. IDs are already used in this site as anchor targets, so reusing them as style hooks makes later changes harder.
Common errors
Most "my CSS does nothing" problems come from a short list of causes:
- Wrong path in
href. A typo or a wrong folder means the file never loads. Open the browser's developer tools, go to the Network tab, and look for a 404 onstyles.css. - Missing semicolon or brace. The browser skips the broken declaration and sometimes the rules after it. Check the line above the one that fails.
- Link outside the
<head>. Browsers often tolerate this, but keep it in the head so styles load before the page is painted. - Misspelled property or value.
colourandtext-align: centreare ignored without any error message. - Selector mismatch.
.projectwill not matchclass="projects". Class names must be identical, including case. - Cached stylesheet. If edits do not appear, do a hard reload (Ctrl+Shift+R or Cmd+Shift+R).
Try it
Add the <link>, then put the body, h1, h2 and nav a rules above into styles.css. Reload. The page font should change, the navigation links should lose their underlines, and the headings should lose their top margin.
Exercise
You add <link rel="stylesheet" href="css/styles.css"> but the page is unstyled, and your file sits next to index.html as styles.css. What is wrong, and how do you confirm it?
Show answer
The href points to a css folder that does not exist. Change it to styles.css, or move the file into a css folder. To confirm, open the developer tools Network tab and look for a 404 response for the stylesheet.