Sub topic 4
Border: the edge of the box
This chapter assumes you can write CSS rules with selectors and declarations, and that you know units such as px, em, and rem. It also assumes you have met the content and padding areas from the earlier chapters.
Where the border sits
The border is the layer between padding and margin. It draws a line around the content plus its padding. Everything inside the border belongs to the element's own box. Everything outside it is margin, which is empty space.
The border counts toward the element's size. With the default box-sizing: content-box, the border is added on top of width and height.
Three properties, one shorthand
A border has a width, a style, and a color. Each has its own property:
border-width: the thickness, such as5pxor the keywordsthin,medium,thick.border-style: the line type, such assolid,dashed,dotted,double, ornone.border-color: any CSS color. If you omit it, the border uses the element'scolorvalue.
The border shorthand sets all three at once:
.card {
width: 300px;
padding: 20px;
border: 5px solid #333;
margin: 10px;
}The order inside the shorthand does not matter. solid 5px #333 works the same way.
Setting individual sides
Each side has its own properties: border-top, border-right, border-bottom, and border-left. The longhand properties also take one to four values, in the same clockwise order as margin and padding (top, right, bottom, left).
.notice {
border-style: solid;
border-width: 1px 1px 1px 6px;
border-color: #ccc #ccc #ccc #0a7;
}This draws a thin gray box with a thick green stripe on the left. You often see this pattern on alert boxes.
What the border adds to the size
Take the .card rule above. The horizontal arithmetic under content-box is:
| Part | Width |
|---|---|
| Content (`width`) | 300px |
| Padding (20px × 2) | 40px |
| Border (5px × 2) | 10px |
| Border box | 350px |
The margin adds another 20px, so the card takes 370px of horizontal space. The vertical calculation works the same way, using height, top and bottom padding, and top and bottom border.
If you set box-sizing: border-box, width: 300px already includes the padding and border. The content area then shrinks to 250px. The next chapters cover box-sizing in detail.
Common errors
The border does not appear. The default border-style is none. When the style is none, the computed border width becomes 0, whatever width you set. Writing border-width: 2px; border-color: red; alone draws nothing. Always set a style, or use the shorthand.
The layout jumps on hover. If you add border: 2px solid blue only in :hover, the box grows by 4px in each direction and its neighbours move. Reserve the space in advance:
.button {
border: 2px solid transparent;
}
.button:hover {
border-color: blue;
}The transparent border takes up the same space, so only the color changes.
Percentages in border widths. border-width does not accept percentages. Use lengths such as px or rem, or one of the keywords.
Borders and backgrounds. By default, an element's background extends underneath its border. A semi-transparent or dashed border therefore shows the background through the gaps. You can change this with background-clip.
Exercise
An element has width: 200px; padding: 10px; border: 4px solid black; and the default box-sizing. What is the width of its border box? What would it be if you removed solid from the border shorthand?
Show answer
The border box is 200 + 20 (padding) + 8 (border) = 228px. Without solid, the border style falls back to none, so the computed border width is 0 and the border box is 200 + 20 = 220px.