CSS Box Model Properties – Explained With Examples ✨ (2024)

Today we're gonna learn how to use the CSS box model with examples. This will help you make pixel perfect websites and will teach you to use the box-sizing, margin, padding, and border properties more accurately.

We're also going to see some practical use cases for these properties. Let's get started 💖

Table of Contents

  • Why learn CSS Box Model?
  • CSS Box-Model Diagram
  • The Padding Property
  • The Border Property
  • The Margin Property
  • The box-sizing Property.
  • Content-box VS Border-box
CSS Box Model Properties – Explained With Examples ✨ (1)

You can watch this tutorial on YouTube as well if you like:

Why learn CSS Box Model?

CSS Box Model Properties – Explained With Examples ✨ (2)

The CSS box model comprises the box-sizing, padding and margin properties. If you don't use them, your website will look like this 👇

CSS Box Model Properties – Explained With Examples ✨ (3)

But if you use the box model properties correctly, your website will look like this 👇

CSS Box Model Properties – Explained With Examples ✨ (4)

Much more visually appealing, right? If you want to make your website with accurate calculations, like the one above 👆 then this topic is for you. Learning about the CSS box model is one of many ways that will help you make pixel perfect websites.

This article will talk about how to use these properties:

  • Padding
  • Margin
  • Border
  • box-sizing

How to Use CSS box-model Properties

Let's look at some examples of where we can use the properties of the CSS box-model. We're gonna dissect the website shown above. 👆

Let's have a closer look at the navbar. You can notice the difference between the example that uses the padding property and the one that doesn't:

CSS Box Model Properties – Explained With Examples ✨ (5)

Now let's have a closer look to the content section along with the buttons. Again, you'll notice the difference – the right one is also using the padding property.

CSS Box Model Properties – Explained With Examples ✨ (6)

CSS Box-Model Diagram

Think of the CSS box-model like an onion. It has 4 Layers:

  • 1st layer: Content
  • 2nd layer: Padding
  • 3rd layer: Border
  • 4th layer: Margin

1st box-model layer: Content

In HTML, everything behaves like a box. Let's insert some content with a kitty image. 👇

CSS Box Model Properties – Explained With Examples ✨ (7)

2nd box-model layer: Padding

The next layer of the CSS box model is the padding layer. It wraps our content like this 👇

CSS Box Model Properties – Explained With Examples ✨ (8)

3rd box-model layer: Border

The next layer of the CSS box model is the border layer. It wraps our content + padding like this 👇

CSS Box Model Properties – Explained With Examples ✨ (9)

4th box-model layer: Margin

The next and final layer of the CSS box model is the margin layer. It wraps our content + padding + border like this 👇

CSS Box Model Properties – Explained With Examples ✨ (10)

Alright, let's see how these properties work in a project.

How to Setup the Project

CSS Box Model Properties – Explained With Examples ✨ (11)

This tutorial is good for everyone including beginners. If you want to code along, then follow these steps.

HTML

Open VS Code or Codepen.io and write this code 👇 inside the body tag:

<div class="box-1"> Box-1 </div>

CSS

Clear the default styles of our browser 👇

* { margin: 0px; padding: 0px; font-family: sans-serif;}

Now, let's style our box 👇

.box-1 { width: 300px; background-color: skyblue; font-size: 50px;}

We're all set, let's start coding! ✨

CSS Box Model Properties – Explained With Examples ✨ (12)

The Padding Property

But first, let's discuss the practical uses of the padding property. Then, we'll see how to use this property.

Generally, I use padding to put some space between contents. Look at this navbar 👇

CSS Box Model Properties – Explained With Examples ✨ (13)

Here's another example for you – look at the below content, with two buttons👇

CSS Box Model Properties – Explained With Examples ✨ (14)

How to use the padding property in CSS

This is the shorthand of the four padding properties:

  • padding-top
  • padding-right
  • padding-bottom
  • padding-left
CSS Box Model Properties – Explained With Examples ✨ (15)

And remember, padding is the space you add on top of your main content:

CSS Box Model Properties – Explained With Examples ✨ (16)

Let's add some padding to our content. The red colored area is the padding 👇

CSS Box Model Properties – Explained With Examples ✨ (17)

In order to recreate the results above, ☝ write this code in your CSS: 👇

// Padding added on top, right, left, bottom of .box-1.box-1{ padding : 100px;}

Let's open our developer console and go to the computed section:

CSS Box Model Properties – Explained With Examples ✨ (18)

At the very middle is our content which is 300px in width. Look around our content, we have added 100px padding all around it.

Let's try adding padding to just 1 side of our content (only the right side):

CSS Box Model Properties – Explained With Examples ✨ (19)

In order to recreate the results above, ☝ write this code in your CSS: 👇

.box-1{ padding: 0 100px 0 0;}// Or you can use 👇.box-1{ padding-right: 100px;}

Now, open the computed section on your developer console 👇

CSS Box Model Properties – Explained With Examples ✨ (20)

Look – the padding of 100px has only been added on the right side of our content as we specified.

The Border Property

You'll commonly use the border property while making buttons. Here's a GIF demo 👇

CSS Box Model Properties – Explained With Examples ✨ (21)

