How to Give a CSS Class Priority Over an Id

How do I give a CSS class priority over an id?

Do not use !important because it is the worst solution, if you want to switch the styling then do it that way.

#idname{  border: 2px solid black;}
#idname.classname { border: 2px solid gray;}
<div id = "idname" class="classname">it is a test</div>

Is it possible to give one CSS class priority over another?

  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.

How to prioritize a CSS class?

With CSS Specificity, you should just override the more specific selector with your own.

.panel-default > .panel-heading .badge.alert-success {
color: #3c763d;
background-color: #dff0d8;
border-color: #d6e9c6;
}

Edit: Other options can include:

  1. Removing/Altering the offending CSS file (difficult with bootstrap, but doable, and maintenance headache if you decide to update bootstrap.css)

    • Update/remove offending selectors
    • Adding :not() selector to avoid certain scenarios
  2. Altering your HTML Structure so that it does NOT follow the offending CSS selectors.

    • Changing class names and/or using new ones
    • Inserting/Deleting additional nested divs/elements (to avoid > direct-child selector)

Thankfully, if you have lots of classes to update, a smart regex replace is your best friend, but that's another topic.

CSS, why won't my ID take priority over my class?

position:absolute is the culprit as it will make the width shrink-to-fit thus there is nothing to align since the content will define the width

Add border to better see the issue:

.stats {  text-align: left;  position: absolute;  font-family: guy;  font-size: 20px;  bottom: 0;  border:1px solid;}
#queue { text-align: right;}
<div class="stats">  <p>Online Users: 100</p>  <p id="queue">Players in Queue: 1</p></div>

CSS class priority

CSS is all about context.

The style an element receives depends on:

  1. Specificity
  2. Position in the cascade

Specificity

  • p {} is less specific than
  • p.introduction {} which is less specific than
  • article p.introduction {} which is less specific than
  • article#profile p.introduction {} etc.

Cascade

For situations where specificity is equivalent...

  • In article p {color: green;} body p {color: red;} the paragraph text will be red.

  • But in body p {color: red;} article p {color: green;} the paragraph text will be green.


So, although you write:

I would like to have a class [...] called .themecolor that I can put on
any element that I want to take on that background-color.

as you will have seen, using:

.themecolor {
background-color: rgb(47, 86, 111);
}

will often not work - because, your .themecolor declaration might easily (albeit accidentally) get overridden by a more specific selector elsewhere in your stylesheet, or by a selector of equivalent specificity further down the cascade.

Better then, to group selectors together, using your .themecolor as a quasi-variable:

.THEMECOLOR,
.navbar.themecolor,
.glyphicon.themecolor {
background-color: rgb(47, 86, 111);
}

You will then be able to add as many new elements to this list as you wish, always stating the main Bootstrap class of the element immediately followed by .themecolor:

.THEMECOLOR,
.class1.themecolor,
.class2.themecolor,
.class3.themecolor,
.class4.themecolor,
.class5.themecolor,
.class6.themecolor {
background-color: rgb(47, 86, 111);
}

N.B. The syntax .class1.themecolor means:

When an element has .class1 and it has .themecolor

Class selector priority over several unique ID's

Absolutely, you can keep nesting your styles in order to increase the specificity and target the element.

You could use !important but that should be a last option. Otherwise your code will not be scalable after a certain point and is considered a worst practice.

And put, id's first since they carry more weight (Just a pattern which in no way affects specificity)

#green-text.white-text {
color: white;
}

Once you associate a style to an id.

  • The only way to override would be is to use !important (Avoid it)
  • Use a more specific style (which is always nested inside the id so
    that you are targeting it).
  • Use inline style which has a better specificity than the id

In the long run it won't be maintainable. So it is always a better idea to replace it with a class instead

.green-text.white-up-text {
color: white;
}
.grey-text.white-up-text {
color: white;
}
.orange-text.white-up-text {
color: white;
}

Also take a look at OOCSS which advocates the principle of code reuse, where classes are the king.

CSS ID starting with some string having highest priority

One attribute selector is equally specific to one class selector. But here you have two class selectors, which is more specific than just one attribute selector. And since both rules have important width declarations, the order of precedence is not changed at all by !important — the first rule will override the second by specificity alone.

You can fix this by balancing the specificity, by adding the two class selectors to the second rule:

.someClass.modal{    width:100px !important;    border:1px solid #4c4c4c;}.someClass.modal[id^=prod]{      width:50px !important;}
<div id="product1" class="modal someClass">Test</div><div id="product2" class="modal someClass">Test</div><div id="product3" class="modal someClass">Test</div><div id="product4" class="modal someClass">Test</div>

What is the order of precedence for CSS?

There are several rules ( applied in this order ) :

  1. inline css ( html style attribute ) overrides css rules in style tag and css file
  2. a more specific selector takes precedence over a less specific one
  3. rules that appear later in the code override earlier rules if both have the same specificity.
  4. A css rule with !important always takes precedence.

In your case its rule 3 that applies.

Specificity for single selectors from highest to lowest:

  • ids (example: #main selects <div id="main">)
  • classes (ex.: .myclass), attribute selectors (ex.: [href=^https:]) and pseudo-classes (ex.: :hover)
  • elements (ex.: div) and pseudo-elements (ex.: ::before)

To compare the specificity of two combined selectors, compare the number of occurences of single selectors of each of the specificity groups above.

Example: compare #nav ul li a:hover to #nav ul li.active a::after

  • count the number of id selectors: there is one for each (#nav)
  • count the number of class selectors: there is one for each (:hover and .active)
  • count the number of element selectors: there are 3 (ul li a) for the first and 4 for the second (ul li a ::after), thus the second combined selector is more specific.

A good article about css selector specificity.

Css class priority when Adding Dynamic Class

Your div is red instead of blue because of your style declaration order:

.hello {
background-color: blue;
}

.mystyle {
background-color: red;
}

CSS specificity is not dependent on the order in which you apply the class to an element, but rather the order in which you declare the class in the style. In this case, you have declared .hello then .mystyle, meaning .mystyle has high specificity compared to the .hello and hence, you get the red background.

Why isn't the CSS ID the highest priority CSS rule in this case?

If you were trying to override a selector that didn't have an ID with one that did, your confusion would be spot on.

In this case, though, you're using #get_started (which has an ID) to override #header ul li a which also has an ID in it.

Surely you're thinking that your ID is more specific than the #header ID — and you're not entirely wrong — but that's not how CSS works. It only cares about the number of IDs used in the selector, not which elements those IDs happen to target.

To figure out which of two selectors takes precedence, first count the IDs in each. If one has more IDs than the other, it wins and you're done.

If it's the same number (in this case both have one) move on to the number of classes. Again, if one has more classes than the other, it wins and you're done. In this case, both have zero classes.

So we move on to the number of tag names. Once again, if one has more tag names it wins. And here #header ul li a has three tags in it (ul, li, and a) whereas #get_started has none. You lose. The existing selector wins.

You could work around this with just #header #get_started which now has two IDs, but my preference would be to more clearly describe it as:

#header ul li a#get_started


Related Topics



Leave a reply



Submit