Can One CSS File Take Priority Over Another CSS File

Can one CSS file take priority over another CSS file?

It depends on how you set them in your header. So something like this will work:

<link rel="old stylesheet" href="path/to/style.css" />
<link rel="newer stylesheet" href="path/to/style.css" />
<link rel="newest stylesheet" href="path/to/style.css" />

The last one will be picked up.

And an helpful link about stylesheets here:
http://www.w3.org/TR/html401/present/styles.html#h-14.3.2

See also: Precedence in CSS if the above doesn't work for you.

Hope it is clear.

Priority in multiple css files in a web page

While linking the css files, move the high priority file the end.
The last included file will have the highest priority as long as you dont have an inline style

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 make CSS file more important than another CSS file?

There is no such magic. CSS has its own principles, such as the cascade, and you cannot switch them off.

You can place the reference to css.css after the reference to bootstrap.min.css, but this is just one factor in the process of resolving how CSS rules will be applied. Adding !important, which is generally a symptom of wrong design, is just yet another factor: an !important rule in an earlier file may override your !important rule, if its selector has higher specificity.

To override CSS rules, you need to analyze the rule you wish to override and set up a rule that “wins”, by the cascade principles.

How to prefer a css file over another?

Usually what I do in such cases of CSS conflicts is roundup the classes that are being used by the element that is causing the conflict and then extract only those classes and have them as internal styles(inside the tags) because the CSS preferences you know is in-line > internal > external and id > class > element and also !important takes precedance. for more info refer here - What is the order of precedence for CSS?

As an example -

<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</head>
<body>
<style>
.text-primary{
color: black !important;
}
</style>

<div class="container">
<h2>Contextual Colors</h2>
<p>Use the contextual classes to provide "meaning through colors":</p>

<p class="text-primary">This text is important.</p>
<p class="text-success">This text indicates success.</p>

</div>

</body>

</html>

In which order do CSS stylesheets override?

The rules for CSS rule cascading are complex -- rather than trying to paraphrase them badly, I'll simply refer you to the spec:

http://www.w3.org/TR/2011/REC-CSS2-20110607/cascade.html#cascade

In short: more specific rules override more general ones. Specificity is defined based on how many IDs, classes, and element names are involved, as well as whether the !important declaration was used. When multiple rules of the same "specificity level" exist, whichever one appears last wins.

Order of prioritization when using multiple contradictory css files

Quick Answer:

If both pieces of CSS have the same specificity (for example, they're both body{), then whichever gets called LAST will override the previous one.

BUT, if something has higher specificity (a more specific selector), it will be used regardless of the order.


Example 1:

<div class="container">
<div class="name">Dave</div>
</div>

<style>
.name { color: blue; }
.name { color: red; }
</style>

The above example will make the color red. Both selectors are the same, and therefore also have the same specificity. And because CSS reads top-to-bottom, we first tell it to be blue, but then we override that by telling it "nevermind, make it red".


Example 2:

<div class="container">
<div class="name">Dave</div>
</div>

<style>
#container .name { background-color: blue; }
.name { background-color: red; }
</style>

The above example will make the background color blue, even though blue was first because the selector is more "specific".


Exception (the use of !important):

The inclusion of !important will override both specificity and order, but in my opinion, should only be used if you're trying to mess with a third party code in which you don't have access to change it any other way.


External CSS:

Overwrite rules work the same on external CSS files. Just imagine putting them first-to-last, top-to-bottom. The selectors called in the first file(s) will get overwritten by same-specificity-selectors in any subsequent files. But specificity will still trump order within the same file or in multiple files.


How to test:

In Chrome, Firefox, and modern versions of IE (probably Safari too), you can right click on something and click "Inspect Element". This will show you the HTML as well as any applied CSS. As you scroll down the CSS (usually on the right), you'll see things that are crossed out - that means they're either incorrect CSS or have been overwritten. To test, you can modify the CSS selectors (either in your own code or right there in the developer tools box) to make them more specific and see if that makes then un-crossed out...etc. Play around w/ that tool - it's VERY helpful.

Not sure how "specific" something is?
Try some of the many online CSS specificity tools:

  • https://polypane.app/css-specificity-calculator
  • https://specificity.keegan.st/
  • https://www.codecaptain.io/tools/css-specificity-calculator

Bootstrap get priority over my CSS - with correct implementation order

In this situation, bootstrap's code is more specific (has 2 classes, while yours has only one specified).

Take a look at this MDN page for more information

You can override bootstrap by being more specific or using '!important' in your css rule (be aware that usually !important is not to be preferred, higher specificity is more flexible and easy to further override or change)

color: #C77358 !important;

Is there a way to set a stylesheet’s priority higher than !important?

You can either:

  1. Ensure your declarations are equally specific but come later in the stylesheet: