CSS3 Box Sizing

By Codes With Pankaj


CSS3 Box Sizing is a property that allows you to control how the size of an element is calculated. It defines whether the padding and border of an element are included in the element's width and height. Understanding and using box-sizing properly can simplify layout creation and prevent common issues related to sizing elements. This tutorial will explain how box-sizing works, how to use it, and provide practical examples.

Table of Contents

  1. What is Box Sizing?

  2. How Box Sizing Works

  3. Understanding the content-box Model

  4. Understanding the border-box Model

  5. Practical Examples

    • Example 1: Default content-box Behavior

    • Example 2: Using border-box for Consistent Sizing

    • Example 3: Applying box-sizing Globally

  6. Best Practices for Using Box Sizing

  7. Cross-Browser Compatibility

  8. Conclusion


1. What is Box Sizing?

box-sizing is a CSS property that determines how the total width and height of an element are calculated. By default, the width and height of an element apply only to the content box, not including padding, border, or margin. The box-sizing property allows you to include padding and border in the width and height, which can simplify layout calculations and prevent overflow issues.

Key Concept:

  • Box Sizing: A CSS property that controls how the total size of an element is calculated.

Example:

.element {
  box-sizing: border-box;
}

Explanation:

  • This example uses the border-box model, where the element's width and height include padding and border.


2. How Box Sizing Works

The box-sizing property can have two main values:

  1. content-box (default): The width and height properties apply only to the content of the element. Padding and border are added outside the content box, increasing the overall size of the element.

  2. border-box: The width and height properties include the content, padding, and border, so the total size of the element remains consistent.

Basic Syntax:

selector {
  box-sizing: value;
}

Example:

.element {
  width: 300px;
  padding: 20px;
  border: 5px solid #333;
  box-sizing: border-box;
}

Explanation:

  • With box-sizing: border-box;, the element's total width remains 300px, including the padding and border.


3. Understanding the content-box Model

The content-box model is the default value for box-sizing. When using content-box, the width and height properties apply only to the content, excluding padding and border. This means that the actual size of the element will be larger than the specified width and height if padding or borders are added.

Example:

.element {
  width: 300px;
  padding: 20px;
  border: 5px solid #333;
  box-sizing: content-box; /* Default behavior */
}

Explanation:

  • The content width is 300px, but the total width of the element is 350px (300px content + 20px padding on each side + 5px border on each side).


4. Understanding the border-box Model

The border-box model includes padding and border within the specified width and height. This approach simplifies the calculation of the element's size, as the total size remains consistent regardless of padding or border.

Example:

.element {
  width: 300px;
  padding: 20px;
  border: 5px solid #333;
  box-sizing: border-box;
}

Explanation:

  • The total width of the element remains 300px, with the content area adjusted to accommodate the padding and border.


5. Practical Examples

Let's explore some practical examples of using box-sizing.

Example 1: Default content-box Behavior

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Default Content Box - codeswithpankaj</title>
  <style>
    .box {
      width: 300px;
      padding: 20px;
      border: 5px solid #333;
      background-color: lightblue;
      box-sizing: content-box; /* Default behavior */
    }
  </style>
</head>
<body>

<div class="box">Content Box</div>

</body>
</html>

Explanation:

  • The total width of the box is 350px (300px content + 20px padding on each side + 5px border on each side).

Example 2: Using border-box for Consistent Sizing

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Border Box - codeswithpankaj</title>
  <style>
    .box {
      width: 300px;
      padding: 20px;
      border: 5px solid #333;
      background-color: lightcoral;
      box-sizing: border-box;
    }
  </style>
</head>
<body>

<div class="box">Border Box</div>

</body>
</html>

Explanation:

  • The total width of the box remains 300px, with the content width adjusted to fit within this size.

Example 3: Applying box-sizing Globally

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Global Box Sizing - codeswithpankaj</title>
  <style>
    * {
      box-sizing: border-box;
    }

    .box {
      width: 300px;
      padding: 20px;
      border: 5px solid #333;
      background-color: lightgreen;
    }
  </style>
</head>
<body>

<div class="box">Global Border Box</div>

</body>
</html>

Explanation:

  • Applying box-sizing: border-box; globally ensures consistent sizing across all elements.


6. Best Practices for Using Box Sizing

When using box-sizing, consider the following best practices:

  • Use border-box for Layouts: Applying box-sizing: border-box; can simplify layout calculations, especially when dealing with padding and borders.

  • Apply Globally: Consider applying box-sizing: border-box; globally with a universal selector (*) to maintain consistent sizing across all elements.

  • Test Across Browsers: Ensure that your layout works as expected across different browsers, especially if you're applying box-sizing globally.


7. Cross-Browser Compatibility

CSS3 box-sizing is supported in all modern browsers. However, older versions of browsers like Internet Explorer may require vendor prefixes.

Example with Vendor Prefixes:

.element {
  -webkit-box-sizing: border-box; /* Safari */
  -moz-box-sizing: border-box; /* Firefox */
  box-sizing: border-box; /* Standard */
}

Explanation:

  • Including vendor prefixes ensures compatibility across different browsers.


8. Conclusion

CSS3 box-sizing provides an essential tool for controlling how the width and height of elements are calculated. By understanding and using box-sizing, you can create more predictable and consistent layouts, reducing the complexity of handling padding and borders.

  • content-box: The default model, where width and height apply only to the content, excluding padding and borders.

  • border-box: A model where width and height include padding and borders, simplifying layout calculations.

By mastering the box-sizing property, you can create layouts that are both flexible and easy to manage, avoiding common pitfalls related to element sizing.


For more tutorials and insights, visit Codes With Pankaj.

Last updated