CSS border-color Property
The border-color property sets the color of an element's four borders. It is a shorthand that allows you to manage the top, right, bottom, and left border colors in a single declaration.
| <color> | Defines the color using keywords, hex, RGB, RGBA, HSL, or HSLA values. |
| transparent | Sets the border color to be fully see-through, though it still occupies space. |
| inherit | Adopts the border-color value from the parent element. |
| initial | Resets the property to its default value. |
| revert | Resets the property to the user agent's default styling. |
| unset | Resets the property to its natural state, behaving as inherit or initial depending on the property. |
Code Examples
A basic example showing a solid blue border applied to all sides of a div.
<div style="border-width: 5px; border-style: solid; border-color: #3498db; padding: 20px;">
This box has a uniform blue border.
</div>An advanced example using JavaScript to dynamically apply unique colors to each side of the border when the element is clicked.
<div id="colorBox" style="border: 10px solid #ccc; padding: 20px; transition: border-color 0.3s;">
Click me to change my border colors.
</div>
<script>
const box = document.getElementById("colorBox");
box.onclick = function() {
box.style.borderColor = "#e74c3c #2ecc71 #f1c40f #9b59b6";
};
</script>Pro Tip
If you are planning to add a border on hover, set the initial border-color to "transparent" instead of adding the border only on hover. This prevents the layout from "jumping" because the border width is already accounted for in the box model calculation.
Deep Dive
Think of border-color as the paint for the frame around your content box. By default, if you do not specify a color, the browser uses "currentcolor", which matches the text color of the element. However, the color will not appear at all unless you also define a "border-style", because the default style is "none". You can supply one to four values: one value applies to all sides, two values apply to (top/bottom, left/right), three values apply to (top, left/right, bottom), and four values apply to (top, right, bottom, left) in clockwise order. This property is strictly for aesthetics and does not affect the layout dimensions unless the border-width is also modified.
Best Practices
Always pair border-color with border-style and border-width to ensure visibility. Use the shorthand syntax to keep your CSS dry and readable. If you want a consistent look that adapts to theme changes, consider using "currentcolor" so the border naturally follows the text color without manual updates.
Common Pitfalls
The most frequent mistake is setting a border-color and wondering why it is invisible; this usually happens because the "border-style" is missing or set to "none". Also, remember the clockwise mnemonic "TRouBLe" (Top, Right, Bottom, Left) when using four values to avoid putting the wrong color on the wrong side.
Accessibility
Maintain a high contrast ratio between the border color and the background to help users with low vision identify the boundaries of elements like inputs or buttons. Do not rely solely on border color changes to communicate state, such as using only a red border for an error; always include a secondary indicator like an icon or descriptive text.
Dev Data Table: border-color property
| default | currentcolor |
| animatable | yes |
| inherited | no |
| experimental | no |
| year_intro | 1996 |
| year_standard | 1998 |
| js_syntax_1 | element.style.borderColor = "#ff0000"; |
| js_syntax_2 | element.style.setProperty("border-color", "#ff0000"); |
| js_note | When targeting this property in JavaScript, use camelCase for the style object or the full property string with setProperty. |
| browsers | { "Chrome": 1, "Edge": 12, "Firefox": 1, "Safari": 1, "Opera": 3.5, "Chrome Android": 18, "Safari on iOS": 1, "Samsung Internet": "1.0", "Opera Mobile": 12 } |