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
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.
More in this module
Keep going in Web Developer Path
Backgrounds and gradients
Give `.hero` a `background-image` that uses `linear-gradient`, set its `background-size`, and give `.card` a `background-color`. Use the long-hand properties.
Start โ9Fonts and text styling
Style `h1` with a `font-family`, a `font-size`, `text-align: center` and `text-transform: uppercase`. Style `p` with a `line-height` and `letter-spacing`, and remove the underline from `a` with `text-decoration: none`.
Start โ10Combinators, pseudo-classes and pseudo-elements
Write four rules with exactly these selectors: `.menu a:hover` (set a `color`), `.menu li:first-child` (set `font-weight`), `h2::before` (set `content`) and `input[type="text"]` (set a `border`).
Start โ11The box model
Style `.card`: give it a `width`, some `padding`, a `border`, a `margin` of `12px auto`, and `box-sizing: border-box`.
Start โ12Display and position
Make `.card` `position: relative`, pin `.badge` to its top-right corner with `position: absolute`, `top` and `right`, and hide `.hide-me` with `display: none`.
Start โPractise the idea
๐จ Play HTML & CSS Lab
Edit a safe sample web page and change its styles in a live preview.
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
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.
Pick a game
- Code BuilderThe lines of a Python program are mixed up. Tap them into the rightโฆPlay
- Bug HunterA Python program prints the wrong answer. Find the one line with theโฆPlay
- Predict the OutputRead a short Python program and choose what it prints. Then watch it runโฆPlay
- Code JungleWrite real Python to guide Miko the monkey to every banana. 24 levels:โฆPlay