← HTML practicum

Lesson 03 · 4 steps

The cascade: which rule wins

Three ways to set an appearance — in the tag, in the page, in a separate file — all on one page, style1.html. One class is described twice on purpose, so you can see which rule survives and why.

Hand in style1.html style4.css
What the browser makes of it open on its own ↗
02Inline styleThe task: three paragraphs styled with an inline style.code ↓
<!-- 2) внутрішньотекстовий стиль: три абзаци -->
<p style="font-family:Century;font-size:medium;font-style:oblique;font-weight:100;"> Counter-Strike тримається на трьох речах: прицілі, </p>
<p style="font-family:Verdana;font-size:x-large;font-style:italic;font-weight:bolder;"> знанні карти </p>
<p style="font-family:Georgia;font-size:20pt;font-style:italic;font-weight:lighter;"> і довірі до команди. </p>

An inline style is written straight into the tag, in a style attribute. It reaches that one paragraph and nothing else.

Four font properties: font-family for the typeface, font-size, font-style for slant, font-weight for thickness. Size takes a word (medium, x-large) or a number with a unit (20pt).

Today

If a typeface is missing from the reader's computer the browser quietly substitutes its own, so real sites either list fallbacks or ship the font with the page — which is what kogiant.com does with IBM Plex.

03All three at onceThe task: apply embedded, linked and inline styles to the paragraphs at the same time; create style4.css for the linked one.code ↓
<!-- 3) зв'язаний стиль -->
<link rel="stylesheet" href="style4.css" type="text/css">

<!-- 3) впроваджений стиль -->
<style type="text/css">
body {background:#141414}
p {background:#1f1f1f;color:#e6e6e6}
p.p3 {background:#1d2b1d;color:#8bd450}
h2.p5 {font-family:Arial;color:#f0a500;background:#2b2b2b}
h1 {color:#ff7b5b}
</style>
/* 3) зв'язаний стиль для сторінки style1.html.
   Клас p3 навмисно описано і тут, і впровадженим стилем у самій сторінці:
   так видно, який із них переможе. */
p.p3 {background:grey;color:green;font-size:20pt}
p.p4 {background:#8b1a1a;color:white;font-size:large}
<!-- 3) чотири нові абзаци: три способи задати стиль одночасно -->
<h1> Карта Mirage </h1>
<p> Mirage поділена на три напрямки: A, мід і B </p>
<p class="p4"> Через мід команда забирає контроль над усією картою </p>
<h2 class="p5"> Точка A </h2>
<p style="font-family:Century;color:#f0a500;background:#2b2b2b"> На точці A вирішує димова граната в CT-спавн </p>
<p class="p3"> На точці B все залежить від того, хто першим зайде в апартаменти </p>
<h2 class="p5"> Хто перемагає </h2>

Three ways of styling meet in one document, and the class p3 is described twice: grey on green in style4.css, dark green in the embedded block. On the page the embedded one wins.

The browser decides in this order: first by the weight of the selector, and only when those are equal, by which rule comes later. The linked file is attached at the top of head and the embedded block follows it, so the embedded block takes it.

An inline style outranks both. It sits in the tag itself, and the only thing that overrules it is an !important note — which is best left alone.

Today

The pecking order is inline → embedded → linked, with «later wins» settling ties. It is exactly why large sites barely use the first two: once rules are scattered across tags, nobody can predict what the page will look like.