In CSS (Cascading Style Sheets), you can set the border color of an element using the border-color property.
There are several methods to specify the border color:
Keyword Values:CSS provides some predefined color names that you can use as border colors.
div {
border-color: red;
border-style: solid;
width: 200px;
height: 100px;
}
Common color keywords include red, blue, green, black, white, yellow, purple, orange, and many others.
Hexadecimal color values allow you to define custom colors using a combination of red, green, and blue (RGB) values represented as six-digit codes.
div {
border-color: #FF0000; /* Red */
border-style: solid;
width: 200px;
height: 100px;
}
RGB color values represent the intensity of red, green, and blue as integers in the range 0-255.
div {
border-color: rgb(255, 0, 0); /* Red */
border-style: solid;
width: 200px;
height: 100px;
}
RGBA color values are similar to RGB values but include an additional alpha channel to specify opacity (0 = transparent, 1 = fully opaque)
div {
border-color: rgba(255, 0, 0, 0.5); /* Semi-transparent red */
border-style: solid;
width: 200px;
height: 100px;
}
HSL stands for Hue, Saturation, and Lightness. It provides an alternative way to define colors. HSLA is similar to HSL but includes an alpha channel for opacity.
div {
border-color: hsl(0, 100%, 50%); /* Pure red */
border-color: hsla(0, 100%, 50%, 0.5); /* Semi-transparent red */
border-style: solid;
width: 200px;
height: 100px;
}
You can also define individual colors for each side of the border in the following order: top, right, bottom, and left.
div {
border-color: red green blue yellow; /* Top, Right, Bottom, Left */
border-style: solid;
width: 200px;
height: 100px;
}
If you want an element to inherit the border color you can use the inherit keyword:
div {
border-color: inherit; /* Will have the same border color as the parent */
border-style: solid;
width: 200px;
height: 100px;
}
To make a border transparent, you can use the transparent keyword or rgba() with an alpha value of 0:
div {
border-color: transparent; /* Transparent border */
border-style: solid;
width: 200px;
height: 100px;
}
You can set different border colors for each side of an element using the following individual properties:
div {
/* Applying different border colors to each side */
border-top-color: red; /* Top border is red */
border-right-color: green; /* Right border is green */
border-bottom-color: blue; /* Bottom border is blue */
border-left-color: #FFA500; /* Left border is orange */
border-style: solid;
width: 200px;
height: 100px;
}