CSS selectors and colours
You'll learn to read a CSS rule and say which elements it styles and how.
Watch & read
Start with the required ones. The others are there when you need them.
Explanation
Your HTML says what things are. Now you want them to look good: a dark heading, a softer grey for small text, a nicer font. That is the job of CSS.
Think of the HTML as a plain school uniform. CSS is the instruction sheet: "all shirts white, house captains wear a red badge". The instruction names who it is for, then what to change.
A piece of CSS is called a rule:
h1 {color: #1e3a8a;font-family: Arial, sans-serif;}.note {color: gray;}
h1is the selector. It picks every<h1>on the page.coloris a property, the thing to change.#1e3a8ais its value, a dark blue..notewith a dot picks every element withclass="note". Classes let you style some paragraphs and not others.
Colours can be names (gray), hex codes (#1e3a8a) or rgb(30, 58, 138). They all mean the same thing.
CSS lives in its own file, like style.css. The HTML connects to it with one line in the <head>:
<link rel="stylesheet" href="style.css">.
When two rules pick the same element and disagree, the more specific one wins. For now, remember one thing: a class beats a tag. If they are equally specific, the rule lower down in the file wins.
People often forget the dot. note { } looks for a <note> tag, which does not exist, so nothing
happens and there is no error. Also check every line ends with a ;. One missing ; can break the
next line too.
When you read CSS, read the selector first and find those elements in the HTML. Then read the properties as a list of changes.
Code tour
Click a line number with a ring to read the note for that line.
Quick check
3 questions. Each one explains itself after you answer.
- 1 / 3Which selector picks
<p class="price">? - 2 / 3In
h2 { font-size: 24px; }, what isfont-size? - 3 / 3
p { color: black; }comes after.warning { color: red; }. What colour is<p class="warning">?