Skip to content

Instantly share code, notes, and snippets.

@kingluddite
Created April 27, 2024 11:26
Show Gist options
  • Save kingluddite/bbfedab62b16817062edd64207856870 to your computer and use it in GitHub Desktop.
Save kingluddite/bbfedab62b16817062edd64207856870 to your computer and use it in GitHub Desktop.
using box sizing can save you time in CSS

That's correct! In CSS, the width and height properties of an element refer only to the content area of the element, excluding padding, border, and margin. If you want to set the total width and height of an element including padding and border, you would typically use the box-sizing property.

The box-sizing property can have two values:

  1. content-box (default): This is the standard box model where the width and height properties only apply to the content area of the element, and padding, border, and margin are added on top of that.

  2. border-box: In this model, the width and height properties include padding and border, but not margin. This is often preferred as it simplifies layout calculations.

Here's an example of how you can use box-sizing:

.element {
  box-sizing: border-box;
  width: 300px; /* This will include padding and border */
  height: 200px; /* This will include padding and border */
  padding: 20px;
  border: 2px solid black;
  margin: 10px;
}

In this example, the total width of the element would be 300px + 2*20px + 2*2px = 344px, including padding and border but excluding margin.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment