Link Your First Stylesheet

Rosa’s garden website works. Its pages have a header, a nav, a table of plots, and the garden’s rules as a list, and every link leads to its page. But it still looks like a page nobody has styled yet, with black serif text on white and underlined links in the browser’s default blue or purple. Nothing on it says garden.

This course changes that. It’s the second course on the HTML and CSS Foundations path, and if you’re starting here, you’re welcome too. The first section below gives you the site to work on. By the end, the garden’s three main pages share one stylesheet, the nav shows which page you’re on, the plots table reads at a glance, and you can find out in DevTools why a pasted rule doesn’t work.

CSS is declarative: you describe how the page should look rather than the steps to get there, so some programming habits won’t fit it. The lessons name each one as it comes up. Each section ends with exercises you run in the browser, and the course ends with a project on the garden’s site.

In this lesson, you’ll link a stylesheet to the garden’s work day notice, write your first rules, and find them in DevTools, so you can see why the page looks the way it does instead of guessing. Start by getting the garden’s files open in front of you.

Open the Garden Site

This lesson assumes you can program and know how an HTML page is put together, with a <head> that describes the page and a <body> that holds what the window shows. You should also be familiar with relative paths and the Elements panel in DevTools. You don’t need to know any CSS, and there’s no JavaScript or build step.

Work with the page open beside the lesson, type the CSS rather than pasting it, and change values to see what happens. The page changing is your check. Skip any step you’ve already done:

  1. Get a current version of Chrome or Edge. Every screenshot comes from Chrome, and Edge shows the same.
  2. Install Visual Studio Code from its website. Then open its Extensions view with Ctrl+Shift+X, or Cmd+Shift+X on macOS, search for Live Preview, and click Install on the one published by Microsoft.
  3. Get the garden/ folder. Use your own if you’ve built the garden’s site in Structuring Web Pages With HTML. If you’re arriving directly, or your folder differs from the one below, download the course’s sample code ZIP, unzip it, and copy its 00-intro/garden/ folder to where you keep your projects. The photos aren’t included, so add your own images under the names shown below.
  4. Open the folder in VS Code with File → Open Folder, then open events/work-day.html.
  5. Start the preview. With the page as the active tab, open the Command Palette with Ctrl+Shift+P, or Cmd+Shift+P on macOS, and run Live Preview: Show Preview (External Browser). If the page opens in another browser, paste its address into Chrome or Edge.

Your folder holds these files:

garden/
│
├── events/
│   ├── seed-swap.html
│   ├── upcoming.html
│   └── work-day.html
│
├── images/
│   ├── leaf-divider.png
│   ├── plot-7.jpg
│   └── seed-swap-2026.jpg
│
├── about.html
├── index.html
└── plots.html

The home, plots, and about pages each have a header, a nav, one <main>, and a footer. The events/ folder holds the events page, upcoming.html, and two simpler pages that each contain only a <main> element: the work day notice and the seed swap. Here’s the notice at http://127.0.0.1:3000/events/work-day.html, the page you’ll style first:

The work day notice in the browser's default styles, with a large bold black heading, black serif text and two blue underlined links.
The Work Day Notice Before Any CSS

Even without CSS, the notice has a look of its own. Its heading is big and bold, its paragraphs have space between them, and its links are underlined, blue until visited, and often purple after. DevTools shows where that comes from later in this lesson. First, give the notice some styles of your own.

To style a page, the browser needs CSS, and the best home for it is a stylesheet, a file of its own that the page points to. You’ll try out ideas on the event pages with a practice stylesheet, events/events.css, and save the site’s real stylesheet for the project.

In VS Code’s Explorer, right-click the events folder, choose New File…, and name the file events.css. Type this into it, and save:

Language: CSS Filename: events/events.css
h1 {
  color: darkgreen;
}

The notice doesn’t change yet, because a page loads a stylesheet only when its HTML asks for it. That request belongs in the <head>, with the other things that describe the page. Add a <link> element to the head of work-day.html, right after the <title>:

Language: HTML
<head>
  <meta charset="utf-8">
  <meta name="viewport"
    content="width=device-width, initial-scale=1">
  <title>Work Day | Elm Street Community Garden</title>
  <link rel="stylesheet" href="events.css">
</head>

The <link> element connects the page to another file. Like <meta>, it’s a void element, so it has no end tag. Its rel attribute says what the file is to this page. Here, that’s a stylesheet. Its href says where the file is.

A relative href works the same way here as in a link: the browser reads it from the folder containing the page. The notice and events.css are both in events/, so the filename alone is enough. From the home page in garden/, the path to the same file would be events/events.css.

Save the page, and the heading turns dark green. Nothing else changes, because your rule mentions only <h1> elements. If the heading stays black, compare the href with the filename and folder in VS Code’s Explorer, and check that rel="stylesheet" is there. Without it, the browser doesn’t load the file as CSS.

If it’s still black after those checks, the button below opens a chat with the mentor, with the question already typed. Add your <link> line and where your files are:

Read the Parts of a Rule

One green heading proves that the link works. Before you write more, name the parts of what you typed, because every style rule you’ll write in this course has the same shape.

h1 { color: darkgreen; } is a rule. It starts with a selector, h1, which identifies the elements the rule applies to: every <h1> on the page. The braces hold the declaration block.

