TechTicket Logo
CSS Fundamentals and Modern Layouts

Session 4 — The CSS Box Model

🎯 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.

⏪ Quick Recap: Session 3

Before we dive into layouts, let's remember our styling tools:

📦 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:

  1. Content: The actual text or image (blue).
  2. Padding: Transparent space inside the box, between the content and the border (green).
  3. Border: The wall of the box. It can be visible or invisible (yellow).
  4. Margin: Transparent space outside the box, pushing other elements away (orange).
Margin (Outside Space) Border (The Wall) Padding (Inside Space) Content (Text/Image)

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.

Content

⚠️ 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.

display: block (takes full width) inline-block inline-block

🏋️‍♂️ 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:

  1. Change display to inline-block (so we can add padding).
  2. Add padding (10px on top/bottom, 20px on left/right).
  3. Remove the underline using text-decoration: none;
  4. Add a background color, text color, and rounded corners (border-radius).
a {
  display: inline-block;
  padding: 10px 20px;
  text-decoration: none;
  background-color: #06b6d4;
  color: white;
  border-radius: 8px;
}

🏠 Homework Project: Profile Card

Your task is to build a Profile Card component.

  1. Create a <div class="profile-card">.
  2. Inside it, put an <img> (a profile picture), an <h2> (your name), and a <p> (your bio).
  3. In CSS, style the .profile-card: Give it a dark background, a border, 30px of padding, a specific width (like 300px), and rounded corners.
  4. Make sure to include the * { box-sizing: border-box; } rule at the top of your CSS!

📝 Session Summary

⬅ Previous Session Next Session ➡