Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mark CSS rule as less important?

Tags:

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:

  1. Append !important to each of the specific widths. Works, but many claim (rightly, I think) that !important should be avoided, and it is rather cumbersome as it must be added to each element with a specific width.
  2. Prepend #inputDiv to each of the specific selectors, i.e. #inputDiv #differentInput1 Again, works, and avoids the issues with using !important, but still cumbersome as it has to be done to each element.

Is there any way to simply say that the items in the first declaration are less important, and shouldn't override anything?

like image 492
ibrewster Avatar asked Jun 19 '13 20:06

ibrewster


People also ask

How do you make CSS less important?

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.

How do you mark as important in CSS?

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!


1 Answers

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.

like image 105
Explosion Pills Avatar answered Feb 28 '23 17:02

Explosion Pills