Book a free demo

Web Developer Path ยท lesson 7 of 18

Adding CSS: three ways and three selectors

Make a plain page colourful. Learn where CSS goes, how a rule is written, and how selectors pick the right elements.

What you will learn

  • three places to put CSS: inline, internal <style> and an external file
  • rule syntax: selector, property and value
  • element, class (.name) and id (#name) selectors
  • colour names, hex codes and rgb()

Understand the idea

HTML says what content is; CSS (Cascading Style Sheets) says how it looks. There are three places to write it. Inline CSS goes in a style attribute on one element, internal CSS goes between <style> tags in the head, and external CSS lives in its own file linked with <link rel="stylesheet" href="style.css">. External files are best, because one file can style a whole website.

A CSS rule has a selector that says what to style, then curly brackets holding declarations. Each declaration is a property, a colon, a value and a semicolon: h1 { color: tomato; }. The three most useful selectors are the element selector (h1, every h1), the class selector (.intro, everything with class="intro") and the id selector (#special, the one element with that id).

Colours can be written in three ways. A name such as tomato or navy is easiest. A hex code such as #ff6347 is red, green and blue in base 16. rgb(255, 99, 71) gives the same three numbers from 0 to 255. Use color for text and background-color for the area behind it.

When two rules disagree, the more specific selector wins: an id beats a class, and a class beats an element. If they are equally specific, the rule written later wins. That is the "cascade" in the name.

Example

Three rules, three kinds of selector
h1 {
  color: rebeccapurple;
}

.intro {
  background-color: #fff3bf;
}

#special {
  color: rgb(220, 20, 60);
  background-color: lavender;
}

The page on the right already contains an h1, a paragraph with class intro and a paragraph with id special.

Your turn: code to type and practise

Colour the h1 text, give .intro a background-color, and give #special both a color and a background-color.

Stretch challenge

Write the same colour three ways (a name, a hex code and rgb()) and check they match.

โ† Forms and user inputBackgrounds and gradients โ†’

CSS

More in this module

Keep going in Web Developer Path

Practise the idea

๐ŸŽจ Play HTML & CSS Lab

Edit a safe sample web page and change its styles in a live preview.

Play now

FAQ

Adding CSS: three ways and three selectors: common questions

What is the difference between a class and an id?

A class can be used on many elements and is selected with a dot (.name). An id belongs to one element and is selected with a hash (#name).

Which CSS rule wins when two rules conflict?

The more specific selector wins (id, then class, then element). If they are equal, the one written last wins.

Should I use inline, internal or external CSS?

External CSS is best for real sites because one file can style every page. Inline and internal are fine for quick tests.

I am stuck. What can I do?

Press Hint for a nudge, read the message next to each goal, and compare your code with the lesson text. If you still need it, Show answer reveals a model solution. Try to type it yourself rather than copy it.

Does my work get saved?

Your ticks and XP are saved in this browser, and in your account if you log in. The code you type is not stored on a server.

Start a conversation

Enjoying coding? Meet a teacher.

Tell us the learner's age, country and what they would like to build, and we will suggest a class. A parent or guardian can make the enquiry.

  • Tell us the learnerโ€™s age or grade and goals
  • Weโ€™ll suggest a suitable class or learning path
  • Weโ€™ll confirm format, timing and fees with a parent or guardian

For a childโ€™s class, please have a parent or guardian send the enquiry. Avoid sharing passwords or sensitive personal details.

Weโ€™ll use your details only to respond to this request.

Play it, don't just read it

Write some code right now

Free games that need no sign-up. Pick one and play for five minutes.

All free games โ†’
Chat on WhatsApp
Esc

Try