Notice how a white colored border shows around the button when I hover the mouse over the button.

How to use the border property in CSS

And remember, the border is the space added on top of our main content + padding: 👇

CSS Box Model Properties – Explained With Examples ✨ (22)

There are three crucial inputs of the border property:

  • border size
  • border style : solid / dotted/ dashed
  • border color
CSS Box Model Properties – Explained With Examples ✨ (23)

There are three styles of border property as I listed above. In this example, we'll use the dashed style:

CSS Box Model Properties – Explained With Examples ✨ (24)

To recreate the results above, write this code in your CSS: 👇

.box-1 { width: 300px; font-size: 50px; padding: 50px; border: 10px dashed black;}

Let's open our console and see the box model calculations:

CSS Box Model Properties – Explained With Examples ✨ (25)

Now look at the above image☝ – a 10px border is added all around our content + padding.

The Margin Property

Generally, I use the margin property to put some whitespace between my content and the main screen on the desktop layout (large screens). Look at this GIF: 👇

CSS Box Model Properties – Explained With Examples ✨ (26)

Notice that I'm adding the margin to the left and right edges of my website above 👆

Here's another sample GIF of a use case of the margin property: 👇

CSS Box Model Properties – Explained With Examples ✨ (27)

How to use margin property in CSS

This is the shorthand for the four properties of the margin property:

  • margin-top
  • margin-right
  • margin-bottom
  • margin-left
CSS Box Model Properties – Explained With Examples ✨ (28)

And remember, margin is the space added on top of our main content + padding + border:

CSS Box Model Properties – Explained With Examples ✨ (29)

Let's add a margin to our content. The content is getting pushed due to the Margin in this GIF:👇

CSS Box Model Properties – Explained With Examples ✨ (30)

To recreate the results above, write this code in your CSS: 👇

.box-1 { padding: 50px; border: 10px dashed black; margin: 50px;}

We can check the calculations again: 👇

CSS Box Model Properties – Explained With Examples ✨ (31)

Look, a 50px margin has been added all around our content + padding + border.

Let's try adding a margin to just 1 side of our content (only the left side):

CSS Box Model Properties – Explained With Examples ✨ (32)

To recreate the results above, write this code in your CSS 👇

.box-1 { padding: 50px; border: 10px dashed black; margin-left: 50px;}

On the console, we can see that a 50px margin got applied only on the left side 👇

CSS Box Model Properties – Explained With Examples ✨ (33)

Take a Break!

So far so good – take a break! You deserve it 💖

CSS Box Model Properties – Explained With Examples ✨ (34)

The Box-sizing Property

This property defines how our margin, padding, and borders will be calculated. There are three types of calculations (you can call them values):

  • border-box
  • padding-box
  • content box

Note:

We're not gonna discus box-sizing: padding-box, as only Firefox supports it and it isn't used very often.

What is the difference between content-box and border-box in CSS?

Both border-box and content-box work in the same way. Look at these images:👇

CSS Box Model Properties – Explained With Examples ✨ (35)
CSS Box Model Properties – Explained With Examples ✨ (36)

So, what's the main difference here? The difference is noticeable when we are adding margin, border, or padding to our boxes.

When we are using the box-sizing: content-box, which is the default value, it will add a margin, padding, and borders outside the box, like this: 👇

CSS Box Model Properties – Explained With Examples ✨ (37)

You can see the calculations here as well: 👇

CSS Box Model Properties – Explained With Examples ✨ (38)

Which means that things can get out of control and you'll see unexpected calculations. This means that it's hard to make responsive websites. Always use the box-sizing: border-box property instead.

But when we are using the box-sizing: border-box property, it will add a margin, padding, and borders inside the box, like this:👇

CSS Box Model Properties – Explained With Examples ✨ (39)

The box-sizing: border-box property shows us the EXACT calculations of our HTML elements, which means that this value is ideal for making responsive websites.

You can experiment with the values as well – just follow along with this code: 👇

* { box-sizing: border-box;}/* Or, Write 👇 */* { box-sizing: content-box;}

Conclusion

Congratulations! You can now make pixel perfect websites. Not only that, but when you're coding, you can figure out why your content is behaving strangely.

Here's your medal for reading till the end ❤️

Suggestions & Criticisms Are Highly Appreciated ❤️

CSS Box Model Properties – Explained With Examples ✨ (40)

YouTube / Joy Shaheb

LinkedIn / JoyShaheb

Twitter / JoyShaheb

Instagram / JoyShaheb

Credits

CSS Box Model Properties – Explained With Examples ✨ (2024)
Top Articles
Latest Posts
Article information

Author: Kerri Lueilwitz

Last Updated:

Views: 6101

Rating: 4.7 / 5 (67 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Kerri Lueilwitz

Birthday: 1992-10-31

Address: Suite 878 3699 Chantelle Roads, Colebury, NC 68599

Phone: +6111989609516

Job: Chief Farming Manager

Hobby: Mycology, Stone skipping, Dowsing, Whittling, Taxidermy, Sand art, Roller skating

Introduction: My name is Kerri Lueilwitz, I am a courageous, gentle, quaint, thankful, outstanding, brave, vast person who loves writing and wants to share my knowledge and understanding with you.