Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the CSS border-color inheriting the color property?

Tags:

css

Is it normal that a border color would be inherited from font's color property? I was surprised to find that:

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

JSBIN

gives me a div with a red border. Usually not specifying a color will default to black. What is this odd inheritance?

enter image description here

like image 803
1252748 Avatar asked Jan 08 '16 00:01

1252748


People also ask

Is Border color a CSS property?

The border-color shorthand CSS property sets the color of an element's border.

Is Border inherited CSS?

While this question uses the word "inherit", it should be noted that specifying border-color: inherit does not inherit from the color property — like all other CSS properties, it inherits from the border-color of the parent element.

Which CSS property is used to specify the color of a border?

The border-color property is used to add color to the border of an element. The border-color property will only work when the border-style property is defined first, which is used to set the borders. This property will not work alone.

What is the border-color property in CSS?

The border-color property sets the color of an element's four borders. This property can have from one to four values. Note: Always declare the border-style property before the border-color property. An element must have borders before you can change the color.

Does the border-color inherit the color of the parent element?

While this question uses the word "inherit", it should be noted that specifying border-color: inherit does not inherit from the color property — like all other CSS properties, it inherits from the border-color of the parent element.

Is it possible to change the color of an element's borders?

More "Try it Yourself" examples below. The border-color property sets the color of an element's four borders. This property can have from one to four values. Note: Always declare the border-style property before the border-color property. An element must have borders before you can change the color. yes.

What is the use of border color?

Definition and Usage. The border-color property sets the color of an element's four borders. This property can have from one to four values. If the border-color property has four values: border-color: red green blue pink; top border is red. right border is green.


1 Answers

Based on section 4.1 of the relevant Backgrounds and Borders Module spec, the initial border-color value is currentColor:

CSS Color Module - 4.4. currentColor color keyword

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.

In other words, the value is treated as the following in your case:

border: 4px solid currentColor; 

Therefore you could also use the currentColor value for something such as the background-color property. For instance:

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

Small fun fact, if you change the font color (e.g. :hover), the bordercolor changes with it! It also works well with transitions!

like image 151
Josh Crozier Avatar answered Sep 18 '22 11:09

Josh Crozier