Skip to content
Tutorials Center
My learning

Topics

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.

CSS
.card {
  border: 2px solid #4a5568;
}

Try it

Border shorthand

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.

Different borders per side

Syntax

CSS
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. solid is 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-bottom or border-left; for different values per side use the longhands, which accept up to four values.
  • Omitted parts reset to initial. border: solid after border-color: red resets the color to currentcolor, because shorthands set every part they cover. The shorthand also resets border-image.
  • Borders add to the box size under content-box. A 200px wide element with 10px of padding and a 2px border is 224px wide. Use box-sizing: border-box to include them. See box-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 outline or box-shadow.
  • Rounded corners. The border follows border-radius.
  • Logical equivalents. border-block and border-inline follow the writing direction.
  • Tables. With border-collapse: collapse, adjacent cell borders merge; see border-collapse.

Browser support

Supported in every browser.