Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override !important?

Tags:

css

I have created a custom style sheet that overrides the original CSS for my Wordpress template. However, on my calendar page, the original CSS has the height of each table cell set with the !important declaration:

td {height: 100px !important}

Is there some way I can override this?

like image 523
user1444027 Avatar asked Jun 24 '12 15:06

user1444027


People also ask

Can you override an important?

important at all cost. This is a dirty, dirty hack, but you can override an ! important, without an ! important, by using an (infinitely looping or very long lasting) animation on the property you're trying to override the importants on.

What can override an important declaration?

The only way to override an ! important rule is to include another ! important rule on a declaration with the same (or higher) specificity in the source code - and here the problem starts!

How do you avoid important?

To avoid using ! important , all you need to do is increase specificity. In your case, both of your selectors have identical specificity. The issue is most likely caused by your media query being placed before your "Normal CSS", and thus getting overridden.


9 Answers

Overriding the !important modifier

  1. Simply add another CSS rule with !important, and give the selector a higher specificity (adding an additional tag, id or class to the selector)
  2. add a CSS rule with the same selector at a later point than the existing one (in a tie, the last one defined wins).

Some examples with a higher specificity (first is highest/overrides, third is lowest):

table td    {height: 50px !important;}
.myTable td {height: 50px !important;}
#myTable td {height: 50px !important;}

Or add the same selector after the existing one:

td {height: 50px !important;}

Disclaimer:

It's almost never a good idea to use !important. This is bad engineering by the creators of the WordPress template. In viral fashion, it forces users of the template to add their own !important modifiers to override it, and it limits the options for overriding it via JavaScript.

But, it's useful to know how to override it, if you sometimes have to.

like image 188
Matt Coughlin Avatar answered Oct 04 '22 08:10

Matt Coughlin


Apart from overriding a style set by the style attribute, the !important should only be used when you have selectors in your style sheet with conflicting specificity.

But even when you have conflicting specificity, it is better to create a more specific selector for the exception. In your case it's better to have a class in your HTML which you can use to create a more specific selector which doesn't need the !important rule.

td.a-semantic-class-name { height: 100px; }

I personally never use !important in my style sheets. Remember that the C in CSS is for cascading. Using !important will break this.

like image 39
Jasper de Vries Avatar answered Oct 04 '22 08:10

Jasper de Vries


Disclaimer: Avoid !important at all cost.

This is a dirty, dirty hack, but you can override an !important, without an !important, by using an (infinitely looping or very long lasting) animation on the property you're trying to override the importants on.

@keyframes forceYellow {
  from {
    background-color: yellow;
  }
  to {
    background-color: yellow;
  }
}

div {
  width: 100px;
  height: 100px;
  margin: 0 auto;
  background: red !important;
  animation: 1s linear infinite forceYellow;
}
<div></div>
like image 36
mariomc Avatar answered Oct 04 '22 08:10

mariomc


Every part of the styles name has a weight, so the more elements you have that relate to that style the more important it is. For example

#P1 .Page {height:100px;}

is more important than:

.Page {height:100px;}

So when using important, ideally this should only ever be used, when really really needed. So to override the declaration, make the style more specific, but also with an override. See below:

td {width:100px !important;}
table tr td .override {width:150px !important;}
like image 43
KM123 Avatar answered Oct 04 '22 07:10

KM123


Override using JavaScript

$('.mytable td').attr('style', 'display: none !important');

Worked for me.

like image 30
Manish Shrivastava Avatar answered Oct 04 '22 07:10

Manish Shrivastava


This can help too

td[style] {height: 50px !important;}

This will override any inline style

like image 33
DiChrist Avatar answered Oct 04 '22 07:10

DiChrist


In any case, you can override height with max-height.

like image 38
pasquale Avatar answered Oct 04 '22 07:10

pasquale


You can use higher specificity by going up in selectors.

td {height: 100px !important}
/* higher precedence */
table td {height: 200px !important}

I wrote a detailed article on how to override CSS here.

like image 21
Jignesh Avatar answered Oct 04 '22 08:10

Jignesh


I found really cool trick with :not. If nothing from above helped you, then try this:

.page-width:not(.anything) {

}
like image 28
fdrv Avatar answered Oct 04 '22 06:10

fdrv