Learning path · Level 2 · Tutorial 1

Apply CSS with Stylesheets and Selectors

Connect a stylesheet to a local HTML page, target the intended elements with reusable selectors, and inspect which rules match.

  • Level: Beginner
  • Duration: 11 min
  • 9 steps
Start tutorial Back to the path
Apply CSS with Stylesheets and Selectors

What you will go through

  1. Link a Local Stylesheet Create a local CSS file beside an HTML page, connect it in the document head, and verify the first visible style. 1 min
  2. Read and Write a CSS Rule Learn to read a CSS rule, write declarations with correct punctuation, and use comments as notes in a stylesheet. 1 min
  3. Match Element Types or All Elements Predict which elements type selectors and the universal selector match. 1 min
  4. Reuse Classes and Target an ID Use reusable classes and a unique ID to target the intended elements, including an element with two classes. 2 min
  5. Share Declarations with Selector Lists Replace duplicated heading declarations with one comma-separated selector list. 1 min
  6. Select Descendants and Direct Children Choose descendant and child combinators to control whether nested paragraphs are included. 2 min
  7. Recognize the Document-Root Selector Recognize the document-root pseudo-class and distinguish its single match from the universal selector. 1 min
  8. Inspect Matched Rules Inspect selected HTML elements in browser developer tools, trace matched rules to their local stylesheet, and repair a controlled class-selector mismatch. 1 min
  9. Apply and Check Your Selectors Complete a local stylesheet, verify its targets in browser developer tools, and distinguish intended matches from an excluded nested element. 2 min

What you will learn

  • Create a local CSS file and link it from an HTML document.
  • Write valid rules that target elements by type, class, ID, and structural relationship.
  • Use selector lists to share declarations without duplicating rules.
  • Inspect an element's matched rules and identify a selector that does not match as intended.

Before you start

  • Create and Inspect a Local HTML Page
  • Link Local Pages with Relative Paths
  • Organize Content with Semantic HTML

Step 1 of 9

Link a Local Stylesheet

Create a local CSS file beside an HTML page, connect it in the document head, and verify the first visible style.

Connect the page to its stylesheet

Two local files, one connection

An external stylesheet is a plain-text file saved with a .css extension. Keep index.html and styles.css together in the css-practice folder.

A <link> element in the HTML <head> connects the page to that stylesheet. Because both files are at the same level, the path is simply the stylesheet filename. The browser resolves that path from the location of index.html.

How the files relate

Diagram showing index.html and styles.css in the same folder, connected by a link element, with a browser preview whose Neighborhood Events heading is navy.

The link points from the HTML document to the CSS file. After both files are saved and the page is refreshed, the starter style changes the heading.

Add the supplied local files

Create index.html

On your computer, create a folder named css-practice. Create index.html and styles.css inside it. Paste this complete page into index.html. The link belongs in <head>, not in the visible page content.

Neighborhood Events page

Save as index.html. Open this file in your browser after you also save the stylesheet below.

html
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Neighborhood Events</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <header>
    <h1>Neighborhood Events</h1>
    <p id="welcome">Find an event near you.</p>
  </header>
  <main>
    <article class="event featured">
      <h2>Book Swap</h2>
      <p>Bring a book to exchange.</p>
      <footer>
        <p>Hosted by the library.</p>
      </footer>
    </article>
    <article class="event">
      <h2>Garden Walk</h2>
      <p>Meet at the community garden.</p>
    </article>
  </main>
</body>
</html>
Expected output
Neighborhood Events [displayed in navy]
Find an event near you.

Book Swap
Bring a book to exchange.
Hosted by the library.

Garden Walk
Meet at the community garden.

Expected browser result after both local files are saved and the page is refreshed. The heading is shown in navy; the remaining text uses the browser’s ordinary presentation at this point.

Starter stylesheet

Save this in styles.css, beside index.html.

css
/* Event styles */
h1 {
  color: navy;
}

Practice the connection

Complete the local link

Complete the connection used by the local page. Then save both files and refresh index.html in your browser.

html
<head>
  <meta charset="utf-8">
  <title>Neighborhood Events</title>
  <link rel="" href="">
