
How to Style HTML Lists with CSS
11/10/2019
CSS Width and Height: How to Set Minimum and Maximum Sizes
12/10/2019
The CSS box model describes the layers around an element: content, padding, border, and margin. Knowing which layer you’re changing makes it easier to explain why an element is bigger or farther from its neighbors than you expected.
9. The CSS box model

- Content holds the text, image, or other material.
- Padding sits between the content and the border.
- Border surrounds the content and padding.
- Margin creates space outside the border.
With the default box-sizing: content-box, width measures the content area. A width of 200px, 16px of padding on each side, and a 2px border on each side make a 236px border box. With box-sizing: border-box, the specified 200px includes the padding and border. Margins sit outside that measurement.
9.1. Padding
| Property | Description | Values |
|---|---|---|
padding-top padding-right padding-bottom padding-left | Padding on the top, right, bottom, and left | length | percentage |
padding | Padding on all sides | length | percentage {1,4} |
Use padding-top, padding-right, padding-bottom, or padding-left to change one side. The shorthand sets them together:
- One value applies to all sides.
- Two values set top/bottom and left/right.
- Three values set top, left/right, and bottom.
- Four values set top, right, bottom, and left.
.card { padding: 1rem 2rem 1.5rem; }The example sets 1rem at the top, 2rem on both sides, and 1.5rem at the bottom. The element’s background normally extends across padding.
Padding longhands and shorthand
div.a {
padding-top: 30px;
padding-bottom: 80px;
padding-right: 50px;
padding-left: 40px;
border: 1px solid black;
background-color: azure;
}
div.b { /* Shorthand */
padding: 30px 50px 80px 40px; /*top, right, bottom, left*/
border: 1px solid black;
background-color: darkseagreen;
}9.2. Margin
| Property | Description | Values |
|---|---|---|
margin-top margin-right margin-bottom margin-left | Margin on the top, right, bottom, and left | length | percentage | auto |
margin | Margin on all sides | length | percentage | auto {1,4} |
The margin shorthand uses the same side order as padding. Margins can be negative, and auto can distribute available space. For example, a block whose width is smaller than its container can be centered with margin-inline: auto. Vertical margins of some adjacent block elements collapse in normal flow, so two margins can share space.
.card {
width: min(100%, 30rem);
margin: 1rem auto;
}Margin longhands and shorthand
div.a {
margin-top: 30px;
margin-bottom: 80px;
margin-right: 50px;
margin-left: 40px;
border: 1px solid black;
background-color: azure;
}
div.b { /* Shorthand */
margin: 30px 50px 80px 40px;
border: 1px solid black;
background-color: darkseagreen;
}9.3. Borders
| Property | Description | Values |
|---|---|---|
border-top-width border-right-width border-bottom-width border-left-width | Top, right, bottom, or left border width | thin | medium | thick | length |
border-width | Border width shorthand | thin | medium | thick | length {1,4} |
border-top-color border-right-color border-bottom-color border-left-color | Top, right, bottom, and left border color | color | transparent |
border-color | Border color shorthand | color | transparent {1,4} |
border-top-style border-right-style border-bottom-style border-left-style | Top, right, bottom, and left border style | none | hidden | dotted | dashed | solid | double | groove | ridge | inset | outset |
border-style | Border style shorthand | none | hidden | dotted | dashed | solid | double | groove | ridge | inset | outset {1,4} |
border-top border-right border-bottom border-left | Width, style, and color of a single border side | border-top-width | border-top-style | border-top-color |
border | Width, style, and color of all borders | border-width | border-style | border-color |
border-radius | Corner radius | length | percentage {1,4} |
Use border-width, border-style, and border-color, or combine them with border. A visible border needs a style such as solid, dashed, or dotted. Set one side with a property such as border-bottom and round corners with border-radius.
.card {
border: 2px solid #49546a;
border-bottom-width: 4px;
border-radius: 0.5rem;
}Border styles for each side
.a { border: 4px solid blue; }
.b { border: 6px dotted green; }
.c { border: 3px dashed purple; }
.d {
border-top: 3px double orange;
border-right: 3px double brown;
border-bottom: 3px double brown;
border-left: 3px double brown;
}Rounded corners with border-radius
.a { background-color: blue; border-radius: 20px 20px 0 0; height:100px; width: 100px;}
.b { background-color: green; border-radius: 0 0 40px 40px; height:100px; width: 100px;}
.c { background-color: purple; border-radius: 50px; height:100px; width: 100px;}Try it yourself
A box has width: 240px; padding: 12px; border: 3px solid; margin: 10px. How wide is its border box with the default box sizing?
Show the calculation
It is 270px wide: 240px for content, 24px of horizontal padding, and 6px of horizontal border. The two margins add another 20px outside that box.