349

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
  • 153,850
  • 22
  • 249
  • 325
user1444027
  • 4,983
  • 7
  • 29
  • 39
  • 6
    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
  • 2
    Which sheet comes last, yours or the template's? – j08691 Jun 24 '12 at 15:30
  • 11
    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
  • Also see [specificity: avoiding and overriding !important](https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity#avoiding_and_overriding_!important) – djvg Jun 14 '23 at 13:00

11 Answers11

477

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
  • 6,481
  • 6
  • 42
  • 50
Matt Coughlin
  • 18,666
  • 3
  • 46
  • 59
  • 12
    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
  • 2
    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
  • 3
    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
  • This solution definitely works. Just in case, if you don't have the luxury to modify the HTML markup to add more classes, you can use same class multiple times to increase the specificity of selector. Like `.class1.class1 { color: red; }` – solo_assassin Jan 27 '23 at 13:41
36

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.

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
22

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;}
TylerH
  • 20,799
  • 66
  • 75
  • 101
KM123
  • 1,339
  • 1
  • 10
  • 21
  • 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
15

Override using JavaScript

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

Worked for me.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Manish Shrivastava
  • 30,617
  • 13
  • 97
  • 101
  • 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
  • 1
    Perfect! And if you put this in the $(document).ready(function (e) { your CSS is done loading and doesn't have a say anymore! :-) Which means you might be able to lose the !important in the attr tag. – Chiwda Apr 04 '22 at 15:27
7

This can help too

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

This will override any inline style

Musakkhir Sayyed
  • 7,012
  • 13
  • 42
  • 65
DiChrist
  • 351
  • 7
  • 18
  • 1
    Your code does not override `this` because, according to [CSS Specificity selector types](https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity#selector_types), "Inline styles added to an element (e.g., style='font-weight: bold;') always overwrite any styles in external stylesheets, and thus can be thought of as having the highest specificity." – Adam Katz Apr 06 '22 at 19:24
5

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.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Jignesh
  • 51
  • 1
  • 1
  • Please remember that explicit disclosure of affiliation is required when posting links, otherwise it counts as spam. – TylerH Sep 13 '22 at 13:30
4

There are a couple of modern approaches that weren't available when this question was first asked.

  1. Use :is() to set an arbitrarily high specificity to your selector

    :is(td, #A#A#A:not(*)) {height: 200px !important}

    The second selector of the selector list in the :is() sets the specificity of the whole selector but the :not(*) part means that the selector will never match any element itself. #A#A#A gives a specificity of (3,0,0) but you can safely choose whatever selector is sufficient to override the other !important setting. However, this is still something of a hack.

  2. A better way is to use cascade layers. Layered !important declarations override non-layered !important declarations so you can just do:

    @layer { 
       td {height: 200px !important} 
    }
    

    By using named layers you can further override this to arbitrary levels.

Note that neither approach will allow you to override a !important setting in an HTML style attribute.

Alohci
  • 78,296
  • 16
  • 112
  • 156
3

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

TylerH
  • 20,799
  • 66
  • 75
  • 101
pasquale
  • 101
  • 1
  • Yes, if you want the result to be less high, if you want it to be higher, use `min-height` to overwrite. – RiZKiT Jan 30 '23 at 14:57
3

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

.page-width:not(.anything) {

}
fdrv
  • 852
  • 1
  • 11
  • 21
  • 1
    I was dealing with a badly designed third-party app that was specifying virtually everything with an `!important` tag. None of the other suggestions worked for me but this one did. (Thank you!) Browser compatibility looks good for my use-case: https://developer.mozilla.org/en-US/docs/Web/CSS/:not#browser_compatibility Here's a list of gotchas to be aware of when using `:not`, as quite a few things can go wrong: https://developer.mozilla.org/en-US/docs/Web/CSS/:not#description – moertel Sep 18 '22 at 10:15
  • This worked for me. I'm editing a wordpress template, which has some !important that i needed to override. Cool hack, huh! – Patricio Villarroel Jun 07 '23 at 00:22
0

If you're trying to override an !important tag that is defined in a class. Simply specify your property in a id tag. id has higher precedence than class.

-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,528
  • 4
  • 27
  • 47