CSS Border Shorthand
In this page:
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.
- 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
<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.
- 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
<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
<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
<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
<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>
- Writing
border: 1px red, which is missing the style, so no border appears. - Using the shorthand after a specific side rule, which resets the side back to the shorthand values.
- Expecting a border to add nothing to the size, when it adds to the element's total width unless
box-sizing: border-boxis used.
- 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.
Supported in all modern browsers: Chrome, Firefox, Safari, Edge and Opera.
Chapter Quiz — Complete all 21 topics to unlock
0/21 topics done
Complete these topics first:
- CSS Colors
- CSS RGB Colors
- CSS HEX Colors
- CSS HSL Colors
- CSS Color Keywords
- CSS Backgrounds
- CSS Background Color
- CSS Background Image
- CSS Background Repeat
- CSS Background Attachment
- CSS Background Shorthand
- CSS Multiple Backgrounds
- CSS Background Size
- CSS Background Origin
- CSS Background Clip
- CSS Borders
- CSS Border Style
- CSS Border Width
- CSS Border Color
- CSS Border Sides
- CSS Border Shorthand