282

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?

Patrick Hofman
  • 143,714
  • 19
  • 222
  • 294
user1444027
  • 4,163
  • 7
  • 24
  • 34
  • 4
    Have you tried using `!important`, too? If your CSS sheet is defined after the original template, it should work well. – Petr Janeček Jun 24 '12 at 15:30
  • 1
    Which sheet comes last, yours or the template's? – j08691 Jun 24 '12 at 15:30
  • 78
    This is why `!important` is considered harmful. – Spudley Jun 24 '12 at 15:30
  • 10
    The most powerful way is like so: td[style] { height: 110px !important; }. it acts as if you injected the style inline to the html because you are applying the styles to the actual style attribute of the tag. – DMTintner Jun 14 '13 at 21:56

8 Answers8

401

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.

Andrew Koper
  • 4,049
  • 5
  • 32
  • 41
Matt Coughlin
  • 17,170
  • 3
  • 42
  • 57
  • 8
    I know that this is a bit on the older side of answers, but can you perhaps add a comment that this is an extremely bad way of authoring your CSS (or state why it is acceptable if you disagree)? I've seen people refer to this answer... :) – ZenMaster Jan 12 '13 at 05:51
  • Great answer. I agree, its a mess. It forces me to hack around their hack, then someone to hack around my hack, etc.. In my case I have to deal with templates that pull their CSS from a database somewhere. I grep the DB dump and it shows me it comes from a 1MB json blob. Not very useful to me in finding where to change it, forcing me to add CSS to a code file, the way it should be done, except with these nasty hacks. – Josh Ribakoff Oct 17 '13 at 14:52
  • See also: [Relationship between !important and CSS specificity](http://stackoverflow.com/questions/5805040/important-in-css-specificity-points) – BoltClock May 13 '14 at 05:19
  • it might be a good idea to add the tag identifier also even though it slows down the css. Just had to fix one where I had to put in div#header.class { style: ; } in order for it to take... very annoying – Sparatan117 Aug 12 '14 at 19:53
  • 1
    There are good uses for `!important`. Like a browser- or site-wide style override from a Stylish, AdBlock, or uBlock script. Or when you have no reasonably easy access to the base CSS, which may be very complex, spread through many files, and change over time (and may also use `!important`). Like any tool, the positive or negative potential is based on how you use it. People need to stop going ape whenever things like this or goto/eval/globals/etc. are mentioned. Might as well say "avoid WordPress" or "avoid CSS" because the potential for "messing things up" is too great. – Beejor Dec 02 '18 at 22:27
  • For more info on `!important`, its history, and the intention behind it, here's a good read: https://www.smashingmagazine.com/2010/11/the-important-css-declaration-how-and-when-to-use-it/ – Beejor Dec 02 '18 at 22:33
32

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.

Jasper de Vries
  • 13,693
  • 6
  • 54
  • 86
  • 17
    This is not true. An `!important` rule in the stylesheet will override a normal rule in a `style` tag, for example. However, in some situations it will appear that a more specific rule overrides it. For example, if you set a `font-size: 24pt !important` on `body`, you can still override it with a `font-size: 12pt` on `p`. This is because you are not overriding the `!important` rule, you are overriding the implicit `font-size: inherit` on `p`. – meustrus Mar 27 '14 at 19:24
23

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>
mariomc
  • 853
  • 1
  • 9
  • 13
16

Okay here is a quick lesson about CSS Importance. I hope that the below helps!

First of all the every part of the styles name as a weighting, 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 overide the decleration, make the style more specific, but also with an override. See below:

td {width:100px !important;}
table tr td .override {width:150px !important;}

I hope this helps!!!

KM123
  • 1,199
  • 1
  • 9
  • 20
  • two `!important` can be override by re-define **.old-class-name {bla bla bla}** as **.old-class-name.overrided {bla bla bla}** in `` tag above the component, it's work for me – Tarek Kalaji Sep 24 '19 at 08:44
13

Override using JavaScript

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

Worked for me.

TylerH
  • 19,065
  • 49
  • 65
  • 86
Manish Shrivastava
  • 26,075
  • 13
  • 88
  • 100
  • I thought inline styles always override a parent's important! flag. – Joshua Ramirez May 28 '15 at 19:26
  • 3
    @JoshuaRamirez No they actually don't override an important flag unless you put the !important flag inline as well – Cam Mar 03 '16 at 18:37
  • 1
    If you're going to use JS, you might as well just delete the element entirely, unless there's a remote possibility you'll need it again. Stacking !important via JS just brings you back to the original question. – SilverbackNet Jun 06 '16 at 10:37
  • @SilverbackNet As per my understanding, Overriding using JS is more accurate and usually works as CSS loads in sequence and JS jquery block loads after everything gets loaded. So, It overrides the changes done by css independent of css sequences. – Manish Shrivastava Jun 06 '16 at 10:56
  • overriding like this doesn’t work for style attributes. – user2284570 Oct 03 '16 at 21:02
  • or `$(".node-with-inline-rule").removeAttr("style")` – Alex Szücs Mar 21 '21 at 12:15
6

This can help too

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

This will override any inline style

Musakkhir Sayyed
  • 6,208
  • 13
  • 37
  • 60
DiChrist
  • 269
  • 4
  • 13
1

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

TylerH
  • 19,065
  • 49
  • 65
  • 86
pasquale
  • 65
  • 1
-4

I would like to add an answer to this that hasn't been mentioned, as I have tried all of the above to no avail. My specific situation is that I am using semantic-ui, which has built in !important attributes on elements (extremely annoying). I tried everything to override it, only in the end did one thing work (using jquery). It is as follows:

$('.active').css('cssText', 'border-radius: 0px !important');
IWI
  • 1,150
  • 1
  • 18
  • 35