Lesson 1 Review
In our last class, we learned how to target HTML elements using CSS Selectors (Element, Class ., and ID #). Today, we look at how those elements physically take up space on the screen.
Step 1: Everything is a Box
In HTML and CSS, every single element is a rectangular box. Even if a picture looks perfectly round, the browser treats it as a square box. This concept is known as the CSS Box Model.
The Box Model consists of four distinct layers, wrapping around each other from the inside out:
- Content: The actual text or image.
- Padding: Clears an area inside the border, pushing the border away from the content. It inherits the content's background color.
- Border: A line that goes around the padding and content.
- Margin: Clears an area outside the border, pushing the entire box away from other boxes. It is completely transparent.
Step 2: Padding (Inner Space)
Padding makes elements look "breathable." Without padding, text touches the exact edge of its container, which looks unprofessional.
The CSS:
background-color: #4C8BF5;
color: white;
}
.good-pad {
background-color: #4C8BF5;
color: white;
padding: 20px; /* 20px cushion on all 4 sides */
}
Output:
Step 3: Border (The Wall)
To create a border, you must specify three values: width, style, and color.
The CSS:
/* Long way: */
border-width: 5px;
border-style: solid; /* Other options: dashed, dotted, double */
border-color: #2ecc71;
/* Professional Shorthand way: */
border: 5px solid #2ecc71;
}
Step 4: Margin (Outer Space)
Margin is used to push two completely different elements away from each other. If you have two paragraphs stacked on top of each other, margin creates the blank space between them.
The Shorthand Trick
You can set margin (and padding) for individual sides: margin-top, margin-right, margin-bottom, margin-left. But professionals use shorthand:
margin: 10px 30px; /* Top/Bottom get 10px, Left/Right get 30px */
margin: 10px 5px 15px 20px; /* Top, Right, Bottom, Left (Clockwise) */
width: 50%;), you can perfectly center it on the screen by setting margin: 0 auto;. The "auto" tells the browser to automatically split the remaining empty space equally on the left and right sides!
Step 5: The Broken Math Problem & The Fix
Here is where junior developers fail. Look at this CSS:
width: 300px;
padding: 25px;
border: 5px solid black;
}
The Answer: It is 360px, NOT 300px!
Math: 300px (Content) + 25px (Left Pad) + 25px (Right Pad) + 5px (Left Border) + 5px (Right Border) = 360px.
By default, browsers add padding and borders ON TOP of your requested width. This breaks layouts constantly. If you tell a box to be 100% wide, and add 20px of padding, it becomes 100% + 40px wide, and causes a horizontal scrollbar to appear!
The Modern Solution: box-sizing
To fix this, we force the browser to include the padding and border inside the width we asked for. We do this using the box-sizing property.
The Code:
* {
box-sizing: border-box;
}
width: 300px, the box will NEVER be wider than 300px. The browser will automatically shrink the inner content area to make room for your padding and borders.
Final Quiz: Test Your CSS Logic
Click the buttons below to verify your knowledge of the Box Model.
Class Dismissed!
You have now unlocked the core secret of Web Design. Every beautiful website you visit is simply a clever arrangement of rectangular boxes separated by margins and padding.
Next time, we will learn CSS Typography and how to import custom fonts from Google!
0 Comments