85

Say I have a div that uses two css classes that both use text-align, but one is centered and the other is right aligned.

Is it possible to specify something that will give one class priority over the other?

Abe Miessler
  • 82,532
  • 99
  • 305
  • 486

8 Answers8

111
  1. specify a more specific selector, eg prefix an ID before it or prefix the nodename before the class
  2. assign it after the other class
  3. if two classes are in separate files, import the priority file second
  4. !important

!important is the lazy way, but you really should go for #1 to avoid important-ception. Once you've added one !important you can't use it to make some other rule even more important.

Alex Feinman
  • 5,393
  • 1
  • 30
  • 48
meder omuraliev
  • 183,342
  • 71
  • 393
  • 434
  • 6
    +1 But I'd switch 1 and 3; `!important` is a little hackey compared to using CSS specificity. – Richard JP Le Guen Sep 08 '10 at 20:20
  • 4
    @Richard JP Le Guen: I was going through my old, deleted answers and came across my gem of a -1 answer here. These days I auto-downvote `!important` answers that make no mention of specificity as well. I was going to say I left it out of my answer otherwise it'd end up being a long-winded rant, but that's just an excuse ;) – BoltClock Jul 20 '12 at 15:22
  • 1
    @BoltClock - Wow, it's been a while (and I can't see the deleted answer) but I actually remember commenting something like that! Glad to hear you're one of us now - and glad to know you took my down-voting as a source of constructive criticism. Setting a good example for the rest of the SO community :) – Richard JP Le Guen Jul 20 '12 at 15:51
  • Kudos for suggestion 1. – Matt P Nov 18 '15 at 14:02
  • 4
    Does point number 2 apply when you use CSS Modules? It seems to not work for me, where the first class `padding` valie is prevailing. See screenshot: https://imgur.com/gTRifYQ ps: I'm on nextjs with react and bulma – gasbi Apr 18 '20 at 10:40
  • @gasbi same here, had to use `important!`... – User Oct 26 '22 at 05:26
36

If you want to be explicit about it, you can specify how the combination of those two classes work together, by supplying a rule for elements that contain both classes. For instance, you can explicitly give something with both classes foo and bar the same styling as just bar as follows. This works because .foo.bar is more specific than just .foo for elements which have both classes, and thus this rule will take precedence over the .foo rule.

.foo { text-align: center }
.bar, .foo.bar { text-align: right }

If you don't want to be this explicit, you could just place the rule for bar after the rule for foo, as given selectors of the same specificity, later rules take precedence over earlier ones:

.foo { text-align: center }
.bar { text-align: right }

You can learn more about how precedence between rules is determined in the CSS specification chapter about the cascade; that's the "C" of CSS, and is important to understand well in order to take full advantage of CSS.

Brian Campbell
  • 322,767
  • 57
  • 360
  • 340
16

You should use CSS specificity to override previous declarations http://htmldog.com/guides/cssadvanced/specificity/

p = 1 point
.column = 10 points
#wrap = 100 points

So: p.column { text-align: right; } can be overwritten by: body p.column { text-align: left; }

Johan
  • 1,958
  • 11
  • 20
  • 5
    Great article. For one who sees "specificity" very often but never completely understood its meaning, this was liquid gold. I finally have a mathematical rule to apply when CSS conflicts occur. – GigiSan Apr 11 '16 at 12:42
7

as “meder omuraliev” has answered, you may use a more specified selector. and I would like to provider a general way that how to sepcific a higher priority for any type of selector, that is use the attr presdeo.

for example:

html body .foo { font-family: Arial !important;}
html body .bar[attr]{ font-family: Arial !important;}

to override this you may use like this:

html body .foo:not([NONE_EXISTS_ATTR]){ font-family: Consolas !important;}
html body .bar[attr]:not([NONE_EXISTS_ATTR]){ font-family: Consolas !important;}
dexiang
  • 1,345
  • 16
  • 23
5

To add to the other answers, you don't need to add selectors not related to what you originally wanted to increase specificity, the same can be achieved by repeating the same selector multiple times:

.foo.foo takes precedence over .foo, and .foo.foo.foo takes precedence over the previous ones.

This is better than adding non-related selectors, because you only select what you really want to select. Otherwise you might get unexpected behaviour when unrelated stuff you added changes.

2
.bar { text-align: right !important;}
ABHAY JOHRI
  • 1,997
  • 15
  • 19
0

use !important

Example :

p {
  background-color: red !important;
}
Mounesh
  • 561
  • 5
  • 18
0

And last option maybe remove classes by JS ?

function removeCssClasses(src, list)
{
    // get all objects containing src class
    var objs = document.getElementsByClassName(src);
    for(var i=0;i<objs.length;i++)
    {
        var cls = objs[i].classList;
        var j = cls.length;
        while (j--) {
            // is particular object class in remove list, but ignore src class
            if (cls[j] == src) continue;
            var pos = list.indexOf(cls[j]);
            if(pos > -1)
            {
                objs[i].classList.remove(cls[j]);
            }
        }
    }
}

document.body.onload = function() {
    removeCssClasses("yourNewClass", ["Unwanted1", "Unwanted2"]);
    removeCssClasses("yourNewClass2", ["Unwanted3"]);
}
Jan
  • 2,178
  • 3
  • 14
  • 26