Free course · Lesson 8 of 30

CSS Basics: Selectors and How Styles Apply

Same HTML, completely different look. CSS is where your website gets its personality, and it all starts with one simple rule.

In this lesson

This is Video 8 of ManoraTech's free 30-video web development course, and the start of the CSS module. You will see the three ways to add CSS (inline, internal and external) and link one external stylesheet to every page of your website. You will learn the anatomy of a CSS rule (selector, property and value), see inheritance in action, and use element, class and id selectors, grouping and descendant selectors. Finally you will learn the cascade and specificity, the rules that decide which style wins, and use the Styles panel in Chrome DevTools to find crossed-out declarations.

What you will learn

  • Add CSS three ways (inline, internal, external) and know why external is best.
  • Read and write a CSS rule: selector, property and value.
  • Use element, class, id, grouping and descendant selectors.
  • Predict which rule wins using the cascade and specificity basics.

Key takeaways

  • Use one external stylesheet, linked with a link tag, for every page.
  • A CSS rule is a selector plus declarations: property, colon, value, semicolon.
  • Element selectors style every tag of a type, classes (with a dot) are reusable, and an id (with a hash sign) is unique.
  • Specificity decides first (id, then class, then element); in a tie, the rule that comes last wins.

What you will build

The first stylesheet for the personal website (my-website/). A new file, css/style.css, is linked from both index.html and contact.html with a <link> tag. It has rules for the whole page (body), all headings (a grouping selector), the navigation links (a descendant selector), and two classes: .tagline for the line under the name and .highlight for a phrase in the About text.

The code from this lesson

This is the code as it is at the end of the video. Copy it into VS Code and open the page with Live Server to see it in your browser.

index.html Home page, linked to the stylesheet; new classes tagline and highlight

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Sara Ahmed | Aspiring Web Developer</title>
    <link rel="stylesheet" href="css/style.css">
  </head>
  <body>
    <header>
      <h1>Sara Ahmed</h1>
      <p class="tagline">Aspiring web developer · Coffee lover · Lifelong learner</p>
      <nav>
        <ul>
          <li><a href="index.html">Home</a></li>
          <li><a href="#about">About</a></li>
          <li><a href="#skills">Skills</a></li>
          <li><a href="contact.html">Contact</a></li>
        </ul>
      </nav>
    </header>

    <main>
      <!-- About me -->
      <section id="about">
        <h2>About Me</h2>
        <figure>
          <img src="images/avatar.svg" alt="Illustration of Sara smiling, with dark hair and a yellow top" width="200" height="200">
          <figcaption>That's me — learning to code one line at a time.</figcaption>
        </figure>
        <p>Hi! I'm <strong>Sara</strong>, and I'm learning web development from scratch. I started this course because I want to <span class="highlight">build websites for small businesses</span> — and maybe one day for my own.</p>
        <p>When I'm not coding, you'll usually find me with a cup of <em>very</em> strong coffee and a good book.</p>

        <h3>My Hobbies</h3>
        <ul>
          <li>Photography</li>
          <li>Reading mystery novels</li>
          <li>Cooking new recipes</li>
          <li>Hiking on weekends</li>
        </ul>

        <h3>My Goals This Year</h3>
        <ol>
          <li>Finish this web development course</li>
          <li>Build and publish three websites:
            <ul>
              <li>A personal website (this one!)</li>
              <li>A to-do list app</li>
              <li>A website for a local business</li>
            </ul>
          </li>
          <li>Get my first freelance client</li>
        </ol>
      </section>

      <!-- Skills table -->
      <section id="skills">
        <h2>Skills I'm Learning</h2>
        <table>
          <caption>My learning progress</caption>
          <thead>
            <tr>
              <th scope="col">Skill</th>
              <th scope="col">Level</th>
              <th scope="col">Started</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>HTML</td>
              <td>Beginner</td>
              <td>Week 1</td>
            </tr>
            <tr>
              <td>CSS</td>
              <td>Starting soon</td>
              <td>Week 2</td>
            </tr>
            <tr>
              <td>JavaScript</td>
              <td>Starting soon</td>
              <td>Week 4</td>
            </tr>
          </tbody>
        </table>
      </section>

      <!-- Links -->
      <section id="links">
        <h2>Find Me Online</h2>
        <ul>
          <li><a href="https://github.com/" target="_blank" rel="noopener">GitHub</a></li>
          <li><a href="https://www.linkedin.com/" target="_blank" rel="noopener">LinkedIn</a></li>
          <li><a href="mailto:hello@example.com">Email me</a></li>
        </ul>
      </section>
    </main>

    <footer>
      <p>&copy; 2026 Sara Ahmed. Built with HTML while learning web development.</p>
    </footer>
  </body>
