CSS Box Model, Colors & Typography
Introduction
Every element on a webpage is a rectangular box. Understanding the CSS Box Model is fundamental to controlling layout and spacing. This session also covers colors and typography - essential skills for making your pages visually appealing.
The CSS Box Model
The box model describes how every HTML element is rendered as a rectangular box with four layers:
┌─────────────────────────────────────────────────┐
│ MARGIN │
│ ┌─────────────────────────────────────────┐ │
│ │ BORDER │ │
│ │ ┌─────────────────────────────────┐ │ │
│ │ │ PADDING │ │ │
│ │ │ ┌─────────────────────────┐ │ │ │
│ │ │ │ │ │ │ │
│ │ │ │ CONTENT │ │ │ │
│ │ │ │ │ │ │ │
│ │ │ └─────────────────────────┘ │ │ │
│ │ │ │ │ │
│ │ └─────────────────────────────────┘ │ │
│ │ │ │
│ └─────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────┘From inside to outside:
- Content - The actual text, images, etc.
- Padding - Space inside the border, around the content
- Border - The edge of the element
- Margin - Space outside the border, between elements
Content
The content area holds your actual text, images, or other elements.
Setting Dimensions
.box {
width: 300px;
height: 200px;
}Important: By default, width and height set the size of the content area only. Padding and border are added on top of this!
.box {
width: 300px;
padding: 20px;
border: 5px solid black;
}
/* Total width = 300 + 20 + 20 + 5 + 5 = 350px! */We’ll fix this unexpected behavior later with box-sizing.
Padding
Padding creates space inside the element, between the content and the border.
.box {
padding: 20px;
}Key Characteristics
- Background color extends through padding
- Creates “breathing room” inside elements
- Values cannot be negative
Padding Shorthand
/* All four sides */
padding: 20px;
/* Top/bottom | Left/right */
padding: 10px 20px;
/* Top | Left/right | Bottom */
padding: 10px 20px 15px;
/* Top | Right | Bottom | Left (clockwise) */
padding: 10px 20px 15px 25px;Individual Sides
padding-top: 10px;
padding-right: 20px;
padding-bottom: 15px;
padding-left: 25px;Common Use Cases
Buttons:
.button {
padding: 10px 20px; /* More horizontal padding */
}Cards:
.card {
padding: 20px; /* Even spacing inside */
}Containers:
.container {
padding: 0 15px; /* Side padding only */
}Border
The border sits between padding and margin, creating a visible edge.
Border Properties
.box {
border-width: 2px;
border-style: solid;
border-color: black;
}Border Shorthand
.box {
border: 2px solid black;
/* width | style | color */
}Border Styles
| Style | Appearance |
|---|---|
solid | ─────────── |
dashed | - - - - - - |
dotted | · · · · · · |
double | ═══════════ |
none | (no border) |
Individual Sides
.box {
border-top: 2px solid red;
border-bottom: 1px dashed gray;
border-left: none;
border-right: none;
}Border Radius (Rounded Corners)
.box {
border-radius: 10px; /* All corners */
border-radius: 50%; /* Circle (with equal width/height) */
border-radius: 10px 20px; /* Top-left/bottom-right | Top-right/bottom-left */
}Tip: Use border-radius: 50% on a square element to create a circle:
.avatar {
width: 100px;
height: 100px;
border-radius: 50%; /* Perfect circle */
}Margin
Margin creates space outside the element, between it and other elements.
.box {
margin: 20px;
}Key Characteristics
- Margin is always transparent (background doesn’t show)
- Creates space between elements
- Can be negative (pulls elements closer)
Margin Shorthand
Same pattern as padding:
/* All four sides */
margin: 20px;
/* Top/bottom | Left/right */
margin: 10px 20px;
/* Top | Left/right | Bottom */
margin: 10px 20px 15px;
/* Top | Right | Bottom | Left */
margin: 10px 20px 15px 25px;Individual Sides
margin-top: 10px;
margin-right: 20px;
margin-bottom: 15px;
margin-left: 25px;Centering with Margin
To center a block element horizontally:
.centered {
width: 600px; /* Must have a width */
margin: 0 auto; /* auto on left and right */
}auto tells the browser to split the remaining space equally.
Margin Collapse
When two vertical margins touch, they collapse into a single margin (the larger one wins):
.box1 {
margin-bottom: 30px;
}
.box2 {
margin-top: 20px;
}
/* Space between them = 30px (not 50px!) */This only happens with vertical margins, not horizontal ones.
Padding vs Margin
| Aspect | Padding | Margin |
|---|---|---|
| Location | Inside border | Outside border |
| Background | Shows background color | Always transparent |
| Effect | Makes element larger | Creates space between elements |
| Negative values | Not allowed | Allowed |
| Collapse | Doesn’t collapse | Vertical margins collapse |
Analogies
Picture frame:
- Padding = The mat (colored border inside the frame)
- Margin = Wall space between frames
Personal space:
- Padding = Your coat’s thickness
- Margin = How far you stand from others
When to Use Which
Use padding when:
- You want background color to extend
- Creating space inside a button or card
- Adding breathing room around text
Use margin when:
- Separating elements from each other
- Centering block elements
- Creating gutters between items
The box-sizing Fix
The Problem
By default, width and height only set the content size. Padding and border are added to that:
.box {
width: 300px;
padding: 20px;
border: 5px solid black;
}
/* Actual width: 300 + 20 + 20 + 5 + 5 = 350px */This makes sizing unpredictable!
The Solution: border-box
.box {
box-sizing: border-box;
width: 300px;
padding: 20px;
border: 5px solid black;
}
/* Actual width: 300px (padding and border included) */With border-box, the width you set is the total width including padding and border.
Best Practice: Apply to Everything
Add this to the top of every CSS file:
*,
*::before,
*::after {
box-sizing: border-box;
}This applies border-box to all elements, making sizing intuitive.
Universal Reset
Many developers also reset default margins and padding:
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}This gives you a clean slate to work with.
Colors in CSS
CSS offers several ways to specify colors.
CSS has ~140 named colors:
color: red;
color: blue;
color: tomato;
color: rebeccapurple;
color: coral;
color: lightgray;Pros: Easy to remember Cons: Limited selection
Six-digit codes representing Red, Green, Blue values:
color: #ff0000; /* Red */
color: #00ff00; /* Green */
color: #0000ff; /* Blue */
color: #000000; /* Black (no color) */
color: #ffffff; /* White (all colors) */
color: #808080; /* Gray */Format: #RRGGBB where each pair is 00-FF (0-255 in hexadecimal)
Shorthand: When pairs repeat, you can shorten:
#FF0000 → #F00
#AABBCC → #ABCSpecify Red, Green, Blue as decimal values (0-255):
color: rgb(255, 0, 0); /* Red */
color: rgb(0, 255, 0); /* Green */
color: rgb(0, 0, 255); /* Blue */
color: rgb(128, 128, 128); /* Gray */Add an alpha channel for opacity (0 = transparent, 1 = solid):
color: rgba(255, 0, 0, 1); /* Solid red */
color: rgba(255, 0, 0, 0.5); /* 50% transparent red */
color: rgba(0, 0, 0, 0.3); /* 30% black (for shadows) */Color Comparison
| Format | Example | Best For |
|---|---|---|
| Named | tomato | Quick prototyping |
| Hex | #FF6347 | Most common, design tools |
| RGB | rgb(255, 99, 71) | When you need to calculate |
| RGBA | rgba(255, 99, 71, 0.5) | Transparency effects |
Color Tools
Don’t pick colors randomly - use tools:
- Coolors - Generate color palettes
- ColorHunt - Pre-made palettes
- Adobe Color - Advanced color wheel
- Browser DevTools - Built-in color picker (click any color value)
Typography
Typography is the art of styling text. Good typography dramatically improves readability and aesthetics.
font-family
Specifies which font to use:
body {
font-family: Arial, Helvetica, sans-serif;
}Always provide fallbacks in case the first font isn’t available.
Generic Font Families
| Family | Style | Example |
|---|---|---|
serif | Has decorative strokes | Times New Roman |
sans-serif | Clean, no strokes | Arial, Helvetica |
monospace | Fixed-width characters | Courier New |
Always end with a generic family as the final fallback:
font-family: "Roboto", Arial, sans-serif;Using Google Fonts
Google Fonts provides free, professional fonts.
Go to Google Fonts
Visit fonts.google.com
Select a font and weights
Choose a font and select weights (Regular 400, Bold 700, etc.)
Copy the link tag
Copy the <link> tag into your HTML <head>:
<head>
<!-- Google Font BEFORE your stylesheet -->
<link
href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap"
rel="stylesheet"
/>
<link
rel="stylesheet"
href="styles.css"
/>
</head>Use the font in CSS
body {
font-family: "Roboto", sans-serif;
}font-size
Sets the size of text:
p {
font-size: 16px; /* Pixels - fixed size */
font-size: 1rem; /* Relative to root - responsive */
font-size: 1.2em; /* Relative to parent - cascades */
}For now, use px. We’ll cover rem and em later.
Common sizes:
- Body text: 14-16px
- h1: 32-48px
- h2: 24-32px
- h3: 18-24px
font-weight
Controls how bold/light text appears:
p {
font-weight: normal; /* 400 */
font-weight: bold; /* 700 */
}
h1 {
font-weight: 300; /* Light */
font-weight: 400; /* Normal */
font-weight: 500; /* Medium */
font-weight: 600; /* Semi-bold */
font-weight: 700; /* Bold */
font-weight: 900; /* Black */
}font-style
p {
font-style: normal;
font-style: italic;
}line-height
Controls spacing between lines of text:
p {
line-height: 1.5; /* 1.5x the font size - recommended */
line-height: 24px; /* Fixed value */
line-height: 150%; /* Percentage of font size */
}Best practice: Use unitless values (1.4-1.6 for body text):
body {
line-height: 1.6; /* Good readability */
}letter-spacing
Space between characters:
h1 {
letter-spacing: 2px; /* Spread out */
letter-spacing: -0.5px; /* Tighter */
}text-transform
Changes capitalization:
.uppercase {
text-transform: uppercase; /* ALL CAPS */
}
.lowercase {
text-transform: lowercase; /* all lowercase */
}
.capitalize {
text-transform: capitalize; /* First Letter Of Each Word */
}text-decoration
Adds lines to text:
a {
text-decoration: none; /* Remove underline */
text-decoration: underline; /* Add underline */
text-decoration: line-through; /* Strikethrough */
}Complete Typography Example
body {
font-family: "Open Sans", Arial, sans-serif;
font-size: 16px;
line-height: 1.6;
color: #333;
}
h1 {
font-family: "Playfair Display", Georgia, serif;
font-size: 48px;
font-weight: 700;
letter-spacing: -1px;
line-height: 1.2;
}
.subtitle {
font-size: 18px;
font-weight: 300;
text-transform: uppercase;
letter-spacing: 3px;
color: #666;
}Using AI for CSS
AI tools can help you write CSS faster, but always verify and understand the output.
Good Prompt Example
Create CSS for a profile card component:
- 300px wide
- White background (#ffffff)
- 20px padding on all sides
- 10px border radius
- Subtle shadow: 0 2px 10px rgba(0, 0, 0, 0.1)
- Center it on the page
- Use 'Roboto' font
- Name should be 24px, bold, dark gray (#333)
- Title should be 14px, regular, medium gray (#666)AI Workflow
- Write a specific prompt with exact values
- Review every property in the output
- Test in browser
- Modify as needed
- Understand what each property does
Viewing the Box Model in DevTools
Browser DevTools are invaluable for understanding and debugging CSS.
To open: Press F12 or right-click → Inspect
Box model visualization:
- Select an element in the Elements panel
- Look for the “Computed” tab
- You’ll see a visual diagram of margin, border, padding, and content
Key Takeaways
- Every element is a box with content, padding, border, and margin
- Padding is inside the border (background shows through)
- Margin is outside the border (always transparent)
- Use
box-sizing: border-boxto make width/height include padding and border margin: 0 autocenters block elements horizontally- Vertical margins collapse (only the larger one applies)
- Colors: Use hex (
#RRGGBB) or RGB - always pick from a palette - Typography: Always include font fallbacks, use
line-height: 1.5for readability - Use browser DevTools to visualize and debug the box model
Additional Resources
Box Model
Colors
Typography
Tools
- Browser DevTools - Inspect box model, edit CSS live
- MDN Box Model Diagram - Visual reference