- Background image
- Border width
- border-color
- border-radius
- Borders
- Class attribute
- Color
- Comment
- First-child selector
- Font family
- Font size
- Font style
- Font weight
- Height
- Linking a style sheet
- Margin
- N-th-child selector
- Overflow property
- padding
- Pixels
- Position property
- Rounding an image
- Selectors
- Text align
- Transition property
- width
CSS
CSS border-color: The Border Color Property in CSS
The CSS border-color
property defines the color of an element's border.
How to Use CSS border-color
The border-color
property works with color names, hex codes, RGB, RGBA, HSL, and HSLA. You can specify a single color to apply for all sides or different colors for each side.
.element {
border-color: blue; /* Single color for all sides */
/* Individual colors for each side */
border-color: red green blue purple;
}
<div class="element" style="border: 4px solid; border-color: red green blue purple;">
Multicolored Border
</div>
When to Use CSS border-color
You need border-color
in CSS to color the border of HTML elements in different scenarios.
Highlighting UI Elements
You can change the border color to highlight interactive elements like buttons or input fields on hover or focus. An e-commerce website might highlight an "Add to Cart" button on hover to emphasize action.
button:hover {
border-color: green;
border-width: 2px;
}
<button style="border: 1px solid; padding: 10px;">Hover over me</button>
Visual Separation
Also, you can use borders with distinct colors to separate different sections of a webpage without solid dividers.
.section {
border-color: #ccc;
border-style: solid;
border-width: 2px;
}
<div class="section" style="margin: 20px;">
<p>Content separated by a border</p>
</div>
Decorative Purposes
Borders can also be decorative, though. For example, you can use multiple border colors to add flair to layouts and graphics.
.fancy-border {
border-style: solid;
border-color: red blue green yellow;
border-width: 5px;
}
<div class="fancy-border" style="padding: 20px;">
Fancy Multicolored Border
</div>
Examples of CSS border-color
Many websites use colored borders to enhance their visual appearance. Here are some simple examples:
Simple Border Color
Content boxes often emphasize specific content. A company blog might use a blue border to highlight its latest articles.
.content-box {
border: 2px solid;
border-color: #0000ff; /* Blue */
padding: 10px;
}
<div class="content-box">
<h2>Here are the latest articles from our company blog</h2>
</div>
Different Colors on Different Sides
A promotional box on a fashion e-commerce website might use a border for emphasis, drawing attention to special deals.
.promo-box {
border-style: solid;
border-color: red green blue yellow;
border-width: 3px;
padding: 15px;
}
<div class="promo-box">
<h2>Boxing Day Sale</h2>
<p>Everything must go!</p>
</div>
Transparent Borders
An online course platform might use transparent borders to add spacing between video thumbnails.
.spacer-div {
border-style: solid;
border-color: transparent;
border-width: 10px;
}
<div class="spacer-div">
Transparent Border Spacer
</div>
Learn More About CSS border-color
Compatibility with Other Border Properties
border-color
works well in conjunction with border-style
and border-width
. In fact, defining the border-style property is necessary for any border color to be visible.
.box {
border-width: 5px;
border-style: solid;
border-color: violet;
padding: 20px;
}
<div class="box">
Box with Violet Border
</div>
border-width
specifies the thickness of the border, and border-style
determines the style, such as solid
, dotted
, or dashed
.
The Border Shorthand Property
You can use the border
shorthand property to set border-width
, border-style
, and border-color
in a single declaration.
.box-shorthand {
border: 3px solid #ff6347; /* Tomato border */
padding: 10px;
}
<div class="box-shorthand">
Shorthand Border Box
</div>
You can omit any of the properties in the shorthand, but setting the border style is mandatory. For instance, border: solid #ff6347
will apply a solid tomato-colored border with a default width of medium
.
Advanced Techniques
For more complex designs, you can combine border-color
with border-radius
and box-shadow
to get enhanced visual effects.
.stylish-box {
border-radius: 8px;
border: 4px solid;
border-color: deepskyblue;
box-shadow: 2px 2px 5px grey;
padding: 20px;
}
<div class="stylish-box">
Stylish Box with Border Radius and Box Shadow
</div>
Individual Borders
You can also specify the color for each side of a border individually. For this purpose, you can set border-top-color
, border-right-color
, border-bottom-color
, and border-left-color
.
.newsletter {
border-style: solid;
border-width: 2px;
border-top-color: red;
border-right-color: blue;
border-bottom-color: green;
border-left-color: yellow;
padding: 15px;
}
<div class="newsletter">
Subscribe to our newsletter!
</div>
Using CSS Variables for Border Colors
Use CSS variables to simplify changing border colors across a website. You can define reusable variables at the root level and use them in different components.
:root {
--primary-border-color: #007bff;
--secondary-border-color: #6c757d;
}
.card-primary {
border: 2px solid var(--primary-border-color);
}
.card-secondary {
border: 2px solid var(--secondary-border-color);
}
<div class="card-primary" style="padding: 10px;">
Primary Border Card
</div>
<div class="card-secondary" style="padding: 10px;">
Secondary Border Card
</div>
Gradient Borders
You can give borders a gradient using background images and border-image
.
.gradient-border {
border: 5px solid transparent;
background-image: linear-gradient(white, white), linear-gradient(to right, red, orange, yellow, green, blue, indigo, violet);
background-origin: border-box;
background-clip: content-box, border-box;
}
<div class="gradient-border" style="padding: 10px;">
Gradient Border Example
</div>
Sign up or download Mimo from the App Store or Google Play to enhance your programming skills and prepare for a career in tech.