CHAPTER 12

CSS Box Model

Understand how margin, border, padding and content combine to create the size of an HTML element.

01. What is CSS Box Model?

Every HTML element is treated as a rectangular box by CSS.
The CSS Box Model consists of content, padding, border and margin.
Understanding the box model is essential for creating accurate layouts.

02. Syntax

selector { width: value; padding: value; border: value; margin: value; }

03. Example

CSS / HTML Example AIFA Coding Lab
<!DOCTYPE html>
<html>
<head>

<style>

.card {

    width: 300px;

    padding: 20px;

    border: 5px solid #2563eb;

    margin: 30px;

    background: #eff6ff;

}

</style>

</head>

<body>

<div class="card">

    <h2>AIFA Infotech</h2>

    <p>
        Learn Web Development.
    </p>

</div>

</body>
</html>

04. Important Points

✓ Content is the actual text or element area.
✓ Padding creates space between content and border.
✓ Border surrounds padding and content.
✓ Margin creates space outside the border.
✓ box-sizing: border-box makes sizing easier.

05. Common Mistakes

⚠ Confusing margin with padding.
⚠ Forgetting border size when calculating element dimensions.
⚠ Not using box-sizing when precise layouts are required.

06. Real World Use

The box model is used in cards, navigation bars, forms, buttons, sections and almost every website layout.

Ready to Practice? 🚀

Open the AIFA CSS Editor and practice this chapter live.

Try It Yourself →
Practice Exercise

Build a Box Model Card

Create a card using width, padding, border and margin. Give each part a different value.