border-color - CSS: Cascading Style Sheets 编辑
The border-color
shorthand CSS property sets the color of an element's border.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Each side can be set individually using border-top-color
, border-right-color
, border-bottom-color
, and border-left-color
; or using the writing mode-aware border-block-start-color
, border-block-end-color
, border-inline-start-color
, and border-inline-end-color
.
You can find more information about border colors in Borders in Applying color to HTML elements using CSS.
Constituent properties
This property is a shorthand for the following CSS properties:
Syntax
/* <color> values */
border-color: red;
/* horizontal | vertical */
border-color: red #f015ca;
/* top | vertical | bottom */
border-color: red rgb(240,30,50,.7) green;
/* top | right | bottom | left */
border-color: red yellow green blue;
/* Global values */
border-color: inherit;
border-color: initial;
border-color: unset;
The border-color
property may be specified using one, two, three, or four values.
- When one value is specified, it applies the same color to all four sides.
- When two values are specified, the first color applies to the top and bottom, the second to the left and right.
- When three values are specified, the first color applies to the top, the second to the left and right, the third to the bottom.
- When four values are specified, the colors apply to the top, right, bottom, and left in that order (clockwise).
Values
<color>
- Defines the color of the border.
Formal definition
Initial value | as each of the properties of the shorthand:
|
---|---|
Applies to | all elements. It also applies to ::first-letter . |
Inherited | no |
Computed value | as each of the properties of the shorthand:
|
Animation type | as each of the properties of the shorthand: |
Formal syntax
<color>{1,4}where
<color> = <rgb()> | <rgba()> | <hsl()> | <hsla()> | <hex-color> | <named-color> | currentcolor | <deprecated-system-color>
where
<rgb()> = rgb( <percentage>{3} [ / <alpha-value> ]? ) | rgb( <number>{3} [ / <alpha-value> ]? ) | rgb( <percentage>#{3} , <alpha-value>? ) | rgb( <number>#{3} , <alpha-value>? )
<rgba()> = rgba( <percentage>{3} [ / <alpha-value> ]? ) | rgba( <number>{3} [ / <alpha-value> ]? ) | rgba( <percentage>#{3} , <alpha-value>? ) | rgba( <number>#{3} , <alpha-value>? )
<hsl()> = hsl( <hue> <percentage> <percentage> [ / <alpha-value> ]? ) | hsl( <hue>, <percentage>, <percentage>, <alpha-value>? )
<hsla()> = hsla( <hue> <percentage> <percentage> [ / <alpha-value> ]? ) | hsla( <hue>, <percentage>, <percentage>, <alpha-value>? )where
<alpha-value> = <number> | <percentage>
<hue> = <number> | <angle>
Examples
Complete border-color usage
HTML
<div id="justone">
<p><code>border-color: red;</code> is equivalent to</p>
<ul><li><code>border-top-color: red;</code></li>
<li><code>border-right-color: red;</code></li>
<li><code>border-bottom-color: red;</code></li>
<li><code>border-left-color: red;</code></li>
</ul>
</div>
<div id="horzvert">
<p><code>border-color: gold red;</code> is equivalent to</p>
<ul><li><code>border-top-color: gold;</code></li>
<li><code>border-right-color: red;</code></li>
<li><code>border-bottom-color: gold;</code></li>
<li><code>border-left-color: red;</code></li>
</ul>
</div>
<div id="topvertbott">
<p><code>border-color: red cyan gold;</code> is equivalent to</p>
<ul><li><code>border-top-color: red;</code></li>
<li><code>border-right-color: cyan;</code></li>
<li><code>border-bottom-color: gold;</code></li>
<li><code>border-left-color: cyan;</code></li>
</ul>
</div>
<div id="trbl">
<p><code>border-color: red cyan black gold;</code> is equivalent to</p>
<ul><li><code>border-top-color: red;</code></li>
<li><code>border-right-color: cyan;</code></li>
<li><code>border-bottom-color: black;</code></li>
<li><code>border-left-color: gold;</code></li>
</ul>
</div>
CSS
#justone {
border-color: red;
}
#horzvert {
border-color: gold red;
}
#topvertbott {
border-color: red cyan gold;
}
#trbl {
border-color: red cyan black gold;
}
/* Set width and style for all divs */
div {
border: solid 0.3em;
width: auto;
margin: 0.5em;
padding: 0.5em;
}
ul {
margin: 0;
list-style: none;
}
Result
Specifications
Specification | Status | Comment |
---|---|---|
CSS Logical Properties and Values Level 1 | Editor's Draft | Added the logical keyword. |
CSS Backgrounds and Borders Module Level 3 The definition of 'border-color' in that specification. | Candidate Recommendation | The transparent keyword has been removed as it is now a part of the <color> data type. |
CSS Level 2 (Revision 1) The definition of 'border-color' in that specification. | Recommendation | The property is now a shorthand property. |
CSS Level 1 The definition of 'border-color' in that specification. | Recommendation | Initial definition. |
Browser compatibility
BCD tables only load in the browser
See also
- Border-color related CSS properties:
border
,border-top-color
,border-right-color
,border-bottom-color
,border-left-color
, - Other border-related CSS properties:
border-width
,border-style
- The
<color>
data type - Other color-related properties:
color
,background-color
,outline-color
,text-decoration-color
,text-emphasis-color
,text-shadow
,caret-color
, andcolumn-rule-color
- Applying color to HTML elements using CSS
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论