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 the units px, em, rem, and %. It also assumes you know the four box layers from the overview chapter: content, padding, border, and margin. Here we look closely at the border layer.
Where the border sits
The border is the layer between padding and margin. It surrounds the content and padding, and everything outside it belongs to the margin. The edge of the border is the outer edge of the element's border box, which is the shape that width describes when you use box-sizing: border-box.
Three facts about borders matter for layout:
- A border takes up space. Under
content-box, border widths are added towidthandheight. Underborder-box, they are subtracted from the content area. - A border is painted on top of the element's background. By default the background extends underneath it, so a semi-transparent or dashed border shows the background through the gaps.
- A border cannot be negative, and it does not collapse with neighbours the way vertical margins do.
Three properties, one shorthand
Every border is described by a width, a style, and a color. Each has its own longhand property.
| Property | Controls | Initial value |
|---|---|---|
| `border-width` | Thickness | `medium` (usually 3px) |
| `border-style` | Line pattern | `none` |
| `border-color` | Color | `currentcolor` |
The shorthand border sets all three on all four sides at once. The order of the values does not matter.
.note {
border: 2px solid #1d4ed8;
}This is equivalent to writing border-width: 2px; border-style: solid; border-color: #1d4ed8;.
The style is required
The initial border-style is none. When the style is none or hidden, the computed border width becomes 0, whatever width you wrote. This is the most common border bug.
.broken {
border-width: 4px;
border-color: crimson;
/* No border-style, so nothing is drawn and no space is used. */
}If you use the longhand properties, always set border-style. The shorthand avoids the problem only if you include a style, because border: 4px crimson resets the style to none.
Color defaults to the text color
If you leave out the color, the border uses currentcolor, which is the element's computed color value. A border written as border: 1px solid; on a paragraph with color: green is green. If you later change the text color, the border changes with it. This is useful for icons and buttons, and it is a surprise when you did not intend it.
Border styles
The border-style keyword picks the pattern.
| Value | Result |
|---|---|
| `none` | No border. Width computes to 0. |
| `hidden` | Same as `none`, except that it wins conflicts in collapsed table borders. |
| `solid` | A continuous line. |
| `dashed` | A series of dashes. |
| `dotted` | A series of dots. |
| `double` | Two lines with a gap. The two lines and the gap together equal the border width. |
| `groove`, `ridge`, `inset`, `outset` | Fake 3D effects built from darker and lighter shades of the color. |
In practice you will use solid most of the time, then dashed and dotted. The 3D styles look different in every browser and rarely look good. double needs a width of at least 3px to show two lines.
Setting individual sides
Each side has its own properties: border-top, border-right, border-bottom, and border-left. Each also has longhands such as border-top-width and border-left-color.
The longhand border-width, border-style, and border-color accept one to four values, using the same clockwise pattern as margin and padding:
.panel {
border-style: solid;
border-width: 1px 4px 1px 12px; /* top right bottom left */
border-color: #cbd5e1 #cbd5e1 #cbd5e1 #1d4ed8;
}A common pattern is to override one side after a shorthand:
.alert {
border: 1px solid #e2e8f0;
border-left: 6px solid #dc2626;
}Order matters here. A later border shorthand resets all four sides, so it would wipe out the earlier border-left. Put the general rule first and the specific side second.
Width values
border-width accepts any non-negative length (px, em, rem, and so on) or one of three keywords: thin, medium, and thick. The specification does not fix exact pixel values for the keywords, but all browsers give medium the value 3px. Percentages are not allowed. A % border width is invalid and the declaration is ignored.
Browsers snap border widths to whole device pixels. A width larger than one device pixel but not a whole number is rounded down. On a standard-density screen, border: 1.5px solid is drawn as 1px. On a screen with a device pixel ratio of 2, the same border is drawn as 3 device pixels. Because the snapping can change the used width, avoid fractional border widths when you need pixel-exact layouts.
How borders change the size of an element
Take this element:
.slab {
width: 200px;
padding: 10px;
border-left: 4px solid #333;
border-right: 12px solid #333;
}The horizontal totals depend on box-sizing:
| Row | `content-box` | `border-box` |
|---|---|---|
| Content width | 200px | 200 − 20 − 16 = 164px |
| Border box width | 200 + 20 + 16 = 236px | 200px |
The border contributes the sum of its left and right widths (16px) in both cases. Only where that sum is taken from changes. The top and bottom borders affect height in the same way. If you add height: 100px and a 2px top and bottom border, the border box is 104px plus padding under content-box, and 100px under border-box.
Transparent borders
A transparent border still takes up space. Because the background extends under the border by default (background-clip: border-box), the background color shows through it. This makes transparent borders useful in two ways.
The first is to reserve space so that a hover state does not move the layout. Adding a border only on hover makes the element grow and pushes its neighbours. Declaring a transparent border in the normal state avoids that:
.tab {
border-bottom: 3px solid transparent;
}
.tab:hover,
.tab[aria-selected="true"] {
border-bottom-color: #1d4ed8;
}The second is to keep a background out from under the border. Set background-clip: padding-box and the background stops at the inner border edge. You will want this when the border color is semi-transparent and you want to see the page behind it, not the element's own background.
Corners and the triangle trick
Where two borders meet at a corner, the browser joins them along a diagonal line, like a picture frame. If the two sides have different colors, you see two triangles meeting at that line. Making three sides transparent and one side colored produces a single triangle when the content area has no size:
.arrow-up {
width: 0;
height: 0;
border-left: 20px solid transparent;
border-right: 20px solid transparent;
border-bottom: 30px solid #333;
}This draws a triangle that is 40px wide and 30px tall. Modern CSS offers clip-path as an alternative, but this technique still appears in older code, such as tooltip arrows.
Rounded corners
border-radius rounds the corners of the border box. It affects painting, not layout. The element keeps its rectangular box for sizing and spacing, and the background and border are clipped to the rounded shape. Content is not clipped unless you also set overflow to something other than visible, which a later chapter covers.
.pill {
border: 2px solid #1d4ed8;
border-radius: 999px;
padding: 0.25rem 0.75rem;
}Borders on inline elements
On an inline element such as <span>, left and right borders add horizontal space around the text. Top and bottom borders are drawn, but they do not change the line's height. They can overlap neighbouring lines. The display types chapter explains why.
Border or outline?
Outlines and shadows look similar to borders, but they are drawn outside the box model and take up no space. The chapter on outline and box-shadow covers them. If you want to highlight an element without moving anything, an outline is often the better choice than a border added on hover.
Common errors
- Setting
border-widthandborder-colorwithout aborder-style. Nothing is drawn. - Writing a side override before the
bordershorthand, so the shorthand overwrites it. - Adding a border on hover and causing a layout shift. Use a transparent border in the base state.
- Using percentages for border width. The declaration is invalid.
- Forgetting that borders add to the size under
content-box, which makes awidth: 100%element overflow its parent.
Exercise
A div has width: 300px; padding: 15px; border: 4px solid black; box-sizing: content-box;. What is the width of its border box? What would the border box be if box-sizing were border-box?
Show answer
Under content-box, the border box is 300 + (2 × 15) + (2 × 4) = 338px wide. Under border-box, width already includes the padding and border, so the border box is 300px wide and the content area is 300 − 30 − 8 = 262px.
Exercise
You write .button { border-width: 2px; border-color: teal; } and no border appears. What is wrong, and what is the smallest fix?
Show answer
The border-style is still its initial value, none, which makes the computed border width 0. Add border-style: solid;, or replace the two declarations with the shorthand border: 2px solid teal;.