</html>

contact.html Contact page, linked to the same stylesheet; class tagline

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Contact | Sara Ahmed</title>
    <link rel="stylesheet" href="css/style.css">
  </head>
  <body>
    <header>
      <h1>Sara Ahmed</h1>
      <p class="tagline">Aspiring web developer · Coffee lover · Lifelong learner</p>
      <nav>
        <ul>
          <li><a href="index.html">Home</a></li>
          <li><a href="index.html#about">About</a></li>
          <li><a href="index.html#skills">Skills</a></li>
          <li><a href="contact.html">Contact</a></li>
        </ul>
      </nav>
    </header>

    <main>
      <section id="contact">
        <h2>Contact Me</h2>
        <p>Have a question or a project idea? Send me a message and I'll reply within two days.</p>

        <!-- action="#" keeps us on this page for now. A real site sends the data to a server or a form service. -->
        <form action="#">
          <div>
            <label for="name">Full name</label>
            <input type="text" id="name" name="name" placeholder="e.g. Ali Khan" required>
          </div>

          <div>
            <label for="email">Email</label>
            <input type="email" id="email" name="email" placeholder="you@example.com" required>
          </div>

          <div>
            <label for="phone">Phone (optional)</label>
            <input type="tel" id="phone" name="phone" placeholder="+1 555 123 4567">
          </div>

          <div>
            <label for="topic">What is it about?</label>
            <select id="topic" name="topic">
              <option value="question">A question</option>
              <option value="project">A project idea</option>
              <option value="hello">Just saying hello</option>
            </select>
          </div>

          <fieldset>
            <legend>How should I reply?</legend>
            <input type="radio" id="reply-email" name="reply" value="email" checked>
            <label for="reply-email">By email</label>
            <input type="radio" id="reply-phone" name="reply" value="phone">
            <label for="reply-phone">By phone</label>
          </fieldset>

          <div>
            <input type="checkbox" id="newsletter" name="newsletter" value="yes">
            <label for="newsletter">Send me tips about learning to code</label>
          </div>

          <div>
            <label for="message">Your message</label>
            <textarea id="message" name="message" rows="5" placeholder="Write your message here..." required></textarea>
          </div>

          <button type="submit">Send message</button>
        </form>
      </section>
    </main>

    <footer>
      <p>&copy; 2026 Sara Ahmed. Built with HTML while learning web development.</p>
    </footer>
  </body>
</html>

css/style.css The new external stylesheet

/* Styles for Sara Ahmed's personal website */

/* Element selector: styles the whole page */
body {
  font-family: Arial, sans-serif;
  color: dimgray;
  background-color: whitesmoke;
}

/* Grouping selector: one rule for three headings */
h1,
h2,
h3 {
  color: indigo;
}

/* Descendant selector: only the links inside nav */
nav a {
  color: indigo;
  font-weight: bold;
}

/* Class selectors start with a dot */
.tagline {
  color: slateblue;
}

.highlight {
  background-color: yellow;
}

Practice challenge

Give two of your hobbies a class called "favourite" and make them a different colour. Then give your footer an id and change its text colour with an id selector.

Show the solution

The solution files are below. In index.html, two hobbies have class="favourite", and the footer has id="page-footer" (on contact.html too, so both pages match). The two new rules are at the end of challenge-solution/css/style.css: .favourite (a dot for a class) and #page-footer (a hash for an id).

