Flash cards
Review the key moves
What is the main idea behind CSS Padding and box-sizing?
Lesson checks
Practice each idea before moving on
Short Mimo-style checks built from this lesson's code, terms, and sequence.
Which statement best captures the main point of this lesson?
Complete the missing token from the example code.
___: 300px;Put the learning moves in the order that makes the concept easiest to apply.
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
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
div {
width: 300px;
padding: 25px;
box-sizing: border-box;
}Live preview
All CSS Padding Properties
| Property | Description |
|---|---|
| padding | A shorthand property for setting all the padding properties in one declaration |
| padding-bottom | Sets the bottom padding of an element |
| padding-left | Sets the left padding of an element |
| padding-right | Sets the right padding of an element |
| padding-top | Sets the top padding of an element |