</head>

Verify in your browser

Confirm the visible result

After completing the link, save index.html and styles.css, then refresh your local page. Report the page heading and the visible change you observed.

Write at least 20 characters (0/20).

Step 2 of 9

Read and Write a CSS Rule

Learn to read a CSS rule, write declarations with correct punctuation, and use comments as notes in a stylesheet.

Recognize the Rule Structure

The shape of a CSS rule

A CSS rule has two main parts:

  1. A selector, which identifies the elements the rule targets.
  2. A declaration block, enclosed in { and }.

Inside the block, each declaration follows this pattern: property: value;.

The colon connects a property to its value. The semicolon marks the end of that declaration.

Parts of a CSS rule

Annotated CSS rule showing h1 as the selector, braces around the declaration block, and color: navy; as a declaration with a property, colon, value, and semicolon.

A declaration is made of a property, a colon, a value, and a closing semicolon.

A complete CSS rule

In styles.css, this rule targets the page heading.

css
h1 {
  color: navy;
}

Write Clear Rules

Add declarations and comments

A declaration names one setting with its property and supplies a value for that setting. A rule can include several declarations; keep each one on its own line and end each with a semicolon.

A CSS comment begins with /* and ends with */. Comments can document your stylesheet, but they are not styling instructions.

More than one declaration

This rule has two declarations and one comment.

css
/* Event styles */
h1 {
  color: navy;
  background-color: whitesmoke;
}

Tip

Formatting reminder

Keep comment markers separate from active CSS. The browser ignores the comment, but it reads the rule below it.

Check the Vocabulary

Identify the parts

Match each CSS fragment to its role.

Tap an item and then its matching pair.

Apply the Syntax

Repair the CSS rule

Complete the rule so it keeps its intended selector and uses valid CSS syntax.

css
h1 
   

Step 3 of 9

Match Element Types or All Elements

Predict which elements type selectors and the universal selector match.

Type selectors target every matching element

Select by element name

A type selector is an HTML element name used as a selector. It targets every element with that name in the document—not only the first one.

For example, h2 targets both event headings on the Neighborhood Events page. A CSS rule applies its declarations to the elements selected by its selector.

What `h2` targets

A simplified page tree where both h2 elements are highlighted and labeled Matches, while other elements are gray and labeled Does not match.

The selector h2 matches both h2 elements, wherever they appear in this document.

A rule for every h2

A type selector followed by a declaration block

css
h2 {
  color: navy;
}

The universal selector

Match all elements

The universal selector is *. When used alone, it matches every element in the document.

It does not mean “every paragraph” or “every visible word.” It targets elements of all types: html, body, headings, paragraphs, articles, and more. Selector matching tells you which elements a CSS rule targets; it does not by itself explain every visible effect on the page.

Paragraphs versus all elements

Side-by-side trees compare p matching only paragraph elements with the universal selector matching every element.

p and * target different sets of elements.

A universal-selector rule

This rule targets every element in the page.

css
* {
  background-color: whitesmoke;
}

Practice: write a type selector

Target both event headings

Complete the selector so the rule targets every level-two heading.

css
 {
  color: navy;
}

Apply: identify the matched set

Check your selector matching

Match each selector to the set of elements it targets on the page.

Tap an item and then its matching pair.

Step 4 of 9

Reuse Classes and Target an ID

Use reusable classes and a unique ID to target the intended elements, including an element with two classes.

Use classes for repeated content

Reuse a class

A class selector starts with a dot (.) followed by a class name. It targets every element whose class attribute contains that name.

In the Neighborhood Events page, both articles use class="event". One CSS rule can therefore target both articles:

.event {
  background-color: whitesmoke;
}

The selector must use the class name exactly as it appears in the HTML.

One class can match more than one element

A document tree shows .event matching both articles, .featured matching only Book Swap, and #welcome matching the welcome paragraph. Other nodes are labeled Does not match.

A selector matches an element when the selector text corresponds to that element’s HTML attributes.

Read class names and an ID

Target a second class or one unique ID

An element may have several space-separated class names. The Book Swap article uses class="event featured", so it matches each individual class selector:

.event {
  background-color: whitesmoke;
}

.featured {
  color: navy;
}

These are two separate selectors. .event also matches Garden Walk; .featured matches only Book Swap.

An ID selector starts with #. The paragraph with id="welcome" is targeted by:

#welcome {
  color: navy;
}

Use an ID value once in a document.

Selector syntax at a glance

Selectors use different prefixes because they read different HTML information. This table describes intended reuse, not which selector wins when rules conflict.

Match the selector prefix to the HTML feature it targets.
Selector kindSyntaxMatchesTypical reuse
Typeh2Every h2 elementAll elements of that type
Class.eventElements with class="event"Many elements
ID#welcomeThe element with id="welcome"One element

Tip

Check the exact name

Keep the HTML value and CSS selector spelling aligned: class="featured" requires .featured, while id="welcome" requires #welcome. A missing dot, hash, or letter means the CSS rule will not match the intended element.

Practice: target the page elements

Complete the intended selectors

Complete the three selectors to target, in order: both event articles, the featured Book Swap article, and the welcome paragraph.

css
 {
  background-color: whitesmoke;
}

 {
  color: navy;
}

 {
  color: navy;
}

Step 5 of 9

Share Declarations with Selector Lists

Replace duplicated heading declarations with one comma-separated selector list.

Understand selector lists

One block, multiple selectors

When two selectors need the same declarations, write them in a selector list. Separate the selectors with a comma and a space. One declaration block is then shared.

An element matches the list if it matches any one of its selectors. So a list containing the two heading element names targets every matching <h1> and every matching <h2> on the page.

Combined heading matches

A document tree where one h1 and two h2 elements are marked Matches, while paragraphs are marked Does not match.

The selector list targets the combined set of h1 and h2 elements.

Write the shared rule

Refactor duplicated rules

Suppose two CSS rules have identical declaration blocks. Instead of maintaining the same declarations twice, replace the two selectors and blocks with one selector list and one block.

Use this only when the declarations should be shared. The refactor changes how the CSS is written, not the intended set of matching headings.

Before and after

The first two rules repeat the same declarations. The final rule keeps those declarations in one shared block.

css
/* Event styles */
h1 {
  color: navy;
  background-color: whitesmoke;
}

h2 {
  color: navy;
  background-color: whitesmoke;
}

/* Refactored */
h1, h2 {
  color: navy;
  background-color: whitesmoke;
}

Practice: combine identical declarations

Combine the heading rules

Complete the selector so this single CSS rule replaces identical rules for the page heading and the article headings.

css
 {
  color: navy;
  background-color: whitesmoke;
}

Step 6 of 9

Select Descendants and Direct Children

Choose descendant and child combinators to control whether nested paragraphs are included.

Select nested content with a space

Descendants can be nested deeply

A descendant combinator is one space between selectors. The final selector identifies what to target; the selector before it limits the target to content nested inside it.

In .event p, p is the target. It matches every paragraph at any depth inside an element whose class is event—not only paragraphs immediately inside the article.

A descendant selector

This CSS rule targets every paragraph nested inside an event article.

css
.event p {
  color: navy;
}

Any nested paragraph can match

Element tree showing that a descendant selector matches both a direct paragraph and a paragraph nested in a footer inside an event article.

With .event p, the space permits any nesting depth inside .event.

Compare the relationship rules

Restrict the relationship

A child combinator uses > and requires an immediate parent-child relationship. In .event > p, the paragraph must be a direct child of the event article.

The summary paragraph in each article is a direct child. The paragraph inside the article footer is not: its direct parent is footer.

Two structural selectors

Compare the two selectors. These are alternative ways to target paragraphs; use the one that matches the relationship you intend.

The final `p` is the target in both selectors.
SelectorRequired relationshipParagraphs matched per event article
.event pNested at any depthSummary paragraph and footer paragraph
.event > pDirect child onlySummary paragraph only

Descendant versus direct child

Side-by-side trees comparing a descendant selector, which includes the footer paragraph, with a child selector, which excludes it.

The > excludes the paragraph because a footer sits between that paragraph and the article.

Check the footer paragraph

Predict the nested match

