1 / 9 · Basic
What CSS does
One CSS file can restyle every page of a site at once. That was the main argument for CSS when the W3C, the group that sets web standards, published CSS level 1 in December 1996.
Jump to the 10 practice questionsYou will learn
- Know what happens to a declaration the browser does not understand
1Explore
Structure in HTML, looks in CSS
HTML says what each part of a page is: a heading, a list, a form. CSS, short for Cascading Style Sheets, says how those parts should look: their colours, fonts, sizes, spacing and position on the screen. Here is a small example. In HTML, <h1>Corner Bakery</h1> says this is the main heading. A CSS rule saying that h1 should be blue changes how that heading looks, and the HTML stays the same.
The HTML below the style element never changes; only the two rules do.
Tap a choice: the code and the preview change together.
<style>
h1 { color: black; }
p { font-size: 14px; }
</style>
<h1>Corner Bakery</h1>
<p>Fresh bread every morning.</p>Nothing is drawn: there is no visible content.
2Explain
The shape of a rule
A stylesheet is a list of rules. Each rule starts with a selector, which says which elements it applies to, followed by a declaration block in curly braces. Inside the block, each declaration is a property, a colon, a value and a semicolon. In the rule above, h1 selects every level one heading, and the two declarations make those headings blue and set their font size to 32 pixels.
1 selector, 2 property, 3 value, 4 declaration block. Each property: value pair is one declaration and ends with a semicolon.
3Problem
What color is
In p { color: red; }, what is color?
Hint
It comes before the colon.
Solution
Answer: a property
The word before the colon names the feature being set, so color is the property. The selector p chooses elements, while red supplies the value.
4Problem
The end of a declaration
What ends each declaration?
Hint
Look at what separates color: red from the next declaration.
Solution
Answer: A semicolon
A missing semicolon merges two declarations into one invalid one.
5Explain
One mistake skips one declaration
When the browser meets a declaration it does not understand, such as a misspelled property or a value that makes no sense, it skips that one declaration and carries on with the next. This is deliberate: it lets new CSS features be added without breaking older browsers. The flip side is that a single missing semicolon can silently swallow the declaration that follows it, because the two run together into one invalid declaration.
Fix the spelling of font-sise and watch the text grow.
<style>
p { color: green; font-sise: 30px; background: #fff4d6; }
</style>
<p>The misspelled property is skipped; the others still apply.</p>6Problem
A declaration the browser does not know
The browser meets a declaration it does not understand. What happens?
Hint
The rest of the rule is not thrown away.
Solution
Answer: It skips that declaration and continues
Skipping unknown declarations lets new features coexist with old browsers.
7Explain
Three ways to add CSS
An external stylesheet, linked from the <head> with <link rel="stylesheet">, is the usual choice for a real site: every page shares it, and the browser downloads it once and keeps it in its cache. A <style> element holds rules for one page, which is what the exercises in this course use. The style attribute applies declarations to a single element; it is handy for a quick test but hard to maintain, because the same look has to be repeated on every element.
<!-- 1. External stylesheet: one file for the whole site -->
<link rel="stylesheet" href="styles.css">
<!-- 2. Internal stylesheet: rules for this page only -->
<style>
p { line-height: 1.5; }
</style>
<!-- 3. Inline style: one element only -->
<p style="color: green;">Fresh today</p>The link and style elements normally go in the head.
8Problem
Blue links without CSS
Even a page with no CSS has some style: headings are big and bold, links are blue and underlined, lists are indented. These come from the browser's default stylesheet, called the user agent stylesheet. Your rules are applied on top of it. That is why the bakery page from the HTML course was readable before it had any CSS, and why a good stylesheet changes only what it needs to.
Why are links blue on a page with no CSS?
Hint
Some styles come with the browser.
Solution
Answer: The browser default stylesheet
The browser applies its user agent stylesheet even when the author supplies no CSS. Those default rules give links their familiar colour and underline.
9Practice
Your first two rules
Write two rules: make the h1 colour #1a5fb4, and make every p 18px in size.
- h1 colour
- p font size
<style>
/* write your rules here */
</style>
<h1>Corner Bakery</h1>
<p>Fresh bread every morning from 7 am.</p>Hint
Two rules, each with one declaration: h1 { color: … } and p { font-size: … }.
A solution
<style>
h1 { color: #1a5fb4; }
p { font-size: 18px; }
</style>
<h1>Corner Bakery</h1>
<p>Fresh bread every morning from 7 am.</p>10Practice
Find the missing character
This rule does nothing because of one missing character. Fix it so the paragraph is green and 20px.
- green text
- 20px
<style>
p { color: green font-size: 20px; }
</style>
<p>This text should be green and 20 pixels.</p>Hint
Every declaration ends with a semicolon.
A solution
<style>
p { color: green; font-size: 20px; }
</style>
<p>This text should be green and 20 pixels.</p>11Explain
Common mistakes
<style>
p { color: green font-size: 20px; }
</style>
<p>Fresh bread</p><style>
p { color: green; font-size: 20px; }
</style>
<p>Fresh bread</p>Without the semicolon the browser reads one long, invalid value for color and skips both declarations.
12
Summary
- HTML gives structure; CSS gives looks.
- Rule = selector { property: value; }.
- External stylesheet for a site, style element for a page, style attribute for one element.
- Unknown declarations are skipped, so check semicolons when a style does not apply.