← Back to CSS Course | Chapter 2: Colors & Backgrounds | Lesson 21 of 21

CSS Border Shorthand

The border shorthand lets you set width, style, and color for all four sides in one line instead of writing three separate declarations every time.
Syntax
css
selector {
  border: width style color;
}

The Shorthand Syntax

border: width style color combines border-width, border-style, and border-color into one declaration, applied identically to all four sides. Only border-style is strictly required for anything to render; width and color have sensible defaults (medium and currentColor) if omitted.

Note:
  • Accepted values:
  • border — width style color, e.g. 1px solid #333
  • width — length, thin, medium, or thick
  • style — solid, dashed, dotted, and more; required to show a border
  • color — any CSS color, defaults to currentcolor
  • Order of the three parts is flexible
  • inherit | initial | unset | revert — CSS-wide keywords accepted by every property

Example: The Shorthand Syntax

css
<style>
.box {
  border: 2px solid red;
}
</style>
<div class="box">Width, style, and color together</div>

Value Order Flexibility

Unlike some CSS shorthands, border's three values can technically appear in any order since each value's type (a length, a style keyword, or a color) is unambiguous — but width-style-color is the near-universal convention for readability.

Note:
  • Accepted values:
  • width — length or thin, medium, thick
  • style — solid, dashed, dotted, double and others
  • color — any color value
  • values may appear in any order
  • style — required for the border to appear
  • inherit | initial | unset | revert — CSS-wide keywords accepted by every property

Example: Value Order Flexibility

css
<style>
.box {
  border: red solid 2px;
}
</style>
<div class="box">Order doesn't matter, each value's type is unambiguous</div>

Shorthand Resets Unspecified Values

Using the border shorthand resets ALL of border-width, border-style, and border-color to their defaults for any value you don't include — so border: solid red alone resets width back to medium even if a wider border-width was set earlier in the cascade.

Example: Shorthand Resets Unspecified Values

css
<style>
.box {
  border-width: 10px;
  border: solid red;
}
</style>
<div class="box">border-width silently reset back to medium</div>

Combining Shorthand With Per-Side Overrides

A common pattern is setting border as the shorthand baseline for all sides, then overriding just one side afterward with border-left or border-top — the later, more specific side rule wins for that edge only.

Example: Combining Shorthand With Per-Side Overrides

css
<style>
.box {
  border: 2px solid black;
  border-left: 6px solid teal;
}
</style>
<div class="box">Shorthand baseline, left side overridden</div>

border vs border-image

The border shorthand only handles solid-color borders; achieving a patterned or image-based border (like a dotted line made of icons) requires the separate border-image property instead, which border does not control.

Example: border vs border-image

css
<style>
.solid {
  border: 10px solid black;
}
.image {
  border: 10px solid transparent;
  border-image: url('https://placehold.co/60x60') 20;
}
</style>
<div class="solid">Plain solid border</div>
<div class="image">border-image handles the pattern instead</div>
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Writing border: 1px red, which is missing the style, so no border appears.
  2. Using the shorthand after a specific side rule, which resets the side back to the shorthand values.
  3. Expecting a border to add nothing to the size, when it adds to the element's total width unless box-sizing: border-box is used.
Chapter Summary
  • CSS colors can be written in names, RGB, HEX, and HSL forms.
  • Background properties control color, image, repeat, attachment, size, origin, and clip, and can be combined in shorthand.
  • Border properties control style, width, color, and sides.
Browser Support

Supported in all modern browsers: Chrome, Firefox, Safari, Edge and Opera.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.