Inside it, color: darkgreen; is a declaration: a property, color, which sets the text’s color, then a colon, a value, darkgreen, and a semicolon that ends the declaration. The value is one of CSS’s named colors.

The braces may remind you of a block of code that runs once, from top to bottom. A rule isn’t that. It describes what should be true—“every <h1> has dark green text”—and the browser makes it true for every element the selector matches. The order of rules matters only when two of them disagree, and later lessons show exactly when.

A rule can hold several declarations, one per line. Add a rule for <body> above the h1 rule, so the whole page gets the garden’s look, and a comment at the top that says what the file is for:

Language: CSS Filename: events/events.css
/* Practice styles for the garden's event pages */

body {
  background-color: honeydew;
  color: darkslategray;
  font-family: system-ui, sans-serif;
}

h1 {
  color: darkgreen;
}

Text between /* and */ is a comment, the only kind CSS has, so // doesn’t start one.

The background-color property paints behind an element’s content, and on <body> it fills the whole window. The value of font-family is a list, read left to right: system-ui is your operating system’s own font, and sans-serif is the fallback, any font without the small strokes at the ends of letters.

Save the file, and the notice changes all at once:

The work day notice on a pale green background, in a sans-serif font, with a dark green heading, dark gray text and two links still blue and underlined.
The Notice Styled From events.css

The paragraphs are dark slate gray, but the heading stays dark green, because a rule that targets the <h1> itself beats a color the heading picks up from <body>. Passing styles down like this is called inheritance, and it gets its own lessons later. That’s all you need about it for now. The two links keep their default colors and underlines, so some other rule must be styling them.

Inspect Your Rules in DevTools

The page shows the result of your rules, but not which rules produced it, so you can’t yet tell what’s styling those links. DevTools can. Right-click the heading and choose Inspect. The Elements panel opens with the <h1> selected. Beside the tree, the Styles pane lists every rule that matches the selected element:

The Styles pane for the h1 lists an empty element.style, the h1 rule from events.css line 9, then the user agent stylesheet's h1 rule with font-size and font-weight.
The Styles Pane for the Notice's Heading

Read it from the top. The element.style entry is empty. It’s where a style attribute on the element would appear, and you’ll meet one in the next section. Below it is your h1 rule, with events.css:9 at its right, the file and line it comes from.

Below your rule is one labeled user agent stylesheet. The user agent is the browser, and this rule comes from its built-in default stylesheet. It sets the heading’s font-size and font-weight, which is why the heading was big and bold before you wrote any CSS. Every page starts from these defaults, and your rules change only what they mention.

Further down, under Inherited from body, is your body rule, with its color crossed out, because the heading’s own rule gives it a color. Inspect one of the links, and you’ll find the default stylesheet’s rule for links, with its own color and text-decoration: underline. That color is why your body color is crossed out there too.

Now write a rule with no listing to copy. At the end of events.css, add a rule that gives every <h2> the named color olive, and save. The two subheadings, Turn the Compost and Mulch the Plots, turn olive, while the <h1> and the paragraphs keep their colors. If nothing changes, check the selector, braces, colon, and semicolon.

Inspect one of the subheadings: your h2 rule sits right under element.style, with events.css and its line number, and the inherited body color is crossed out. Then delete the h2 rule and save, so your file matches the one the next lessons start from.

Share One Stylesheet Between Pages

Besides a <link>, there are two other ways to get CSS into a page, and you’ll meet both in other people’s code. Once you see why the linked file is the usual choice, you’ll know where your own CSS belongs.

A <style> element in the page’s <head> holds rules directly, and they apply to that page only:

Language: HTML
<style>
  h1 {
    color: darkgreen;
  }
</style>

A style attribute goes on one element. It holds declarations with no selector and no braces, because the element it’s on is the one it styles:

Language: HTML
<h1 style="color: darkgreen;">Saturday Work Day</h1>

The Styles pane would show that declaration under element.style. Both forms work, but they tie the look to one page or even one element. Rosa’s site has six pages, and the colors you chose belong on all of them. A linked file gives every page that links to it the same rules, so one change in the file changes every page at once. It also keeps the HTML focused on the content.

You can see that now. Open events/seed-swap.html, and add the same line to its <head>, right after its <title>:

Language: HTML
<link rel="stylesheet" href="events.css">

Save, and open the page at /events/seed-swap.html instead of /events/work-day.html. The seed swap now looks like the styled notice: a pale green background, sans-serif text in dark slate gray, and its Spring Seed Swap heading in dark green, without a single new rule. Change darkgreen to purple in events.css, save, and reload both pages. On each, the <h1> at the top turns purple, and nothing else changes. Set it back to darkgreen afterward.

Before you move on, try putting the case for the linked file into your own words. The button below opens the mentor with the start of that explanation. Finish the sentence and send it, and you’ll get feedback on your reasoning:

You’ve written rules in a linked stylesheet and checked in the Styles pane that the browser applied them. So far, every declaration you’ve typed has been correct. In the next lesson, Catch CSS That Fails Silently, you’ll find out what the browser does with a declaration that’s wrong, and why nothing on the page tells you.

Become a Member to join the conversation.