bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths

Loading lesson path

Learn/CSS/CSS Foundations
CSS•CSS Foundations

CSS Padding and box-sizing

Padding and Element Width

The CSS width property specifies the width of the element's content area. The content area is the portion inside the padding, border, and margin of an element ( the box model ).

So, if an element has a specified width, the padding added to that element will be added to the total width of the element. This is often an undesirable result.

Example

Formatted code
div {
  width: 300px;
  padding: 25px;
}

Live preview

Padding and box-sizing

The box-sizing property defines how the width and height of an element are calculated: should they include padding and borders, or not.

The box-sizing property can have the following values:

  • content-box - This is default. The width and height properties includes only the content (border and padding are not included)
  • border-box - The width and height properties includes content, padding and border

So, to keep the width at 300px, no matter the amount of padding, you can use the box-sizing: border-box; . This causes the element to maintain its actual width; if you increase the padding, the available content space will decrease.

Example

Formatted code
div {
  width: 300px;
  padding: 25px;
  box-sizing: border-box;
}

Live preview

All CSS Padding Properties

PropertyDescription
paddingA shorthand property for setting all the padding properties in one declaration
padding-bottomSets the bottom padding of an element
padding-leftSets the left padding of an element
padding-rightSets the right padding of an element
padding-topSets the top padding of an element

Previous

CSS Padding

Next

CSS Height and Width