Consider the paragraph Hosted by the library. inside the footer of the Book Swap article. Which statement is correct?

Apply the child combinator

Target only direct-child paragraphs

Complete the selector so the rule targets only paragraphs that are direct children of an .event article.

css
.event  p {
  background-color: whitesmoke;
}

Step 7 of 9

Recognize the Document-Root Selector

Recognize the document-root pseudo-class and distinguish its single match from the universal selector.

Find the root in the document tree

A selector for the document root

Some selectors begin with a colon. A selector in this form is a pseudo-class: it selects elements according to a defined condition or structural role.

:root is a pseudo-class. It selects the document's root element. In an HTML document, that element is html, not body.

One root versus every element

A comparison of :root matching only the html node and the universal selector matching every node in an HTML document tree.

:root has one match: html. The universal selector * matches every element in the document.

Write :root as a selector

Use the selector before the rule block

Read a pseudo-class as one selector unit: the colon belongs to its name. Place :root where a selector goes—before the braces and declarations.

This CSS rule selects html; it does not select body.

A :root rule

A CSS rule that uses the document-root pseudo-class

css
:root {
  background-color: whitesmoke;
}

Practice pseudo-class syntax

Add the required punctuation

Complete the selector so this rule uses the document-root pseudo-class.

css
root {
  background-color: whitesmoke;
}

Identify selector matches

Check the root selector

Match each symbol or selector to what it identifies in an HTML document.

Tap an item and then its matching pair.

Step 8 of 9

Inspect Matched Rules

Inspect selected HTML elements in browser developer tools, trace matched rules to their local stylesheet, and repair a controlled class-selector mismatch.

Read the matched-rule view

Select an element, then read its rules

Open your local Neighborhood Events page and open browser developer tools. When you select an element in Elements / Inspector, the Styles / Rules pane updates to show the CSS rules that match that selected element.

Use this view to connect three pieces of evidence: the selected HTML element, its attributes and nesting, and each matched rule. A listed rule also shows its stylesheet source, such as styles.css.

This step checks whether a selector matches. It does not compare competing declarations or decide which declaration wins.

How inspection connects HTML and CSS

Developer-tools diagram showing a selected Book Swap article, a matching featured class selector from styles.css, and a misspelled selector that does not match.

Select an element first, then compare its HTML values with the selectors listed in Styles / Rules.

Tip

Keep the correction local

Developer tools are useful for inspection and short experiments, but edits made only inside those tools do not update your local source files. Make lasting corrections in styles.css, save the file, and refresh index.html.

Create, observe, and correct the mismatch

Test a class-name mismatch

Create a controlled mismatch in your local styles.css. Add the rule below after your existing rules. If you already created a rule beginning with .featured, temporarily change that selector to the misspelled selector below instead of adding a duplicate.

Save styles.css, refresh index.html, and select the Book Swap container in Elements / Inspector. Its class values do not exactly match the misspelled selector, so this rule is absent from Styles / Rules. Then change only the selector spelling so it matches the HTML, save, refresh, and select the same element again.

Controlled mismatch rule

Add this controlled test rule to your local styles.css. After observing its absence, correct the selector in the same local file.

css
.feautured {
  background-color: whitesmoke;
}
Expected output
Before the local correction:
Selected in Elements / Inspector: Book Swap container
Styles / Rules: no .feautured rule from styles.css is listed.

After correcting the selector, saving styles.css, and refreshing:
Selected in Elements / Inspector: same Book Swap container
Styles / Rules: the corrected rule is listed with source styles.css.
background-color: whitesmoke;

Panel names and rule order vary by browser. Other matched rules may also be listed; focus on this controlled rule and its source.

Apply the inspection workflow

Repair and verify locally

Complete the controlled check on your computer. With the misspelled rule present, select the Book Swap container and confirm that rule is absent. Correct the selector in local styles.css, save, refresh, and inspect the same container again.

Report: the selected element's tag and class attribute; the selector now listed as a matched rule; and the stylesheet filename shown as its source.

Write at least 45 characters (0/45).

Step 9 of 9

Apply and Check Your Selectors

Complete a local stylesheet, verify its targets in browser developer tools, and distinguish intended matches from an excluded nested element.

