为什么CSS border-color继承了color属性?

前端之家收集整理的这篇文章主要介绍了为什么CSS border-color继承了color属性?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
从font的color属性继承边框颜色是正常的吗?我很惊讶地发现:
div
{
 border: 4px solid;
 color: red;
 height: 100px;
 width: 100px;
}
<div></div>

JSBIN

给了我一个带红色边框的div。通常不指定颜色将默认为黑色。这个奇怪的遗产是什么?

解决方法

根据相关 Backgrounds and Borders Module specsection 4.1,初始边界颜色值为 currentColor

07003

CSS1 and CSS2 defined the initial value of the border-color property to be the value of the color property but did not define a corresponding keyword. This omission was recognized by SVG,and thus SVG 1.0 introduced the currentColor value for the fill,stroke,stop-color,flood-color,and lighting-color properties.

CSS3 extends the color value to include the currentColor keyword to allow its use with all properties that accept a <color> value. This simplifies the definition of those properties in CSS3.

换句话说,在您的情况下,该值被视为以下内容

border: 4px solid currentColor;

因此,您还可以将currentColor值用于诸如background-color属性之类的内容。例如:

div {
  color: red;
  width: 100px;
  height: 100px;
  border: 4px solid;
  background-color: currentColor;
}
<div></div>

很有趣的事实,如果你改变字体颜色(例如:悬停),边框颜色会随之改变! It also works well with transitions!

猜你在找的CSS相关文章