Lesson 05 · 10 steps
Every element on a page is a rectangle with space outside it, a line around it and space inside. margin.html works through all three — plus the two things that surprise everyone: negative margins, and margins that swallow each other.
<!-- урок 5, крок 2) посилання на решту сторінок сайту --> <hr> <h3> Сторінки сайту </h3> <a href="style.html"> Стиль </a> | <a href="style1.html"> Каскад </a> | <a href="link.html"> Посилання </a> | <a href="margin.html"> Поля й рамки </a>
Four separate exercises turn into a site at this step, and it costs nothing but a handful of a tags. Every page now carries a way home.
<!-- 4) margin трьома різними способами -->
<style type="text/css">
p.p1 {margin:30;background:#1b1b1b;color:#f0a500;font-size:20pt}
p.p2 {margin:5% 10% 2% 15%;background:#1b1b1b;color:#8bd450;font-size:20pt}
p.p3 {margin:5px 70px 10px;border:solid 70px #2b2b2b;background:#262626;color:#5bb4ff;font-size:20pt}
<!-- 4) margin --> <p class="p1"> Відстань між гравцями на карті вирішує все: занадто близько — одна граната забирає двох. </p> <p class="p2"> Занадто далеко — ніхто нікого не прикриває. </p> <p class="p3"> У сторінки те саме: margin розсуває блоки, padding дає повітря всередині. </p>
Margin is the empty space outside a block. One number means «the same on all four sides». Four numbers go clockwise from the top: top, right, bottom, left. A percentage is measured against the width of whatever holds the block.
Units are missing in the assignment's own sample — margin:30 rather than margin:30px. Browsers still accept it out of habit, but a number without a unit is an error in CSS; everything except a plain zero needs px, % or em.
<!-- 5) різні значення border --> <p style="margin:30;border:double 15px #f0a500;padding:10px;font-size:20;background:#1b1b1b;color:#e6e6e6"> AK-47 — автомат терористів, 2700 доларів. </p> <p style="margin-top:50;margin-bottom:70;margin-left:100;margin-right:5%;border-style:groove;border-width:20px;border-color:#8bd450;padding:10px;font-size:24;background:#1b1b1b;color:#e6e6e6"> AWP — снайперська гвинтівка, 4750 доларів. </p> <p style="margin:30 23% 20%;border-top:dashed 9px #f0a500;border-bottom:dotted #5bb4ff 10px;border-left:ridge #8bd450 20px;border-right:outset #ff7b5b 10px;padding:10px;font-size:24;background:#1b1b1b;color:#e6e6e6"> Чотири сторони рамки можна задати по-різному — як тут. </p>
A border takes three things: thickness, style and colour. The styles are solid, double, dashed, dotted, groove, ridge, outset — the last three fake a bevelled edge by lightening one side and darkening the other.
Each side can be set separately, which is what the third paragraph does: a different style on every edge.
<!-- 6) симетричне розташування абзаців полями --> <p style="margin:1% 67% 1% 2%;border:double 10px #f0a500;padding:2%;background:#1b1b1b;color:#e6e6e6"> Точка A </p> <p style="margin:1% 34% 1% 33%;border:double 10px #8bd450;padding:2%;background:#1b1b1b;color:#e6e6e6"> Мід </p> <p style="margin:1% 1% 1% 66%;border:double 10px #5bb4ff;padding:2%;background:#1b1b1b;color:#e6e6e6"> Точка B </p>
Three blocks, three positions — left, centre, right — and no positioning property in sight. Wide margins on one side push a block to the other.
This is how pages were laid out before CSS had tools for it. Today the same three boxes are a grid: display:grid; grid-template-columns:repeat(3,1fr) — and they stay side by side on a phone screen too, which this version does not.
<!-- 7) від'ємні значення margin --> <p style="margin:0 50% 1 10;border:groove 10px #f0a500;padding:2%;background:#1b1b1b;color:#e6e6e6"> Перший абзац стоїть на своєму місці. </p> <p style="margin:-30 10 1 50%;border:ridge 10px #8bd450;padding:2%;background:#1b1b1b;color:#e6e6e6"> Другий має від'ємне верхнє поле і наїжджає на попередній. </p> <p style="margin:-40 25 1 25;border:outset 10px #ff7b5b;padding:2%;background:#1b1b1b;color:#e6e6e6"> Третій наїжджає ще сильніше. </p>
A negative margin pulls a block towards its neighbour instead of pushing it away, and blocks are allowed to overlap. The preview shows paragraphs climbing on top of each other.
/* 8) один клас для двох різних тегів */
.frame {border:double #5bb4ff;padding:10px;color:#e8c08a;font-size:14px}
<!-- 8) один клас двом різним тегам --> <h2 class="frame"> Natus Vincere </h2> <hr class="frame" align="left" width="300" color="DarkGreen"> <p class="frame" align="center"> <i> Київська організація, заснована 2009 року. </i> </p>
A class is not tied to a tag. .frame — a dot and no tag name in front — matches anything carrying that label: a heading, a rule, a paragraph.
Compare it with lesson 2, where the selectors were written p.p1: that one only ever matched paragraphs.
<!-- 9) поглинання відступу двох сусідніх блоків --> <h3> Поглинання відступу </h3> <p style="margin:0 25% 10 25%;border:groove 10px #8bd450;padding:2%;background:#1b1b1b;color:#e6e6e6;font-size:22"> Нижнє поле 10 px </p> <p style="margin:5 25% 10 25%;border:ridge 10px #888;padding:2%;background:#1b1b1b;color:#e6e6e6;font-size:25"> Верхнє поле 5 px, нижнє 10 px — між цими двома абзацами буде не 15, а 10 </p> <p style="margin:0 25% 10 25%;border:outset 10px #5bb4ff;padding:2%;background:#1b1b1b;color:#e6e6e6;font-size:22"> Верхнє поле 0 px, нижнє 10 px </p> <p style="margin:-5 25% 0 25%;border:outset 10px #f0a500;padding:2%;background:#1b1b1b;color:#e6e6e6;font-size:25"> Верхнє поле -5 px </p>
Two blocks on top of each other: the first has 10px below it, the second 5px above. The gap between them is 10px, not 15 — the larger margin swallows the smaller one.
It looks like a bug and it is deliberate: without it, every paragraph in a text would add its neighbour's margin to its own and the spacing would grow all the way down the page.
Collapsing only happens vertically, only between block elements, and only when nothing stands between them — no border, no padding, nothing. It is the single most common reason behind «why is this gap not the size I asked for».