🎯 Session Overview
Welcome to Session 4! You know how to add colors and beautiful fonts to your text. But how do you control where things go on the screen? Why is your text touching the edge of the screen? How do you push things apart?
In this session, you will learn the absolute most important concept in all of web design: The CSS Box Model.
- Understand how every HTML element is treated as a rectangular "box".
- Learn to use Padding to create space inside an element.
- Learn to use Margin to create space outside an element.
- Learn how to style Borders.
- Understand the difference between Block and Inline elements.
⏪ Quick Recap: Session 3
Before we dive into layouts, let's remember our styling tools:
- We used
colorfor text andbackground-colorfor backgrounds. - We learned color formats: Names, HEX (
#ff0000), and RGBA for transparency. - We changed fonts using
font-familyand imported custom Google Fonts. - We made text readable using
font-size,font-weight, andline-height.
📦 The CSS Box Model
Here is the biggest secret in web development: Every single HTML element on a webpage is a rectangular box. Even if a button has perfectly round corners, the browser still treats it like a box.
The "Box Model" is made up of 4 layers, wrapped around each other like an onion. From the inside out, they are:
- Content: The actual text or image (blue).
- Padding: Transparent space inside the box, between the content and the border (green).
- Border: The wall of the box. It can be visible or invisible (yellow).
- Margin: Transparent space outside the box, pushing other elements away (orange).
Let's look at how to control each of these layers!
📏 Width, Height, and Padding
Width and Height
By default, a <div> will stretch to fill the whole width of the screen, and its height will only be as tall as the text inside it. We can change this using width and height.
.my-box {
width: 300px;
height: 200px;
background-color: blue;
}
Padding (The Bubble Wrap)
If you put text inside a box, it will touch the very edges of the box. This looks terrible! Padding adds "bubble wrap" around the content, pushing the border away from the text.
You can set padding on all sides at once, or target specific sides.
.my-box {
/* Adds 20 pixels of space inside the box on ALL 4 sides */
padding: 20px;
/* Or target specific sides: */
padding-top: 10px;
padding-left: 30px;
}
🚧 Borders and Margins
Borders (The Wall)
The border draws a line around your padding and content. To make a border visible, you must give it 3 values: width, style, and color.
.my-box {
/* width | style | color */
border: 2px solid white;
border-radius: 8px; /* Makes the corners round! */
}
Styles you can use: solid, dashed, dotted.
Margin (Personal Space)
What if you have two boxes, and they are touching each other? You need to push them apart! Margin creates invisible space outside the border.
.my-box {
/* Pushes other elements 30px away on ALL sides */
margin: 30px;
/* Or target specific sides: */
margin-bottom: 50px;
}
🎮 Interactive Demo: The Box Builder
Play with the sliders below to see how Padding, Border, and Margin affect the blue box inside the layout.
⚠️ The Box-Sizing Secret
Did you notice something weird in the interactive demo? When you added Padding or Border, the entire blue box got bigger!
By default, if you tell a box to be width: 200px, and then add padding: 20px, the browser adds them together. Your box is now 240px wide! This breaks website layouts all the time.
To fix this, modern developers ALWAYS add this magic rule at the top of their CSS file:
/* The * selector targets EVERY element on the page */
* {
box-sizing: border-box;
}
border-box forces the browser to squeeze the padding and border inward, so your box never grows larger than the width you set!
🧱 The Display Property
Why do headings (<h1>) force the next element onto a new line, but links (<a>) sit side-by-side in the same sentence? It's all because of the Display property.
- Block (e.g.,
div,h1,p): Takes up the full width available. Forces a line break before and after it. - Inline (e.g.,
span,a,strong): Only takes up as much width as necessary. Sits side-by-side. You cannot set width/height on inline elements! - Inline-Block: The best of both worlds. Sits side-by-side like inline, but allows you to set width, height, margin, and padding!
- None: Makes the element completely disappear.
🏋️♂️ Practice Exercises
Open your code editor. Create an HTML file with a <div class="box">Hello</div> and link a CSS file.
Exercise 1: Basic Dimensions
Target the .box class. Give it a background color of blue, a width of 300px, and a height of 150px.
.box { background-color: blue; width: 300px; height: 150px; }
Exercise 2: Inside and Out
The text is touching the edge! Add 20px of padding to the box. Then, add a white solid border that is 3px thick.
padding: 20px;
border: 3px solid white;
Exercise 3: Personal Space
Create a second <div class="box"> in your HTML so you have two boxes. They are probably touching. Add margin-bottom of 40px to your CSS to push them apart.
margin-bottom: 40px;
🔥 Mini Challenge: The Modern Button
Create an <a> link in your HTML that says "Click Me". We are going to make it look like a beautiful modern button.
In your CSS, target the a tag and apply these rules:
- Change
displaytoinline-block(so we can add padding). - Add
padding(10px on top/bottom, 20px on left/right). - Remove the underline using
text-decoration: none; - Add a background color, text color, and rounded corners (
border-radius).
🏠 Homework Project: Profile Card
Your task is to build a Profile Card component.
- Create a
<div class="profile-card">. - Inside it, put an
<img>(a profile picture), an<h2>(your name), and a<p>(your bio). - In CSS, style the
.profile-card: Give it a dark background, a border, 30px of padding, a specific width (like 300px), and rounded corners. - Make sure to include the
* { box-sizing: border-box; }rule at the top of your CSS!
📝 Session Summary
- The CSS Box Model dictates how every element takes up space: Content -> Padding -> Border -> Margin.
- Padding creates breathing room inside the box.
- Margin creates personal space outside the box to push other elements away.
- Borders require three values: width, style (solid/dashed), and color.
box-sizing: border-box;is a mandatory rule that stops padding and borders from making your elements wider than you want them to be.- Display:
blockelements stack.inlineelements flow like text.inline-blockflows like text but allows you to set width, height, and padding.