为什么CSS边框颜色继承颜色属性?

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

JSBIN

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

解决方法

根据相关 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>

小乐趣的事实,如果你改变字体颜色(例如:悬停),bordercolor随之改变! It also works well with transitions!

原文链接:/css/219213.html

猜你在找的CSS相关文章