Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding styles without !important

Tags:

css

How is it possible to override styles specified in another style sheet?

I do not want to use the !important tag to override them. I would much rather specify a new style sheet, put styles in there. Could this be achieved by changing the load order of the style sheets?

like image 406
Hendrik Avatar asked Mar 31 '12 13:03

Hendrik


People also ask

How do you override CSS styles without important?

CSS stands for Cascading Style Sheets, and there's a specific order that styles are applied in, overwriting previous styles. Without going into too much detail: If your rules have the same specificity, just load your stylesheet second and everything will work fine.

How do you avoid important in CSS?

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.

What is the alternative of important in CSS?

important is like a cheat-mode for CSS and it's not really advisable to use it in production. Normally the tools available for you to enable style overrides are: - specificity - the cascade order If you rely on the cascade order too much things can get difficult to debug, so in this situation, I'd use specificity.


2 Answers

It depends. CSS stands for Cascading Style Sheets, and there's a specific order that styles are applied in, overwriting previous styles. Without going into too much detail:

  • If your rules have the same specificity, just load your stylesheet second and everything will work fine.
  • If your rules have higher specificity, the order won't matter.
  • If your rules have lower specificity, you'll need to modify them to match.

So, what's specificity? Basically, it's the sum of each selector in a rule. So this:

a {     background-color: black;     color: white; } 

Has less specificity than this:

body a {     color: orange; } 

ID selectors have higher specificity than class selectors, which have the same specificity as pseudo-class selectors, which have higher specificity than tag selectors. So if all your content is contained in a <div> with an id of content, you would be able to override a style that looks like this:

body a {     border: 0; } 

With:

#content a {     border: 1px solid black; } 
like image 196
Ry- Avatar answered Sep 20 '22 06:09

Ry-


The boostrap stylesheet should be loaded first, your stylesheet second, this way your overwrites will be picked up.

This is called "cascading", from the documentation:

Cascading

This is the capability provided by some style sheet languages such as CSS to allow style information from several sources to be blended together. These could be, for instance, corporate style guidelines, styles common to a group of documents, and styles specific to a single document. By storing these separately, style sheets can be reused, simplifying authoring and making more effective use of network caching. The cascade defines an ordered sequence of style sheets where rules in later sheets have greater precedence than earlier ones. Not all style sheet languages support cascading.

like image 39
Andres Ilich Avatar answered Sep 19 '22 06:09

Andres Ilich