contact.html Solution

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Contact | Sara Ahmed</title>
    <link rel="stylesheet" href="css/style.css">
  </head>
  <body>
    <header>
      <h1>Sara Ahmed</h1>
      <p class="tagline">Aspiring web developer · Coffee lover · Lifelong learner</p>
      <nav>
        <ul>
          <li><a href="index.html">Home</a></li>
          <li><a href="index.html#about">About</a></li>
          <li><a href="index.html#skills">Skills</a></li>
          <li><a href="contact.html">Contact</a></li>
        </ul>
      </nav>
    </header>

    <main>
      <section id="contact">
        <h2>Contact Me</h2>
        <p>Have a question or a project idea? Send me a message and I'll reply within two days.</p>

        <!-- action="#" keeps us on this page for now. A real site sends the data to a server or a form service. -->
        <form action="#">
          <div>
            <label for="name">Full name</label>
            <input type="text" id="name" name="name" placeholder="e.g. Ali Khan" required>
          </div>

          <div>
            <label for="email">Email</label>
            <input type="email" id="email" name="email" placeholder="you@example.com" required>
          </div>

          <div>
            <label for="phone">Phone (optional)</label>
            <input type="tel" id="phone" name="phone" placeholder="+1 555 123 4567">
          </div>

          <div>
            <label for="topic">What is it about?</label>
            <select id="topic" name="topic">
              <option value="question">A question</option>
              <option value="project">A project idea</option>
              <option value="hello">Just saying hello</option>
            </select>
          </div>

          <fieldset>
            <legend>How should I reply?</legend>
            <input type="radio" id="reply-email" name="reply" value="email" checked>
            <label for="reply-email">By email</label>
            <input type="radio" id="reply-phone" name="reply" value="phone">
            <label for="reply-phone">By phone</label>
          </fieldset>

          <div>
            <input type="checkbox" id="newsletter" name="newsletter" value="yes">
            <label for="newsletter">Send me tips about learning to code</label>
          </div>

          <div>
            <label for="message">Your message</label>
            <textarea id="message" name="message" rows="5" placeholder="Write your message here..." required></textarea>
          </div>

          <button type="submit">Send message</button>
        </form>
      </section>
    </main>

    <footer id="page-footer">
      <p>&copy; 2026 Sara Ahmed. Built with HTML while learning web development.</p>
    </footer>
  </body>
</html>

index.html Solution

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Sara Ahmed | Aspiring Web Developer</title>
    <link rel="stylesheet" href="css/style.css">
  </head>
  <body>
    <header>
      <h1>Sara Ahmed</h1>
      <p class="tagline">Aspiring web developer · Coffee lover · Lifelong learner</p>
      <nav>
        <ul>
          <li><a href="index.html">Home</a></li>
          <li><a href="#about">About</a></li>
          <li><a href="#skills">Skills</a></li>
          <li><a href="contact.html">Contact</a></li>
        </ul>
      </nav>
    </header>

    <main>
      <!-- About me -->
      <section id="about">
        <h2>About Me</h2>
        <figure>
          <img src="images/avatar.svg" alt="Illustration of Sara smiling, with dark hair and a yellow top" width="200" height="200">
          <figcaption>That's me — learning to code one line at a time.</figcaption>
        </figure>
        <p>Hi! I'm <strong>Sara</strong>, and I'm learning web development from scratch. I started this course because I want to <span class="highlight">build websites for small businesses</span> — and maybe one day for my own.</p>
        <p>When I'm not coding, you'll usually find me with a cup of <em>very</em> strong coffee and a good book.</p>

        <h3>My Hobbies</h3>
        <ul>
          <li class="favourite">Photography</li>
          <li>Reading mystery novels</li>
          <li>Cooking new recipes</li>
          <li class="favourite">Hiking on weekends</li>
        </ul>

        <h3>My Goals This Year</h3>
        <ol>
          <li>Finish this web development course</li>
          <li>Build and publish three websites:
            <ul>
              <li>A personal website (this one!)</li>
              <li>A to-do list app</li>
              <li>A website for a local business</li>
            </ul>
          </li>
          <li>Get my first freelance client</li>
        </ol>
      </section>

      <!-- Skills table -->
      <section id="skills">
        <h2>Skills I'm Learning</h2>
        <table>
          <caption>My learning progress</caption>
          <thead>
            <tr>
              <th scope="col">Skill</th>
              <th scope="col">Level</th>
              <th scope="col">Started</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td>HTML</td>
              <td>Beginner</td>
              <td>Week 1</td>
            </tr>
            <tr>
              <td>CSS</td>
              <td>Starting soon</td>
              <td>Week 2</td>
            </tr>
            <tr>
              <td>JavaScript</td>
              <td>Starting soon</td>
              <td>Week 4</td>
            </tr>
          </tbody>
        </table>
      </section>

      <!-- Links -->
      <section id="links">
        <h2>Find Me Online</h2>
        <ul>
          <li><a href="https://github.com/" target="_blank" rel="noopener">GitHub</a></li>
          <li><a href="https://www.linkedin.com/" target="_blank" rel="noopener">LinkedIn</a></li>
          <li><a href="mailto:hello@example.com">Email me</a></li>
        </ul>
      </section>
    </main>

    <footer id="page-footer">
      <p>&copy; 2026 Sara Ahmed. Built with HTML while learning web development.</p>
    </footer>
  </body>
</html>

css/style.css Solution

/* Styles for Sara Ahmed's personal website */

/* Element selector: styles the whole page */
body {
  font-family: Arial, sans-serif;
  color: dimgray;
  background-color: whitesmoke;
}

/* Grouping selector: one rule for three headings */
h1,
h2,
h3 {
  color: indigo;
}

/* Descendant selector: only the links inside nav */
nav a {
  color: indigo;
  font-weight: bold;
}

