To add a css border, set three things: a width, a style and a color. The shorthand border: 2px solid #333 does all three at once, and the style is the only required part because the default style is none, which draws nothing. This guide shows the shorthand, the longhand properties, per-side borders and how borders affect element size.
The order of the three values does not matter, but width style color is the convention. If you omit the color, the border uses the element's color value through currentcolor. If you omit the width, it defaults to medium. If you omit the style, no border appears at all, which is the most common reason for an "invisible border".
border-width accepts a length such as 1px or 0.125rem, or the keywords thin, medium and thick. Percentages are not allowed.
With two values the first is top and bottom, the second is left and right. With four values they run clockwise from the top.
| Value | Look |
|---|---|
none | No border (default) |
solid | Continuous line |
dashed | Dashes |
dotted | Dots |
double | Two lines (needs at least 3px width) |
groove, ridge, inset, outset | 3D effects, rarely used today |
hidden | Like none, but wins conflicts in table borders |
border-color takes any color value and follows the same one to four value pattern as width.
Without a color value the border follows the text color, which makes themes easy: change color and the border updates.
Each side has its own shorthand and longhands.
Longhands such as border-top-width, border-right-style and border-bottom-color exist for every combination. For languages that run right to left or vertically, logical properties like border-inline-start and border-block-end follow the writing direction instead of the physical side.
By default (box-sizing: content-box) the border is added to the element's width and height, so a 200px wide box with a 5px border becomes 210px wide. Most projects set a global reset:
With border-box the width includes padding and border, which makes layouts predictable. Borders also take up space, unlike outline, which is drawn outside the box without affecting layout.
| Feature | border | outline |
|---|---|---|
| Affects layout | Yes | No |
| Per-side control | Yes | No |
Follows border-radius | Yes | In current browsers, yes |
| Typical use | Decoration | Focus indicators |
Never remove the focus outline without providing a visible replacement, because keyboard users depend on it.
The general border rule sets all sides, and the later border-top rule overrides just the top. Order matters: if border came second, it would reset the top again.
Most often the style is missing. Add solid (or another style), because the default border-style is none even when you set a width and color.
Use border-top, border-right, border-bottom or border-left, or the logical versions like border-inline-start.
With content-box yes, it adds to the total size. With box-sizing: border-box the border is included inside the declared width and height.
A border is part of the box and takes layout space. An outline is drawn outside it and does not move anything, which is why it is used for focus rings.
A border needs a style to appear, usually with a width and color given in the shorthand border: 2px solid color. Control each side with the side shorthands, use border-box sizing to keep dimensions predictable, and prefer outlines for focus indicators.