Set up the page

Local application task

Finish by applying the selectors in your local css-practice folder. Keep index.html and styles.css at the same level.

Your stylesheet must:

  • style the h1 and both h2 elements together in navy;
  • give both event articles a whitesmoke background;
  • style the welcome paragraph in navy;
  • style only each article’s direct summary paragraph in navy, not the paragraph inside the Book Swap footer.

Use separate CSS rules with noncompeting declarations. This task checks whether selectors match their intended elements, not how competing declarations are resolved.

index.html

Use this exact index.html only if your existing local page differs. Save it in css-practice beside styles.css.

html
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Neighborhood Events</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <header>
    <h1>Neighborhood Events</h1>
    <p id="welcome">Find an event near you.</p>
  </header>
  <main>
    <article class="event featured">
      <h2>Book Swap</h2>
      <p>Bring a book to exchange.</p>
      <footer>
        <p>Hosted by the library.</p>
      </footer>
    </article>
    <article class="event">
      <h2>Garden Walk</h2>
      <p>Meet at the community garden.</p>
    </article>
  </main>
</body>
</html>
Expected output
Neighborhood Events
Find an event near you.
Book Swap
Bring a book to exchange.
Hosted by the library.
Garden Walk
Meet at the community garden.

After the completed stylesheet is saved and the page is refreshed, the visible text remains in this order. The h1 and both h2 headings are navy; the welcome paragraph and the two direct article summary paragraphs are navy; both article areas have whitesmoke backgrounds. “Hosted by the library.” is not targeted by the direct-child rule, so it does not receive that navy text color from that rule.

Preview the intended matches

Map the targets before you write

Plan each CSS rule from an element’s HTML identity and relationship:

  • The two heading types can share one declaration block through a selector list.
  • The repeated event class reaches both articles, including the article that also has featured.
  • The unique welcome paragraph is identified by its ID.
  • The child relationship reaches paragraphs immediately inside the articles. The footer paragraph is nested one level deeper, so it is outside that match.

Complete the stylesheet on the next screen, then save it and refresh your local page.

Selector map

A simplified Neighborhood Events element tree. Both event articles and their direct summary paragraphs are marked Matches, while the paragraph inside the Book Swap footer is marked Does not match for the child relationship.

A direct-child selector reaches the two summary paragraphs, but not the paragraph nested inside the footer.

Apply the selectors

Complete styles.css

Complete the compact stylesheet. Then enter the completed rules in your local styles.css file using the same formatting.

css
h1,  {
  color: navy;
}

. {
  background-color: whitesmoke;
}

# {
  color: navy;
}

. > p {
  color: navy;
}

Check matched rules

Inspect your evidence

After saving and refreshing your local page, use Elements / Inspector and Styles / Rules to verify the connection and the child-selector boundary.

Select the Book Swap summary paragraph, then select the paragraph inside its footer. Report the connection line you verified in index.html, the matched rule and stylesheet source for the summary paragraph, and whether that same rule appears for the footer paragraph.

Write at least 80 characters (0/80).

Finish

Summary

What you completed

  • A stylesheet connection lets rules in styles.css affect the local HTML page.
  • A selector list shares one declaration block across multiple selector targets.
  • Classes can be reused, IDs identify the intended unique element, and a child selector excludes deeper nested elements.
  • Elements / Inspector and Styles / Rules show which CSS rules match a selected element and which stylesheet supplied them.
  • Choosing matching elements is separate from resolving competing declarations; resolving those conflicts comes next.

Tutorial completed

Congratulations! You completed: Apply CSS with Stylesheets and Selectors

You completed the Neighborhood Events selector application and verified your matched rules locally.

Download the App now to have access to + 5000 free courses, exercises, certificates and lots of content without paying anything!

  • 100% free online courses from start to finish

    Thousands of online courses in video, ebooks and audiobooks.

  • More than 60 thousand free exercises

    To test your knowledge during online courses

  • Valid free Digital Certificate with QR Code

    Generated directly from your cell phone's photo gallery and sent to your email

Cursa app on the ebook screen, the video course screen and the course exercises screen, plus the course completion certificate