4

When I define a variable in various CSS files, and include these files in my HTML file, then it overrides the previous variable. Why is it happening?

Let's say that I have

test1.css

:root {
    --size-of-font: 5rem;
 }
.logo{
    font-size: var(--size-of-font);
}

test2.css

:root {
--size-of-font: 1.2rem;
}
.outer {
    font-size: var(--size-of-font);
    cursor: pointer;
    text-align: center;
}

test.html

<link rel="stylesheet" href="test1.css">
  <link rel="stylesheet" href="test2.css">

<div class="outer">
    <h1 class="logo">Lorem Ipsum</h1>
     <p>Neque porro quisquam est qui dolorem 
          ipsum quia..."<br>
          "There is no one who loves pain 
          itself..."
     </p>
</div>
17
  • 5
    In CSS the latest style rule applied to an element is used, unless the original rule has a greater specificity. So if you apply two style sheets and they both target the same element, the last one loaded will be the one that is applied. Commented Nov 1, 2018 at 8:40
  • Do you not just want the outer-size-font variable in your .outer { }? because then you won't have thie font-size issue if that is the case. Commented Nov 1, 2018 at 8:42
  • just to go off of gavgrif's comment: :root is more generic then .outer, thus .outer stylings get applied :) Commented Nov 1, 2018 at 8:46
  • @ThisGuyHasTwoThumbs: What does "generic" mean here? Commented Nov 1, 2018 at 8:52
  • @BoltClock :root can apply to anything, kinda like.. targetting div with CSS. it's a more generic specification, as opposed to classes and IDs. Classes and IDs are less generic and more specific Commented Nov 1, 2018 at 8:53

5 Answers 5

6

CSS = Cascading Style Sheets... this means the sequence of definition matters, the most recent and more specific takes precedence. If you switched test1 and test2 over in your html you'd get a different result because the variable is defined as the more recent value.

I recommend you use a different variable name for your different .css files if you require them to not share this value.

Sign up to request clarification or add additional context in comments.

1 Comment

not always the most recent - later rules may still not apply with case use of !important or if the previous CSS rules have a higher specificity value
2

When you include both stylesheets, all of their rules are combined into one single stylesheet. This means that you introduce a conflict in two :root CSS rules with the same custom property declaration:

:root {
  --size-of-font: 5rem;
}

:root {
  --size-of-font: 1.2rem;
}

Cascade resolution means that the specified value of the --size-of-font custom property is 1.2rem, not 5rem. Simply, the second declaration overrides the first as both rules have identical selectors.

This value of 1.2rem is then applied to both uses of var(--size-of-font), in .logo and .outer, again because two stylesheets combine to form one. .logo does not only see the --size-of-font in its own stylesheet; it sees whatever is resolved by the cascade, taking all declarations into account.

Comments

1

Here we need to consider two things: How custom properties works and how they are evaluated using var().

  1. The first part is trivial because custom property behave the same as any other property. From the specification we can read:

Custom properties are ordinary properties, so they can be declared on any element, are resolved with the normal inheritance and cascade rules, can be made conditional with @media and other conditional rules, can be used in HTML’s style attribute, can be read or set using the CSSOM, etc.

Considering this, the last custom property defined in your case will override the first one:

:root {
    --size-of-font: 5rem;
 }
.logo{
    font-size: var(--size-of-font);
}

:root {
  --size-of-font: 1.2rem; /* This one will win !*/
}
.outer {
    font-size: var(--size-of-font);
    cursor: pointer;
    text-align: center;
}
  1. When using var() we need to also consider some rules like defined in the same specification:

To substitute a var() in a property’s value:

  1. If the custom property named by the first argument to the var() function is animation-tainted, and the var() function is being used in the animation property or one of its longhands, treat the custom property as having its initial value for the rest of this algorithm.
  2. If the value of the custom property named by the first argument to the var() function is anything but the initial value, replace the var() function by the value of the corresponding custom property. Otherwise,
  3. if the var() function has a fallback value as its second argument, replace the var() function by the fallback value. If there are any var() references in the fallback, substitute them as well.
  4. Otherwise, the property containing the var() function is invalid at computed-value time

In our situation, we will consider the (2) because .logo will inherit the custom property defined inside :root with its value 1.2rem (not an initial value).

In other words, the evaluation of a custom property doesn't consider the order of their appearance in the CSS file. It considers the value of the custom property that is resolved as an ordinary property.


Here another useful question where you can get more details and more examples: CSS scoped custom property ignored when used to calc var in outer scope

1 Comment

Note that custom properties don't work the same as other properties in all respects. You cannot use var(..) on ordinary properties; e.g. {color: #444; background: var(color);} won't work.
0

Yes it will overwrite. If I have two classes having the same name like:

test1.css contains

.text-color{
    color:red;
}

test2.css contains

.text-color{
    color:blue
}

and apply them in sequence

test1.css
test2.css

it will apply the CSS which is in test2.css. It takes the latest CSS if the class names are the same.

Comments

0

There are 3 ways to CSS for websites, within a CSS file, <style> tag, or inside an HTML tag. CSS files will always have precedence over any other CSS format. <style> tags only have precedence over CSS tags in HTML, which are at the bottom.

Here's a visual

  1. CSS Files (Comes first)

  2. <style> (Cannot override CSS files but can override HTML inline)

  3. HTML inline styles (least important)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.