Learning Objectives
By the end of this session, you will be able to:
box-sizing.@font-face and Google Fonts.Aligned to: CO2 — Design and develop well-structured, standards-compliant web pages using HTML5 and CSS3 following accessibility and web standards guidelines.
Quick Recap: CSS Syntax & How We Apply It
CSS = Cascading Style Sheets — it separates presentation (how a page looks) from structure (HTML)
selector {
property: value;
property: value;
}
p { color: navy; }
| Inline | style="color:red;" — Highest priority. Avoid for maintainability. |
|---|---|
| Internal | <style> in <head> — Fine for single-page demos. |
| External | <link rel="stylesheet"> — Best practice: cacheable & reusable across pages. |
.css file linked from <head> — it separates concerns and lets the browser cache it.
External stylesheet — try it live
This box simulates a real project: the left pane is your HTML, the right pane is a separate styles.css "file". Change color below (try purple or #2f9e5c), then click Run.
styles.css would be its own file on disk, linked with <link rel="stylesheet" href="styles.css"> in <head> — exactly like css3-fundamentals.html links to lesson-style.css. Both panes here just let you edit HTML and CSS separately, the same way you would across two real files, without leaving the browser.
01 · Selectors & Specificity
How CSS targets elements — and how conflicts get resolved
Basic Selector Types
The five ways to target elements before you add relationships or state
* Universal | * { margin: 0; } — targets every element |
|---|---|
E Type / Element | p { line-height: 1.6; } — targets every element of that tag |
. Class | .card { padding: 1rem; } — targets every element with that class |
# ID | #main-nav { ... } — targets the one element with that ID |
, Grouping | h1, h2, h3 { font-family: ...; } — applies one rule to several selectors |
Combinators — Selecting by Relationship
Target an element based on its position relative to another
A B Descendant | .card p { } — any B inside A, at any depth |
|---|---|
A > B Child | .card > p { } — B is a direct child of A |
A + B Adjacent sibling | h2 + p { } — B immediately after A |
A ~ B General sibling | h2 ~ p { } — any B after A, same parent |
Attribute Selectors & Pseudo-Classes
Target elements by their attributes, or by the state they're currently in
Attribute selectors
[type="text"] | input[type="text"] { } |
|---|---|
[href^="https"] | a[href^="https"] { } — ^= starts with |
[href$=".pdf"] | a[href$=".pdf"] { } — $= ends with |
[alt] | img[alt] { } — has the attribute at all |
Pseudo-classes — element in a state
:hover | a:hover { color: orange; } |
|---|---|
:focus | input:focus { outline: 2px solid; } |
:first-child | li:first-child { margin-top: 0; } |
:last-child | li:last-child { margin-bottom: 0; } |
:nth-child(2n) | tr:nth-child(2n) { background: #f5f5f5; } |
:not(...) | button:not(.disabled) { cursor: pointer; } |
Pseudo-Elements — Styling a Part of an Element
::before and ::after insert generated content before/after an element's content — icons, quote marks, clearfix hacks
Real-world use: decorative quote marks that aren't part of the actual text (so screen readers and copy/paste skip them)
::first-line and ::first-letter are also common — for editorial / magazine-style typography.
Who Wins? The Specificity Hierarchy
When two rules target the same element, the browser scores each selector and the higher score wins
| 1000 | Inline style — style="color:red;" |
|---|---|
| 100 | ID selector — #header { } |
| 10 | Class / Attribute / Pseudo-class — .btn, [type], :hover |
| 1 | Element / Pseudo-element — p, ::before |
Worked example
#nav .item a:hover { color: orange; }
#nav | 1 ID → +100 |
|---|---|
.item | 1 class → +10 |
:hover | 1 pseudo-class → +10 |
a | 1 element → +1 |
Total specificity = 100 + 10 + 10 + 1 = 121
!important overrides normal specificity entirely — powerful, but makes styles hard to override later. Use sparingly.
02 · The Cascade
How the browser picks a winner when rules conflict
Order of Precedence When Rules Conflict
!important > author !important > author styles > user styles > browser default#winner rule, or add !important — then click Run and watch which color wins change with it.
Inheritance & Special Values
Some properties pass down from parent to child automatically — others don't
Inherited by default
color, font-family, font-size, line-height, text-align, visibility
Not inherited by default
margin, padding, border, background, width, height
inherit | Force this property to take its parent's computed value |
|---|---|
initial | Reset the property to the CSS spec's default value |
unset | Acts as inherit if the property normally inherits, else initial |
revert | Reset to the browser's built-in (user-agent) stylesheet value |
03 · The Box Model
Every element is a box: content, padding, border, margin
Content → Padding → Border → Margin
| Content | width / height — the actual text, image, etc. |
|---|---|
| Padding | Space between content and the border |
| Border | The line around the padding (width, style, color) |
| Margin | Space between this box and neighboring elements |
box-sizing: content-box vs border-box
| content-box (default) | width: 300px applies to content only — rendered width = 300 + padding + border |
|---|---|
| border-box (recommended) | width: 300px includes padding + border — rendered width stays 300px, padding/border eat into the content area instead |
*, *::before, *::after { box-sizing: border-box; } /* common reset — predictable sizing */
Margin Collapsing & Inspecting the Box Model
Margin collapsing
When two block elements stack vertically, their top/bottom margins can overlap and collapse into a single margin — equal to the larger of the two, not the sum.
04 · Colors
Five ways to describe the same color in CSS
CSS Color Formats
| Keyword | steelblue |
|---|---|
| HEX | #4682B4 |
| RGB | rgb(70, 130, 180) |
| RGBA | rgba(70, 130, 180, 0.6) — with alpha (transparency) |
| HSL | hsl(207, 44%, 49%) |
opacity affects the whole element + children; the alpha channel (RGBA/HSLA) only affects that color.
05 · Typography
Font families, sizing units, and text styling
Font Families & Font Stacks
font-family: "Poppins", "Segoe UI", Arial, sans-serif;
| Serif | Times New Roman, Georgia |
|---|---|
| Sans-serif | Arial, Helvetica, Calibri |
| Monospace | Courier New, Consolas |
Sizing Units: px, %, em, rem
px | Fixed pixel value — doesn't scale with user/browser settings. e.g. 16px |
|---|---|
% | Relative to the parent element's font-size. e.g. 150% |
em | Relative to the current element's (or parent's) font-size — compounds when nested. e.g. 1.5em |
rem | Relative to the root (<html>) font-size — predictable, recommended for accessibility. e.g. 1.5rem |
Core Text Styling Properties
font-weight | normal | bold | 100–900 |
|---|---|
font-style | normal | italic |
line-height | 1.5 (unitless preferred) |
letter-spacing | 0.5px |
text-align | left | center | right | justify |
text-decoration | underline | line-through | none |
text-transform | uppercase | capitalize |
text-indent | 2em |
06 · Web Fonts
Going beyond web-safe fonts with @font-face and Google Fonts
@font-face & Google Fonts
Self-hosted with @font-face |
@font-face {
font-family: "Poppins";
src: url("poppins.woff2")
format("woff2");
font-weight: 400;
font-display: swap;
} |
|---|---|
| Hosted via Google Fonts | <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Poppins&display=swap"> body { font-family: "Poppins", sans-serif; } |
font-display: swap shows a fallback font immediately, then swaps once the custom font loads — avoids invisible text while waiting.
07 · Backgrounds
Color, images, positioning, and gradients
Background Properties
background-color | #f5f5f5 |
|---|---|
background-image | url("bg.jpg") |
background-repeat | no-repeat | repeat-x |
background-position | center | 20px 40px |
background-size | cover | contain |
background-attachment | fixed | scroll |
/* shorthand */ background: #222 url(bg.jpg) no-repeat center / cover;
08 · Borders
Widths, styles, colors, and rounded corners
Border Styles & border-radius
border: 2px solid #1572B6; border-top: 4px dashed #e8622c;
border-radius: 50% on a square element makes a perfect circle — the classic technique for circular avatars.
09 · Shadows
Adding depth with box-shadow and text-shadow
box-shadow & text-shadow
box-shadow: 0 4px 12px rgba(0,0,0,.25); /* x y blur spread color */ text-shadow: 2px 2px 4px rgba(0,0,0,.5);
inset for a pressed-in look.
Laboratory Activity
Style a Profile Card · contact styles.css · 20 points
styles.css):hover)box-sizing: border-box globally@font-face to headingsborder-radiusbox-shadow to the card and text-shadow to the name| 4 pts | Selectors & Specificity |
|---|---|
| 3 pts | Box Model |
| 3 pts | Colors |
| 4 pts | Typography / Web Fonts |
| 2 pts | Backgrounds |
| 2 pts | Borders |
| 2 pts | Shadows |
styles.css linked from your profile card HTML · Total: 20 points
Key Takeaways
CSS3 Fundamentals: Selectors · Specificity · Cascade · Box Model · Colors · Typography · Web Fonts · Backgrounds · Borders · Shadows
- CSS separates presentation from structure — external stylesheets are best practice.
- The cascade resolves conflicts using importance, specificity, then source order.
- The box model (content, padding, border, margin) underlies all CSS layout.
- Consistent color, type, and spacing choices are what make an interface feel professional.
- These fundamentals are the foundation for the next lesson: Flexbox, Grid, and positioning.
References
Course resources for this topic
| MDN Web Docs | CSS Reference — developer.mozilla.org/en-US/docs/Web/CSS |
|---|---|
| CSS-Tricks | css-tricks.com |
| W3Schools | CSS Tutorial — w3schools.com/css |
| Duckett, J. (2014) | HTML & CSS: Design and Build Websites. Wiley. |
| Freeman & Robson (2020) | Head First HTML and CSS (2nd ed.). O'Reilly. |
| Robbins, J. N. (2018) | Learning Web Design (5th ed.). O'Reilly. |