CSS Reference ยท Borders, Outlines and Shadows
The border property
Reference for the CSS border shorthand: set width, style and color for all four sides in one declaration, with value syntax, defaults and examples.
The border property is a shorthand that sets the width, style and color of all four borders of an element at once. The border sits between the padding and the margin and is part of the box that box-sizing can include in the element's size.
The most important thing to remember: a border is invisible unless it has a style. The default style is none, so border: 4px red alone draws nothing.
.card { border: 2px solid #4a5568; }
Try it
The fifth box shows the classic mistake (no style, so no border). The sixth leaves out the color; the border takes the element's color, which is blue here.
Syntax
border: <line-width> || <line-style> || <color>
The three parts can come in any order, and each is optional.
- Initial value: the initial value of each longhand:
medium none currentcolor - Applies to: all elements
- Inherited: no
- Animatable: yes (width and color; style changes are discrete)
- Computed value: see the longhands:
border-width,border-style,border-color
Values
| Value | Description |
|---|---|
<line-width> |
A length such as 2px, or thin, medium, thick. Omitted means medium. See border-width. |
<line-style> |
none, hidden, solid, dashed, dotted, double, groove, ridge, inset, outset. Omitted means none, so nothing is drawn. |
<color> |
Any color value. Omitted means currentcolor. |
none |
Resets all four borders (style none, which also makes the width compute to 0). |
Usage notes
- Always include a style.
solidis the usual choice. Without it nothing is drawn. - It sets all four sides to the same value. To style one side use
border-top,border-right,border-bottomorborder-left; for different values per side use the longhands, which accept up to four values. - Omitted parts reset to initial.
border: solidafterborder-color: redresets the color tocurrentcolor, because shorthands set every part they cover. The shorthand also resetsborder-image. - Borders add to the box size under
content-box. A200pxwide element with10pxof padding and a2pxborder is224pxwide. Usebox-sizing: border-boxto include them. Seebox-sizing. - Borders and layout. Changing a border width on hover moves neighbors. To avoid layout jumps, keep the width constant and change only the color, or use
outlineorbox-shadow. - Rounded corners. The border follows
border-radius. - Logical equivalents.
border-blockandborder-inlinefollow the writing direction. - Tables. With
border-collapse: collapse, adjacent cell borders merge; seeborder-collapse.
Browser support
Supported in every browser.
Related
border-width,border-style,border-colorโ the longhandsborder-topand the other side shorthandsborder-radiusโ rounded cornersoutlineโ a border-like line outside the box- Lessons: Borders, The Box Model