Reputation: 20341
I have 2 CSS styles which pretty much the same except one specific 'width' and the other does not. How can I create a CSS style which inherits another CSS style but override a property?
.FirstStyle
{
margin: 0px 0px 0px 8px;
position: relative;
display: inline-block;
height: 29px;
width: 5px;
float: left;
}
.SecondStyle
{
margin: 0px 0px 0px 8px;
position: relative;
display: inline-block;
height: 29px;
float: left;
}
Upvotes: 0
Views: 2871
Reputation: 2407
You can calculate the CSS specificity value
Notes
:first-line
) get 0,0,0,1 unlike their
pseudo-class brethren which get 0,0,1,0:not()
adds no specificity by itself, only what's
inside it's parentheses.!important
value appended a CSS property value is an automatic
win. It overrides even inline styles from the markup. The only way
an !important
value can be overridden is with another !important
rule declared later in the CSS and with equal or great specificity
value otherwise. You could think of it as adding 1,0,0,0,0 to the
specificity valueUpvotes: 0
Reputation: 16341
How can I create a CSS style which inherits another CSS style but override a property?
The succeeding css styling automatically overrides the preceding property so just add the property below the one you want to override and it'll inherit everything except the new styling specified.
Example:
HTML:
<div class="someClass anotherClass">
....
</div>
CSS:
.someClass {
width: 100px;
color: green;
}
.anotherClass {
width: 200px; <-- This will override the above values
color: red;
}
.someClass {
width: 450px !important; <-- The !important tag will override both suceeding as well as preceeding properties
}
.someClass {
width: 500px; <-- This will override the first two styling properties
}
Upvotes: 1
Reputation: 133400
You can do try this way
In the two class above the width is not defined
.FirstStyle, .SecondStyle
{
margin: 0px 0px 0px 8px;
position: relative;
display: inline-block;
height: 29px;
width: 5px;
float: left;
}
Here you define the width only for the FirstStyle Class .. (and anyway do the fact this class is below the others could override an eventually width specified above )
.FirtsStyle
{
width: 5px;
}
Upvotes: 1