/* Class selectors start with a dot */
.tagline {
  color: slateblue;
}

.highlight {
  background-color: yellow;
}

/* Challenge: two favourite hobbies in a different colour */
.favourite {
  color: crimson;
  font-weight: bold;
}

/* Challenge: an id selector starts with a hash */
#page-footer {
  color: slateblue;
}

Quick quiz

Check what you learned. Pick an answer to see if it is right.

Question 1 of 3Which is the recommended way to add CSS to a website?

Show the answer

A. An external .css file linked with <link> One external stylesheet styles every page, so you change things in one place.

Question 2 of 3How do you select elements with class="card" in CSS?

Show the answer

C. .card A dot selects a class, a hash (#) selects an id, and a plain word selects an element type.

Question 3 of 3p { color: blue; } and .intro { color: red; } both target the same paragraph. What colour is it?

Show the answer

B. Red, because a class is more specific than an element selector Specificity: id beats class, class beats element. The class rule wins whatever the order.

Frequently asked questions

What are the three ways to add CSS to HTML?

Inline styles with a style attribute on one tag, an internal style sheet in a style tag inside the head, and an external style sheet: a .css file linked with a link tag. External is best, because one file styles every page.

How do I link a CSS file to my HTML page?

Inside the head, add a link tag with rel="stylesheet" and an href with the path to the file, for example href="css/style.css". Add the same line to every page that should use it.

What is the difference between a class and an id in CSS?

A class can be used on many elements and is selected with a dot, like .tagline. An id must be unique on the page and is selected with a hash sign in front of its name. Most developers style with classes and keep ids for links and JavaScript.

What is a descendant selector in CSS?

Two selectors separated by a space, like nav a. It matches every link that sits anywhere inside a nav element, even several levels deep. Read it from right to left: an a, inside a nav.

Which CSS rule wins when two rules disagree?

The more specific selector wins: an id beats a class, and a class beats an element selector. When two selectors are equally specific, the rule that comes later in the file wins. That is the cascade.

Why is my CSS not working?

Check that the stylesheet is linked with the right path, that class selectors start with a dot, and that no more specific rule overrides yours. In Chrome, right-click the element, choose Inspect and look for crossed-out declarations in the Styles panel.

Read the full transcript

0:00 Welcome to ManoraTech

Hello, and welcome to ManoraTech! This is Web Development for Beginners, a free course in thirty short videos. This is video eight: CSS Basics, Selectors and How Styles Apply.

0:18 Same HTML, new look

This is the website you built in Module 2. It works, but it looks plain: black text, blue links, no design at all. Now watch. Not one HTML tag has changed, and the page has a new look. That is CSS. Over the next nine videos, your site will become this.

Today you take the first step: you will add CSS in three ways, write your first rules, and learn exactly which rule wins when two rules disagree.

0:47 Three ways to add CSS

CSS stands for Cascading Style Sheets. HTML says what each piece of content is. CSS says how it looks: colours, fonts, spacing and layout. There are three ways to add CSS to a page. You will see all three, so you can recognise them, and then we will choose the best one.

Way one is an inline style. You add a style attribute to a single tag. Here, the h1 turns indigo. It works, but only for this one element, and it mixes design into your HTML. Way two is an internal style sheet. Inside the head, you add a style tag and write CSS rules in it.

This rule styles every h1 on this page. But contact dot html cannot see it. With ten pages, you would copy it ten times. Way three is the professional way: an external style sheet. Delete both tests. Create a folder called css, and inside it a file called style dot css.

Then, in the head, add a link tag. The rel attribute says this file is a stylesheet, and href gives the path to it. Add the same line to contact dot html. Now one file styles every page, and you change your design in one place.

2:09 Anatomy of a CSS rule

Open style dot css and type your first rule. A CSS rule has two parts. First comes the selector, which picks the elements to style. Here it is body, the whole visible page. Then come curly braces, and inside them, the declarations. Each declaration is a property, a colon, a value and a semicolon.

The property is what you want to change, like color. The value is what you want it to be, like dim gray. Font family picks the font: Arial, or any simple sans-serif font if Arial is missing. Notice the layout: one declaration per line, indented by two spaces.

The browser does not care, but people reading your code do. And if you forget a semicolon, the next declaration can break. Save, and the whole page changes: a new font, grey text and a soft background. The paragraphs turn grey too, because text styles flow down to the elements inside.

We call that inheritance.

3:15 Selectors: element, class and id

The body rule uses an element selector: just the tag name. It styles every element of that type. Let us add one more: h1, color indigo. Your name turns indigo. But what if you want to style only some elements? Use a class. In index dot html, find the paragraph under your name and add class equals tagline.

A class is a label you choose yourself: use lowercase words, with dashes between them. In CSS, a class selector starts with a dot, so we write dot tagline. Save, and only the tagline changes colour. Many elements can share one class, so classes are reusable.

Now highlight a phrase in your About text. I am using the example name Sara, so pick words from your own text. Wrap them in a span, the neutral inline box from Video 6, with the class highlight. Then give dot highlight a yellow background.

Add the tagline class to contact dot html too. The third type is the id selector, and it starts with a hash. Your sections already have ids, so hash about styles the About section. But an id must be unique on a page, so an id rule can never be reused.

That is why most developers style with classes and keep ids for links and, later, JavaScript. Delete this test rule. Quick pause! If you are enjoying this video, please give it a like. And if you are new here, kindly subscribe to ManoraTech, and press the bell icon, so you get a notification for every new lesson.

Thank you! Now, let us keep going.

4:58 Grouping and descendant selectors

Our h1 is indigo, and the h2 and h3 headings should match it. You could write three rules, but there is a shortcut called grouping. List several selectors, separated by commas, and they share one set of declarations. Put each selector on its own line, so it is easy to read.

Save, and every heading is indigo. Next, the menu links. A rule for a, the anchor tag, would change every link on the page, including GitHub and LinkedIn further down. A descendant selector is more precise. Write nav, a space, and then a. It means: links that sit somewhere inside a nav.

Only the menu links turn bold and indigo. Read a descendant selector from right to left: an a, inside a nav. The link does not have to be a direct child. Ours sits inside a list item, inside a list, and the rule still matches.

That single space is what connects the two parts.

5:58 Which rule wins? Cascade and specificity

Sometimes two rules style the same element in different ways. Which one wins? CSS has clear rules for this. The C in CSS stands for cascade. When two rules have the same selector, the one that comes later in the file wins. Add a test rule at the very bottom: h1, color crimson.

Your name turns crimson, because this rule comes last. Now a different case. Change the test rule to p, color black. The tagline is a paragraph, so this rule matches it too. But the tagline stays slate blue, even though the p rule comes later.

Why? Specificity. A more specific selector wins, whatever the order. Think of a ranking: ids first, classes second, elements last. Only when two selectors have the same rank does the order in the file decide. DevTools shows you this. Right-click the tagline and choose Inspect.

In the Styles panel, the winning rule is at the top, and the losing color is crossed out. When a style does not work, this is the first place to look. You can even untick a declaration there to switch it off for a moment.

Now delete the test rule. Common mistake: forgetting the dot. If you write highlight without the dot, CSS looks for an element called highlight. There is no such tag, so nothing changes, and the browser shows no error.

7:27 Practice challenge

Your turn. In your hobbies list, give two of your hobbies a class called favourite, and make them a different colour, such as crimson or teal. Then give your footer an id, for example page dash footer, and change its text colour with an id selector.

Remember: a dot for a class, and a hash for an id. If something does not change, inspect it and look for a crossed-out rule. Pause the video now and try it. The solution is in the course files linked in the description.

8:00 Quick quiz

Quick quiz time! Three questions about this video. Answer out loud, or pause the video and write your answers in the comments. Question one. Which is the recommended way to add CSS to a website? A: An external dot css file, linked with a link tag.

B: Inline style attributes on every tag. C: A style tag in every page. D: Inside JavaScript. The answer is A. One external stylesheet styles every page, so you change things in one place. Question two. How do you select elements with the class card in CSS?

A: hash card. B: card. C: dot card. D: star card. The answer is C. A dot selects a class, a hash selects an id, and a plain word selects an element type. Question three. One rule makes all paragraphs blue. A second rule, for the class intro, makes them red.

Both target the same paragraph. What colour is it? A: Blue, because it comes first. B: Red, because a class is more specific than an element selector. C: Purple. D: Black. The answer is B. Specificity: id beats class, class beats element. The class rule wins whatever the order.

9:36 Outro and next video

Today you linked your first stylesheet, used element, class, id, grouping and descendant selectors, and learned which rule wins. In the next video, Colors, Fonts and CSS Units, your site gets a real colour palette and beautiful fonts. If this helped, give it a like, and tell me in the comments which colour you chose for your favourite hobbies.

Thank you for watching ManoraTech! If you liked this video, please give it a like, subscribe to the channel, and press the bell icon, so you never miss a new lesson. I will see you in the next video. Goodbye!

Need a website for your business now?

This course teaches you to build one yourself. If you would rather have our team build a fast, conversion-focused site for you, we can help.

Free AuditBook a Call