边框颜色将从字体的颜色属性继承是否正常?我很惊讶地发现:
div { border: 4px solid; color: red; height: 100px; width: 100px; }
<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>
小乐趣的事实,如果你改变字体颜色(例如:悬停),bordercolor随之改变! It also works well with transitions!