The Box Model: Free HTML & CSS Tutorial
Master CSS Box Model for Professional Web Layouts
Core CSS Box Model Concepts
Width & Display Properties
Control how elements occupy space in the document flow. Set width percentages and max-width constraints for responsive design.
Margin & Padding Spacing
Add space outside (margin) and inside (padding) elements. Essential for creating proper visual hierarchy and readability.
Border Properties
Create visual boundaries between elements. Borders appear at the edge of elements, between padding and margin areas.
Setting Up Your CSS Structure
Add Style Tag
Insert a style tag in the head section of your HTML document to begin writing CSS rules.
Save and Preview
Save your file and keep it open in both code editor and browser for real-time testing.
Width vs Max-Width Behavior
| Feature | Width Only | Width + Max-Width |
|---|---|---|
| Narrow Windows | 90% of window | 90% of window |
| Wide Windows | 90% of window | Fixed 800px |
| Readability | Poor on wide screens | Optimal line length |
Typography Hierarchy Applied
Body Text
Set font-family to sans-serif on body element. All child elements inherit this styling automatically.
Main Heading (H1)
28px font size, orange color (#e45c09), and 36px line-height for proper spacing when text wraps.
Subheadings (H2)
18px font size with same orange color as H1 to maintain visual consistency throughout the page.
Setting font-family on the body tag allows all text-based children to inherit the styling without individual rules for each element.
Applying Margin Spacing
Paragraph Spacing
Add 22px margin-bottom to paragraphs for improved readability between text blocks.
Section Separation
Apply 30px margin-bottom to header, nav, and main elements for clear visual hierarchy.
Margin is space outside an element's box. For text, this is similar to space after or before a paragraph in design apps like Photoshop or InDesign.
Margin vs Padding Differences
| Feature | Margin | Padding |
|---|---|---|
| Location | Outside element box | Inside element box |
| Background Effect | Transparent space | Extends background |
| Border Relationship | Outside border | Inside border |
| Element Size | No size change | Increases element size |
For a complete visual guide illustrating the relationship between margin, border, padding, and content, refer to the Box Model reference section.
Key Takeaways
