Lesson 1 ยท Foundations

๐Ÿงฑ HTML, CSS & the Box Model

Every web page is built from two layers working together: HTML describes what the content is, and CSS describes how it looks. Getting the box model right underneath both saves hours of later confusion.

๐Ÿงฑ Semantic HTML

Semantic tags describe what content is, not just how it looks โ€” <nav>, <header>, <main>, <article>, <button> โ€” instead of a page built entirely out of generic <div>s.

<!-- Less useful --> <div class="btn" onclick="submit()">Save</div> <!-- Semantic --> <button type="submit">Save</button>
Why it matters. Screen readers, search engines, and keyboard navigation all rely on semantic meaning. A <div> styled to look like a button gets none of a real <button>'s built-in keyboard and accessibility behaviour for free.

🖼️ See it

The CSS box model, from the inside outContentPaddingBorderMargin

🖼️ See it

Specificity, lowest to highestSelector typeExampleSpecificityTagp { }LowestClass.lead { }MediumID#intro { }HighInline style /!importantstyle="..."Highest

๐ŸŽฏ Selectors and specificity

CSS rules can target elements by tag, class, or ID, and more than one rule can match the same element. When rules conflict, specificity decides which one wins: an ID beats a class, a class beats a bare tag, and !important overrides almost everything (and should be used rarely).

p { color: black; } /* specificity: 1 (tag) */ .lead { color: blue; } /* specificity: 10 (class) */ #intro { color: red; } /* specificity: 100 (ID) */ <p id="intro" class="lead">Which color wins?</p> <!-- red: the ID rule -->
Common mistake. Reaching for !important to "win" a style fight instead of fixing the actual specificity or source-order conflict โ€” it tends to spread, since the next override needs !important too.

๐Ÿ“ฆ The box model

Every element is a box made of content, padding (space inside the border), border, and margin (space outside the border). box-sizing: border-box makes width include padding and border, which is almost always what you actually want.

.card { width: 200px; padding: 16px; border: 2px solid #ccc; box-sizing: border-box; /* total rendered width stays 200px */ } /* without border-box, the rendered width would be 200 + 32 + 4 = 236px */

๐Ÿชœ Step by step: working out a boxโ€™s width

With content-box, padding and border add to the width; with border-box they fit inside it.

Problem: A div has width: 300px; padding: 20px; border: 5px solid. How wide is it on screen with content-box and with border-box?

  1. content-box.
    300 + 2ร—20 + 2ร—5 = 350px
  2. border-box.
    300px total; content = 300 โˆ’ 40 โˆ’ 10 = 250px
  3. Choose. Most projects set * { box-sizing: border-box; } so widths are predictable.

Answer: 350px with content-box; 300px with border-box.

Common mistake: forgetting padding and border are on both sides, so adding them only once.
✏️ Quick check · 1 of 8
Semantics

Using a real <button> instead of a styled <div onclick=...> mainly gains:

What does a browser already know how to do with a real <button>?
✏️ Quick check · 2 of 8
Specificity

Given "p { color: black; }" and ".lead { color: blue; }" on the same <p class="lead">, the text will be:

Which selector type ranks higher: tag or class?
✏️ Quick check · 3 of 8
Specificity

An element matches both "#intro { color: red; }" and ".lead { color: blue; }". The color shown will be:

ID vs class: which wins?
✏️ Quick check · 4 of 8
!important

Relying heavily on !important to resolve style conflicts tends to cause:

What happens when the next rule also needs to beat an !important rule?
✏️ Quick check · 5 of 8
Box model

With box-sizing: border-box and width: 200px, padding: 20px, border: 5px solid, the element's total rendered width is:

What does border-box change about how width is measured?
✏️ Quick check · 6 of 8
Box model

Without box-sizing: border-box, setting width: 200px with 20px padding and a 5px border results in a rendered width of:

Padding and border apply to both sides of the box.
✍️ Type your answer · 7 of 8

A box has content width 150px, 10px padding on each side, and a 5px border on each side, using the default (content-box) box-sizing.

What is the total rendered width in pixels?

Enter a whole number.
Add padding and border on both sides to the content width.
✍️ Type your answer · 8 of 8

A page has 4 CSS rules targeting the same paragraph: 1 by tag, 1 by class, 1 by ID, and 1 inline style.

Ignoring !important, which single one of these 4 wins (count it as rule number 4, the inline style, being the most specific)?

Enter a whole number.
Rank them from least to most specific and take the top one.
📝 Lesson quiz · 5 questions
0 / 5
Re-read the lesson, then give it another go.
Question 1 of 5

Semantic HTML tags describe:

Question 2 of 5

An ID selector, compared to a class selector, has:

Question 3 of 5

box-sizing: border-box makes an element's width include:

Question 4 of 5

Margin, in the box model, is:

Question 5 of 5

Overusing !important tends to: