← HTML practicum

Lesson 02 · 12 steps

Styling the page

The same site, now with an appearance. style.html and three stylesheets: the rules start inside the page, move out into files of their own, and then the two ways of doing it start arguing.

Hand in style.html style1.css style2.css style3.css
What the browser makes of it open on its own ↗
02One style for the whole documentThe task: use an embedded style to print four paragraphs on the site's subject.code ↓
<body>

<!-- 2) впроваджений стиль для всього документа -->
<style>body {background:#141414;color:#e6e6e6}</style>

<h1> Економіка раунду </h1>
<p> Перемога в раунді приносить команді 3250 доларів на кожного гравця </p>
<p> Поразка приносить менше, але сума зростає за кожну поразку поспіль </p>
<p> <b>Еко-раунд</b> — це раунд, у якому команда навмисно не купує зброю </p>
<p> Зекономлені гроші йдуть на повну закупівлю в наступному раунді </p>

A rule inside a <style> tag is called an embedded style: it lives in the page itself and applies to the whole of it. body {background:#141414;color:#e6e6e6} reads as «give the document body this background and this text colour».

Colour is inherited, so saying it once on body is enough — every paragraph inside turns light without being told.

Today

The sample puts <style> inside body. That works, but styles are collected in <head> so the browser knows the appearance before it starts drawing and the page does not flash unstyled first.

03Styling a heading and bold textThe task: an embedded style for the document's heading and bold text, plus four paragraphs.code ↓
<!-- 3) впроваджений стиль для заголовка та напівжирного рядка -->
<style type="text/css">
h1,b {background:black;color:#f0a500}
</style>
<!-- 3) чотири абзаци до стилю заголовка та рядка -->
<h1> Зброя й ціни </h1>
<p> AK-47 коштує 2700 доларів і вбиває з одного пострілу в голову навіть у шоломі </p>
<p> M4A4 коштує 3100 доларів, а M4A1-S — 2900 і стріляє тихіше </p>
<p> AWP за 4750 доларів забирає суперника з одного влучання в тулуб </p>
<p> Desert Eagle за 700 доларів — <b>улюблений пістолет снайперів на еко-раундах</b> </p>

A comma in a selector means «both»: h1,b is one rule covering headings and bold text at once, instead of the same properties typed twice.

04Several different tagsThe task: an embedded style applied to several different tags; add four new paragraphs.code ↓
<!-- 4) впроваджений стиль одразу для кількох різних тегів -->
<style type="text/css">
p {background:#262626}
b {background:#f0a500;color:black}
h1 {color:#ff7b5b}
</style>
<!-- 4) чотири нових абзаци до стилю кількох тегів -->
<p> Гранати теж коштують грошей: димова 300, світлошумова 200 </p>
<p> Повна закупівля п'ятьох гравців коштує близько 20 тисяч доларів </p>
<p> <b>Форс-бай</b> — закупівля на останні гроші, коли іншого шансу немає </p>
<p> Втрачена зброя дістається суперникові, тому AWP намагаються не губити </p>

Different rules for different tags this time. Notice that h1 now appears twice — once in the block above and once here. The later one wins, because the selectors weigh the same. That is the cascade, the C in CSS.

05ClassesThe task: show how to create classes with different properties.code ↓
<!-- 5) класи тегів із різними властивостями -->
<style type="text/css">
p.p1 {background:#1b1b1b;color:#f0a500}
p.p2 {background:#1b1b1b;color:#8bd450}
p.p3 {background:#1b1b1b;color:#5bb4ff}
p.p4 {background:#1b1b1b;color:#ff7b5b}
p.p5 {background:#1b1b1b;color:#d0d0d0}
</style>
<!-- 5) текст для демонстрації класів -->
<p class="p1"> Ролі в команді: </p>
<p class="p2"> entry fragger заходить на точку першим </p>
<p class="p3"> support кидає гранати й підтримує заходження </p>
<p class="p4"> AWPer тримає довгі лінії й ламає заходи суперника </p>
<p class="p5"> IGL придумує тактику й ухвалює рішення за всю команду </p>

A class is a label pinned to a tag: <p class="p1">. The selector p.p1 reads «a paragraph carrying the class p1». It is how identical tags are made to look different without touching the rest of the page.

Today

Classes are named after what they mean, not numbered: role, price, warning. A name like p1 tells you nothing six months later — including you.

06A linked stylesheetThe task: use a linked style, with the properties described in style1.css.code ↓
<!-- 6) зв'язаний стиль: увесь документ -->
<link rel="stylesheet" href="style1.css" type="text/css">
/* 7) зв'язаний стиль для всього документа style.html */
body {background:grey;color:white}
<!-- 6) абзац до зв'язаного стилю всього документа -->
<p> Зв'язаний стиль зручний тим, що один файл CSS працює на всіх сторінках сайту </p>

A linked style sits in a file of its own and the page pulls it in with link. The point is reach: one file can dress a hundred pages, and editing it changes all of them at once.

08A linked stylesheet for several tagsThe task: use a linked style on several different tags; add a new paragraph.code ↓
<!-- 8) зв'язаний стиль: кілька різних тегів -->
<link rel="stylesheet" href="style2.css" type="text/css">
/* 9) зв'язаний стиль одразу для кількох різних тегів */
h1 {background:#2b2b2b;color:#f0a500}
p {background:#333333;color:#e8e8e8}
<!-- 8) абзац до зв'язаного стилю кількох тегів -->
<p> Турнірні сторінки оформлені одним файлом, тому заголовки всюди однакові </p>

The second file is attached exactly like the first. The browser reads them top to bottom, which makes the order of the link tags part of the cascade too.

10Classes in a linked stylesheetThe task: text demonstrating the classes created in style3.css.code ↓
<!-- 10) зв'язаний стиль: класи -->
<link rel="stylesheet" href="style3.css" type="text/css">
/* 11) класи у зв'язаному стилі */
p.p6 {background:#1d2b1d;color:#8bd450}
p.p7 {background:#2b1d1d;color:#ff7b5b}
<!-- 10) два абзаци до класів зі зв'язаного стилю -->
<p class="p6"> Natus Vincere — київська організація, заснована 2009 року </p>
<p class="p7"> Її склад виграв Major у Стокгольмі 2021 року </p>

These classes are deliberately called p6 and p7 rather than p1 and p2 as in the sample. The reason: p1–p5 are already described by the embedded style in the page itself, and embedded beats linked. Reuse the names and the rules in style3.css would change nothing at all — and nothing on the page would show that linked classes work.

Today

This is the case lesson 3 takes apart as cascade priority: when two rules disagree about one tag, the winner is not the one that looks more official but the one with the heavier selector — and, failing that, the one written later.