Detailed · Deep study
1Creating styles
/* main.css - house style for Rivers Hotel */
body { background-color: #f4f8fb; font-family: Arial, sans-serif; }
h1 { font-family: Georgia, serif; font-size: 28px; color: #003366; text-align: center; }
h2 { font-size: 20px; color: #006699; }
p { font-size: 14px; }
li { font-style: italic; }
table { border-collapse: collapse; width: 80%; }
th, td { border: 2px solid #003366; padding: 6px; text-align: left; vertical-align: top; }
th { background-color: #cce0f0; font-weight: bold; }
.offer { background-image: url("../images/stripe.png"); color: #990000; }<p style="color: red; font-weight: bold;">Fully booked in December</p>
<p class="offer">Special offer</p>| Attached (external) stylesheet | Inline style attribute | |
|---|---|---|
| where | a separate .css file linked in the head | inside the opening tag of one element |
| applies to | every page that links to it | that one element only |
| advantages | consistent style across a whole site; change one file to restyle every page | quick, one-off change |
| priority | lower | highest – overrides attached styles |
| Style | Class | |
|---|---|---|
| what | formatting applied to an HTML element (e.g. every h1 or p) | a named set of formatting (e.g. .offer) applied only to the elements given that class |
| used by | all elements of that type automatically | adding class="offer" to chosen elements |
| example | h1 { color: navy; } | .offer { color: red; } |
2The cascade and file paths
When several stylesheets are attached, the one attached last has the highest priority if styles conflict; inline styles have priority over all attached stylesheets. Stylesheets must be attached using relative file paths (e.g. styles/main.css) so they still work when the site is uploaded to a web server or moved to another computer.
3Exam focus
Key words: CSS; external stylesheet; inline style; class; property; font-family; border-collapse; padding; comment; cascade; relative path.
Quick self-check
Answer in your head first, then reveal.
A style is applied to all elements of a type, e.g. every h1 (1); a class is applied only to elements given that class name (1).
The one attached last/lower in the head (1).
h2 { color: blue; font-size: 18px; text-align: center; } (3).
