Is there a way to mark a CSS rule as less important, such that it doesn't override a subsequent rule even if the first rule has higher specifically? For example, say I have the following in my CSS file:
#inputDiv input[type="text"]{ width:125px; } #differentInput1{ width:25px; } #differentInput2{ width:500px; }
The idea I was going for is that all text input fields that are children of the div "inputDiv" get a width of 125px, except for certain specific inputs that get some other width. The problem is that the first declaration overrides the specific item declarations.
I've tried the following:
Is there any way to simply say that the items in the first declaration are less important, and shouldn't override anything?
Change your div id="inputDiv" to a class name class="inputDiv" , and change your css selector to . inputDiv . This way your 1st declaration won't override your proceeding declarations. Use LESS or SASS, which allow you to namespace css rules.
The ! important rule in CSS is used to add more importance to a property/value than normal. In fact, if you use the ! important rule, it will override ALL previous styling rules for that specific property on that element!
There's no way to do this since it's antithetical to CSS in the same way that !important
is -- doing the opposite would be just as abusive. Your only option is to rely on selector specificity. You can write this in a way that is not as cumbersome by using a class for inputDiv
instead of an ID, for example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With