DEV Community

Ranjith srt
Ranjith srt

Posted on

Bootcamp 2 - CSS Box Model

The CSS Box Model is an important concept in web design because every HTML element on a webpage is represented as a rectangular box. The box model defines how the size of an element is calculated and how it is structured.

Image description

The CSS Box Model consists of four parts:
Enter fullscreen mode Exit fullscreen mode

1.Content (the innermost part)
2.Padding
3.Border
4.Margin (the outermost part)

Let’s break it down:

1. Content
Enter fullscreen mode Exit fullscreen mode

This is where the actual content of the element (like text or images) is displayed. It’s the core part of the box.

Width and height affect the content box directly.

Example:

html

<p>This is the content inside a box.</p>
css

p {
  width: 200px;
  height: 100px;
}
In this case, the content box will be 200px wide and 100px high.
Enter fullscreen mode Exit fullscreen mode
2. Padding :
Enter fullscreen mode Exit fullscreen mode

Padding is the space between the content and the border. It pushes the content inward, making space around it.

-Padding is inside the border and can be set individually for the top, right, bottom, and left sides.

Example:
css

p {
  padding: 20px; /* 20px of space inside the box, around the content */
}
You can also set padding for each side individually:
Enter fullscreen mode Exit fullscreen mode
css

p {
  padding-top: 10px;
  padding-right: 20px;
  padding-bottom: 15px;
  padding-left: 5px;
}
-The padding increases the size of the box but does not affect the space outside the element.

Enter fullscreen mode Exit fullscreen mode
3. Border :
Enter fullscreen mode Exit fullscreen mode

The border is the line that wraps around the padding and content. You can control the thickness, style, and color of the border.
-Border has three main properties: border-width, border-style, and border-color.

Example:
css
 p {
  border: 2px solid black; /* A solid black border of 2px thickness */
}

-You can also customize the border for each side:
Enter fullscreen mode Exit fullscreen mode
css
p {
  border-top: 3px dotted blue;   /* Top border */
  border-right: 2px solid red;   /* Right border */
  border-bottom: 5px dashed green; /* Bottom border */
  border-left: 4px double black; /* Left border */
}
Enter fullscreen mode Exit fullscreen mode
4. Margin :
Enter fullscreen mode Exit fullscreen mode

The margin is the space outside the border. It controls the spacing between this element and other elements on the page. It does not affect the size of the box itself.

-Just like padding, margins can be set for each side individually.

Example:
css
 p {
  margin: 30px; /* Adds 30px of space outside the box on all sides */
}
-You can also set different margins for each side:
Enter fullscreen mode Exit fullscreen mode
css

p {
  margin-top: 20px;
  margin-right: 10px;
  margin-bottom: 30px;
  margin-left: 15px;
}
-Important: Margins can collapse. If two elements with margins touch each other, the larger margin between them will apply (not the sum of both margins).
Enter fullscreen mode Exit fullscreen mode
Visual Representation of the Box Model:
Enter fullscreen mode Exit fullscreen mode

Margin
Border

Padding
Content (Text/Image)

Example Combining All Box Model Properties:
html

<!DOCTYPE html>
<html>
<head>
  <style>
    div {
      width: 200px;
      height: 100px;
      padding: 20px;
      border: 5px solid blue;
      margin: 30px;
      background-color: lightgray;
    }
  </style>
</head>
<body>

  <div>This is a box with padding, border, and margin.</div>

</body>
</html>
Enter fullscreen mode Exit fullscreen mode

1.Content: The width and height of the content area are 200px and 100px.
2.Padding: Adds 20px of space inside the box around the content.
3.Border: Adds a 5px blue solid line around the box.
4.Margin: Adds 30px of space outside the box, separating it from other elements.

Box Model Calculation:
Enter fullscreen mode Exit fullscreen mode

By default, the width and height you specify in CSS apply only to the content. To get the total size of the element, you must add the padding, border, and margin to the width and height.

For example:

1.Width: 200px (content width)
2.Padding: 20px on both sides (left + right = 40px)
3.Border: 5px on both sides (left + right = 10px)
4.Total Width: 200px + 40px (padding) + 10px (border) = 250px
Enter fullscreen mode Exit fullscreen mode
Box-Sizing Property:
Enter fullscreen mode Exit fullscreen mode

By default, CSS uses the content-box model, which means the width and height apply to the content only. However, you can change this using the box-sizing property.

1.content-box (default): Width and height include only the content. Padding and border are added on top of the specified width/height.

2.border-box: Width and height include the padding and border, so the overall size of the box remains the same.

Example:
css

div {
  box-sizing: border-box;  /* Now width/height include the padding and border */
  width: 200px;
  padding: 20px;
  border: 5px solid blue;
}

Now the total width remains 200px even with padding and border, because the box-sizing is set to border-box.
Enter fullscreen mode Exit fullscreen mode

Key Points to Remember:

1.Content: The main area where text or images are placed.
2.Padding: Space inside the element between the content and the border.
3.Border: The line around the padding and content.
4.Margin: Space outside the element, separating it from other elements.
5.Box-sizing: Controls how padding and borders affect the total width/height of an element.
Enter fullscreen mode Exit fullscreen mode

Top comments (0)