Skip to main content

How to Change the Border Color in CSS

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

border-color changes the color of a border that's already there, and leaves its width and style alone. That makes it ideal for a state such as an error: one class turns a text box's grey border red.

Syntax​

Syntax
<selector> {
border-color: <color>;
}
PartWhat it does
<selector>The element whose border to recolor. It needs a border with a style already.
<color>The new color. Two values color the top and bottom, then the left and right.

MDN's border-color page shows how one to four values color the sides.

The Example​

The example is a form with two text boxes. Both have a grey border, and the Email box, whose value is incomplete, has the class error, which turns its border red.

Step 1: Write the Code​

Create a file named index.html in a text editor, such as Visual Studio Code:

index.html, an HTML page with a style element holding a rule giving inputs a 1px solid 8a8886 border and a rule for the class error that sets border-color to c50f1f, and two labelled text boxes, Name with the value Ana and Email with the class error and the value ana@example.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Border color</title>
<style>
input {
border: 1px solid #8a8886;
}

.error {
border-color: #c50f1f;
}
</style>
</head>
<body>
<p><label>Name <input value="Ana"></label></p>
<p><label>Email <input class="error" value="ana@example"></label></p>
</body>
</html>

The input rule gives every text box a 1-pixel solid grey border. The .error rule changes only the color.

Step 2: Open It in a Browser​

Double-click index.html. It opens in your browser:

The page in Chromium: a Name text box with a grey border holding Ana, and an Email text box with a red border holding ana@example.

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.

What Happened​

  • The Email box has a red border, rgb(197, 15, 31), still 1 pixel wide and solid.
  • The Name box kept the grey, rgb(138, 136, 134), because it doesn't have the class.

Common Mistakes​

MistakeWhat happensDo this instead
Setting only border-color on an element with no borderNo border appears: the paragraph's border style stayed none and its width 0pxGive it a border first, such as border: 1px solid

Troubleshooting​

SymptomLikely causeFix
No border appearsThe element has no border styleAdd border with a style first
The color doesn't changeThe class isn't on the element, or the selector doesn't matchCheck the class in the HTML

FAQ​

Can each side have its own color?​

Yes. With two values, border-color: #c50f1f #0f6cbd, the top and bottom were red and the left and right blue.

Does border-color change the width?​

No. The red border was still 1 pixel wide, as the input rule set it.

Conclusion​

Give the element its border once, then use border-color wherever only the color should change, such as in an error class.

Additional Resources​