CSS border Property
The border shorthand property sets the width, style, and color of an element's border on all four sides.
A border surrounds an element's padding and content. The shorthand combines several border properties into one declaration, making it useful for styling containers, buttons, form controls, images, and other interface elements.
border Syntax
selector {
border: 2px solid #1769aa;
}
The border shorthand accepts up to three components: a border width, a border style, and a border color.
.example {
border: 2px solid blue;
}
.width-and-style {
border: 3px dashed;
}
.style-and-color {
border: dotted red;
}
.style-only {
border: solid;
}
.no-border {
border: none;
}
The components may appear in any order, provided each value can be identified unambiguously.
.first {
border: 2px solid red;
}
.second {
border: solid red 2px;
}
.third {
border: red 2px solid;
}
All three declarations produce the same border.
CSS-wide values such as inherit, initial, revert, revert-layer, and unset are also accepted.
Unlike border-width, border-style, and border-color, the border shorthand cannot specify different values for different sides in a single declaration.
Property Values
| Value | Description |
|---|---|
<line-width> |
Specifies the border thickness using a nonnegative length or the thin, medium, or thick keyword. |
<line-style> |
Specifies the appearance of the border using values such as solid, dashed, or dotted. |
<color> |
Specifies the border color using a CSS color value, including named colors, hexadecimal colors, and color functions. |
none |
Specifies no border line. This is a border-style value. |
hidden |
Suppresses the border line and has special behavior when resolving collapsed table borders. |
The shorthand's initial value is equivalent to medium none currentColor. Because the default border style is none, no visible border appears until a visible style is specified.
The property is not inherited. Its constituent properties have their own computed values and animation behavior.
How border Works
The border shorthand sets the border width, style, and color for the top, right, bottom, and left sides of an element.
.panel {
border: 3px solid #1769aa;
}
This declaration is equivalent to setting the following properties:
.panel {
border-top-width: 3px;
border-right-width: 3px;
border-bottom-width: 3px;
border-left-width: 3px;
border-top-style: solid;
border-right-style: solid;
border-bottom-style: solid;
border-left-style: solid;
border-top-color: #1769aa;
border-right-color: #1769aa;
border-bottom-color: #1769aa;
border-left-color: #1769aa;
}
A Border Surrounds the Padding
In the CSS box model, the border sits outside the padding and inside the margin.
Unless box-sizing: border-box is used, a border generally increases an element's total rendered dimensions beyond its declared width and height.
The Border Style Controls Visibility
Specifying a border width and color is not enough to display a border if the style remains none.
.invisible-border {
border: 4px red;
}
.visible-border {
border: 4px solid red;
}
The first declaration does not produce a visible border because the omitted style resets to none. The second declaration produces a visible red border.
The Shorthand Resets Existing Values
.box {
border-top: 5px dashed red;
border: 2px solid blue;
}
The later border declaration replaces the earlier top-border settings and applies a 2-pixel solid blue border to all four sides.
Border Width
The width component controls the thickness of the border.
Length Values
.thin-line {
border: 1px solid #1769aa;
}
.medium-line {
border: 3px solid #1769aa;
}
.thick-line {
border: 6px solid #1769aa;
}
Nonnegative lengths such as px, em, and rem can be used. Percentage values are not valid border widths.
Width Keywords
.thin-border {
border: thin solid #1769aa;
}
.medium-border {
border: medium solid #1769aa;
}
.thick-border {
border: thick solid #1769aa;
}
The exact thickness of the thin, medium, and thick keywords is determined by the browser. Their relative order is maintained, but their pixel measurements are not fixed by the specification.
Zero Width
.zero-width {
border: 0 solid red;
}
A zero-width border occupies no border thickness and is not visible.
For consistent visual results across browsers, explicit length values are generally preferable to the width keywords.
Border Style
The style component determines how the border line is drawn.
| Value | Appearance |
|---|---|
none |
No border is drawn. |
hidden |
No border is drawn; takes precedence in collapsed table border conflict resolution. |
solid |
A continuous line. |
dotted |
A series of dots. |
dashed |
A series of short line segments. |
double |
Two parallel lines separated by a gap. |
groove |
A carved or recessed appearance. |
ridge |
A raised or embossed appearance. |
inset |
An appearance suggesting the element is pressed inward. |
outset |
An appearance suggesting the element is raised outward. |
Common Border Styles
.solid-example {
border: 4px solid #1769aa;
}
.dashed-example {
border: 4px dashed #1769aa;
}
.dotted-example {
border: 4px dotted #1769aa;
}
.double-example {
border: 6px double #1769aa;
}
Three-Dimensional Styles
.groove-example {
border: 8px groove #1769aa;
}
.ridge-example {
border: 8px ridge #1769aa;
}
.inset-example {
border: 8px inset #1769aa;
}
.outset-example {
border: 8px outset #1769aa;
}
The appearance of three-dimensional styles depends on browser rendering and color adjustments.
The double Style
The double style draws two lines within the specified border width. A sufficiently large width makes the two lines easier to distinguish.
The browser determines the exact distribution of the lines and the space between them.
Border Color
The color component determines the color of the border line.
Named Colors
.named-color {
border: 2px solid navy;
}
Hexadecimal Colors
.hex-color {
border: 2px solid #1769aa;
}
RGB Colors
.rgb-color {
border: 2px solid rgb(23 105 170);
}
HSL Colors
.hsl-color {
border: 2px solid hsl(207 76% 38%);
}
Transparent Borders
.transparent-border {
border: 3px solid transparent;
}
A transparent border still occupies space in the box model even though its color is invisible.
This can help maintain consistent dimensions between different interface states.
Using currentColor
.matching-border {
color: #1769aa;
border: 2px solid currentColor;
}
The currentColor keyword uses the element's computed color value.
If the color component is omitted, the border color defaults to currentColor.
.automatic-color {
color: #1769aa;
border: 2px solid;
}
This example produces a border matching the text color.
Omitted Values and Defaults
The border shorthand allows components to be omitted. Missing components are reset to their initial values.
| Component | Initial Value |
|---|---|
| Border width | medium |
| Border style | none |
| Border color | currentColor |
Omitting Width
.example {
border: solid blue;
}
The width is reset to medium.
Omitting Color
.example {
border: 2px solid;
}
The color is reset to currentColor.
Omitting Style
.example {
border: 2px blue;
}
The style is reset to none, so the border is not visible.
Resetting All Borders
.reset-border {
border: none;
}
This resets the border style to none, the width to medium, and the color to currentColor on all four sides.
Although the specified width resets to medium, the used width is zero when the border style is none.
Styling Individual Sides
The border shorthand applies the same width, style, and color to all four sides. Use side-specific properties when different sides require different styling.
Top Border
.top-border {
border-top: 4px solid #1769aa;
}
Right Border
.right-border {
border-right: 4px dashed #1769aa;
}
Bottom Border
.bottom-border {
border-bottom: 4px dotted #1769aa;
}
Left Border
.left-border {
border-left: 4px double #1769aa;
}
Combining the Shorthand with Individual Sides
.highlighted-panel {
border: 1px solid #c5d8e8;
border-left: 5px solid #1769aa;
}
The first declaration establishes a consistent border around the element. The second overrides only the left border.
Declaration Order Matters
.example {
border-left: 5px solid red;
border: 1px solid blue;
}
Because the border shorthand appears later, it overrides the earlier left-border declaration when both declarations have equal cascade priority.
Place individual side overrides after the general shorthand when they should take precedence.
Logical Border Properties
Logical border properties apply borders according to the element's writing mode and text direction rather than fixed physical sides.
| Property | Purpose |
|---|---|
border-block |
Sets borders on the block-start and block-end sides. |
border-inline |
Sets borders on the inline-start and inline-end sides. |
border-block-start |
Sets the border on the block-start side. |
border-block-end |
Sets the border on the block-end side. |
border-inline-start |
Sets the border on the inline-start side. |
border-inline-end |
Sets the border on the inline-end side. |
Logical Block Borders
.logical-panel {
border-block: 2px solid #1769aa;
}
In horizontal writing mode, this sets the top and bottom borders. In vertical writing mode, it sets the physical left and right borders.
Logical Inline Borders
.logical-panel {
border-inline: 2px solid #1769aa;
}
In horizontal writing mode, this sets the left and right borders. In vertical writing mode, it sets the physical top and bottom borders.
A Writing-Mode-Aware Accent Border
.notice {
border: 1px solid #c5d8e8;
border-inline-start: 5px solid #1769aa;
}
The accent border follows the inline-start side of the element, which changes according to writing mode and direction.
Use logical border properties when building components intended to support multiple writing systems.
Borders and the Box Model
A border is part of the CSS box model. Its thickness contributes to an element's dimensions according to the selected box-sizing value.
The content-box Model
.content-box-example {
box-sizing: content-box;
width: 200px;
padding: 20px;
border: 5px solid #1769aa;
}
The total border-box width is 250 pixels: 200 pixels of content, 40 pixels of padding, and 10 pixels of borders. Margins are not included.
The border-box Model
.border-box-example {
box-sizing: border-box;
width: 200px;
padding: 20px;
border: 5px solid #1769aa;
}
The total border-box width is 200 pixels. The browser reduces the content width to accommodate the padding and borders.
Avoiding Layout Changes
Adding a border to an element that previously had no border can change its rendered dimensions under the content-box sizing model.
.interactive-card {
border: 2px solid transparent;
}
.interactive-card:hover {
border-color: #1769aa;
}
This approach reserves the border thickness in both states. Changing only the color avoids the size change that could occur when a border is introduced on hover.
Use box-sizing: border-box when you want padding and borders included within a declared width or height.
Working with border-radius
The border-radius property rounds the corners of an element's border box.
.rounded-panel {
border: 2px solid #1769aa;
border-radius: 12px;
}
The border follows the rounded corners established by border-radius.
Pill-Shaped Borders
.pill-button {
border: 2px solid #1769aa;
border-radius: 999px;
padding: 10px 24px;
}
A sufficiently large radius can create a pill-shaped appearance for suitable element dimensions.
Circular Borders
.circle {
width: 100px;
height: 100px;
border: 3px solid #1769aa;
border-radius: 50%;
}
The element forms a circular border when its border box is square.
The border Shorthand Does Not Set Radius
.example {
border-radius: 12px;
border: 2px solid blue;
}
The border shorthand does not reset border-radius. The element retains its rounded corners.
Working with border-image
The border-image property can draw an element's border using an image or gradient instead of a conventional solid color.
Gradient Border
.gradient-border {
border: 6px solid transparent;
border-image: linear-gradient(90deg, #1769aa, #65c7d0) 1;
}
The border image uses a gradient to paint the border area.
The border Shorthand Resets border-image
.example {
border-image: linear-gradient(90deg, blue, cyan) 1;
border: 4px solid red;
}
The later border declaration resets the border-image properties to their initial values, so the element displays a conventional red border.
Declaration Order
.example {
border: 4px solid transparent;
border-image: linear-gradient(90deg, blue, cyan) 1;
}
Place border-image after the border shorthand when the border image should remain active.
Border images and rounded corners have additional painting rules. A border image does not necessarily follow border-radius in the same way as a conventional border.
border vs. outline
Both border and outline can draw a line around an element, but they behave differently.
| Feature | border |
outline |
|---|---|---|
| Box model | Occupies space in the border box. | Does not occupy space in the box model. |
| Placement | Surrounds the padding and content. | Drawn outside the border edge, subject to outline painting rules. |
| Individual sides | Can style each side separately. | Does not provide equivalent side-specific outline properties. |
| Common use | Element decoration and visual boundaries. | Focus indicators and visual emphasis. |
Using a Border
.button {
border: 2px solid #1769aa;
}
Using an Outline
.button:focus-visible {
outline: 3px solid #1769aa;
outline-offset: 3px;
}
The outline provides a visible focus indicator without changing the element's box dimensions.
Do not remove keyboard focus indicators without providing a clear, accessible replacement.
border Example
The following example compares six common border styles. Each box uses the same border width and color so the differences between the styles are easy to see.
<div class="border-demo">
<div class="demo-box solid">solid</div>
<div class="demo-box dashed">dashed</div>
<div class="demo-box dotted">dotted</div>
<div class="demo-box double">double</div>
<div class="demo-box groove">groove</div>
<div class="demo-box ridge">ridge</div>
</div>
.border-demo {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
gap: 15px;
max-width: 600px;
}
.border-demo .demo-box {
box-sizing: border-box;
padding: 22px 12px;
background-color: #f4f8fc;
color: #173b5e;
font-weight: bold;
text-align: center;
}
.border-demo .solid {
border: 6px solid #1769aa;
}
.border-demo .dashed {
border: 6px dashed #1769aa;
}
.border-demo .dotted {
border: 6px dotted #1769aa;
}
.border-demo .double {
border: 6px double #1769aa;
}
.border-demo .groove {
border: 6px groove #1769aa;
}
.border-demo .ridge {
border: 6px ridge #1769aa;
}
The solid, dashed, dotted, and double styles produce different line patterns. The groove and ridge styles create shaded, three-dimensional effects.
The precise appearance of dotted, dashed, groove, and ridge borders may vary slightly between browsers.
Important Considerations
Keep the following points in mind when using border:
- Shorthand property: The property sets border width, style, and color on all four sides.
- Component order: Width, style, and color can appear in any order.
- Initial values: The defaults are
medium,none, andcurrentColor. - Visible border: A visible border style is normally required for a border to appear.
- Border widths: Widths accept nonnegative lengths or the
thin,medium, andthickkeywords. - No percentages: Percentage values are not valid border widths.
- Color inheritance: Border color defaults to
currentColor, which follows the element's computed text color. - Transparent borders: A transparent border can reserve space without displaying a visible line.
- Shorthand resets: Omitted width, style, and color components are reset to their initial values.
- Individual sides: Use
border-top,border-right,border-bottom, andborder-leftfor different side styles. - Logical borders: Use
border-blockandborder-inlinefor writing-mode-aware layouts. - Box model: Borders contribute to an element's rendered dimensions according to its
box-sizingvalue. - Rounded corners: The
border-radiusproperty is separate and is not reset byborder. - Border images: The
bordershorthand resets theborder-imageproperties. - Collapsed tables: The
hiddenborder style has special precedence when resolving collapsed table borders. - Animation: Border widths and colors can be animated, while changes between border styles are discrete.
- Focus indicators: Borders can help show interactive states, but keyboard focus must remain clearly visible.
- Accessibility: Ensure borders have sufficient contrast when they communicate important boundaries or component states.
Use the border shorthand when all four sides share the same border styling. Combine it with individual side properties, logical border properties, and border-radius when more specific control is needed.
Browser Support
The border property has Baseline Widely available status and is supported in all modern browsers, including Chrome, Edge, Firefox, Safari, and other major browser engines.
Basic border widths, styles, and colors have long-standing browser support. Newer CSS color functions may have separate compatibility requirements.
Standard Border Declaration
.panel {
border: 2px solid #1769aa;
}
This syntax works consistently across modern browsers without vendor prefixes.
Fallback for Newer Color Functions
.panel {
border: 2px solid #1769aa;
border-color: color(display-p3 0.1 0.4 0.7);
}
Browsers that do not support the newer color syntax ignore the second declaration and retain the hexadecimal border color.
Visit Can I Use? to review current browser compatibility for CSS border features.
Specifications
The border shorthand property is defined in the CSS Backgrounds and Borders Module Level 3 specification.
CSS Backgrounds and Borders Module Level 3
Logical border properties are defined in the CSS Logical Properties and Values Module Level 1 specification.
