Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this CSS class styling order-dependent?

Tags:

css

.container {
   border: 4px solid;
}

and another class

.border-red {
   border-color:red;
}

and my html

<div class="container border-red"> </div>

The border color does not apply to the element unless i put the .border-red class before .container

Please check for demo JSFIddle

like image 914
Mustafa Avatar asked Feb 15 '26 15:02

Mustafa


2 Answers

The reason is that this

.container {
   border: 4px solid;
}

is actually shorthand for

.container {
   border-width: 4px;
   border-style: solid;
   border-color: ** current font color **;
}

Consequently, the later style overrides the previous border-color declaration.

Border @ MDN

like image 153
Paulie_D Avatar answered Feb 17 '26 06:02

Paulie_D


It is order dependent because border: is a shorthand of border-style: + border-width: + border-color:.

like image 31
Mihai Stancu Avatar answered Feb 17 '26 06:02

Mihai Stancu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!