从font的color属性继承边框颜色是正常的吗?我很惊讶地发现:
div { border: 4px solid; color: red; height: 100px; width: 100px; }
<div></div>
给了我一个带红色边框的div。通常不指定颜色将默认为黑色。这个奇怪的遗产是什么?
解决方法
根据相关
Backgrounds and Borders Module spec的
section 4.1,初始边界颜色值为
currentColor
:
07003
CSS1 and CSS2 defined the initial value of the
border-color
property to be the value of thecolor
property but did not define a corresponding keyword. This omission was recognized by SVG,and thus SVG 1.0 introduced thecurrentColor
value for thefill
,stroke
,stop-color
,flood-color
,andlighting-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!