CSS doesnt overwrite style
I have 2 stylesheet files, style.css, and index.css which are loading respectively
1-style.css
2-index.css
in my style.css I have code like this
#mainNav nav > a:hover span:before {
background-color: transparent !important;
}
#mainNav nav a.darkBlue span:before {
background-color: #17729a;
}
now in my index.css
when I write
#mainNav .darkBlue span:before {
background-color: transparent;
}
It doesnt work and I should write !important at the end to make it work
but when I write selectors order in a different way like the way I used in my style.css it works without !important
like this
#mainNav nav a.darkBlue span:before {
background-color: transparent;
}
Why?
html css css3 css-selectors stylesheet
add a comment |
I have 2 stylesheet files, style.css, and index.css which are loading respectively
1-style.css
2-index.css
in my style.css I have code like this
#mainNav nav > a:hover span:before {
background-color: transparent !important;
}
#mainNav nav a.darkBlue span:before {
background-color: #17729a;
}
now in my index.css
when I write
#mainNav .darkBlue span:before {
background-color: transparent;
}
It doesnt work and I should write !important at the end to make it work
but when I write selectors order in a different way like the way I used in my style.css it works without !important
like this
#mainNav nav a.darkBlue span:before {
background-color: transparent;
}
Why?
html css css3 css-selectors stylesheet
1
Because you use !important in your first css tag
– Bo Vandersteene
Nov 21 '18 at 14:03
no, thats not the reason, the important thing is about :hover
– Cameron A
Nov 21 '18 at 14:04
#mainNav nav a.darkBlue span:before {
is more specific than#mainNav .darkBlue span:before {
and will take precedence.
– Cobra_Fast
Nov 21 '18 at 14:07
1
Possible duplicate of how to overwrite css style
– Fox
Nov 21 '18 at 14:10
add a comment |
I have 2 stylesheet files, style.css, and index.css which are loading respectively
1-style.css
2-index.css
in my style.css I have code like this
#mainNav nav > a:hover span:before {
background-color: transparent !important;
}
#mainNav nav a.darkBlue span:before {
background-color: #17729a;
}
now in my index.css
when I write
#mainNav .darkBlue span:before {
background-color: transparent;
}
It doesnt work and I should write !important at the end to make it work
but when I write selectors order in a different way like the way I used in my style.css it works without !important
like this
#mainNav nav a.darkBlue span:before {
background-color: transparent;
}
Why?
html css css3 css-selectors stylesheet
I have 2 stylesheet files, style.css, and index.css which are loading respectively
1-style.css
2-index.css
in my style.css I have code like this
#mainNav nav > a:hover span:before {
background-color: transparent !important;
}
#mainNav nav a.darkBlue span:before {
background-color: #17729a;
}
now in my index.css
when I write
#mainNav .darkBlue span:before {
background-color: transparent;
}
It doesnt work and I should write !important at the end to make it work
but when I write selectors order in a different way like the way I used in my style.css it works without !important
like this
#mainNav nav a.darkBlue span:before {
background-color: transparent;
}
Why?
html css css3 css-selectors stylesheet
html css css3 css-selectors stylesheet
edited Nov 21 '18 at 14:07
Cameron A
asked Nov 21 '18 at 14:02
Cameron ACameron A
1,04321322
1,04321322
1
Because you use !important in your first css tag
– Bo Vandersteene
Nov 21 '18 at 14:03
no, thats not the reason, the important thing is about :hover
– Cameron A
Nov 21 '18 at 14:04
#mainNav nav a.darkBlue span:before {
is more specific than#mainNav .darkBlue span:before {
and will take precedence.
– Cobra_Fast
Nov 21 '18 at 14:07
1
Possible duplicate of how to overwrite css style
– Fox
Nov 21 '18 at 14:10
add a comment |
1
Because you use !important in your first css tag
– Bo Vandersteene
Nov 21 '18 at 14:03
no, thats not the reason, the important thing is about :hover
– Cameron A
Nov 21 '18 at 14:04
#mainNav nav a.darkBlue span:before {
is more specific than#mainNav .darkBlue span:before {
and will take precedence.
– Cobra_Fast
Nov 21 '18 at 14:07
1
Possible duplicate of how to overwrite css style
– Fox
Nov 21 '18 at 14:10
1
1
Because you use !important in your first css tag
– Bo Vandersteene
Nov 21 '18 at 14:03
Because you use !important in your first css tag
– Bo Vandersteene
Nov 21 '18 at 14:03
no, thats not the reason, the important thing is about :hover
– Cameron A
Nov 21 '18 at 14:04
no, thats not the reason, the important thing is about :hover
– Cameron A
Nov 21 '18 at 14:04
#mainNav nav a.darkBlue span:before {
is more specific than #mainNav .darkBlue span:before {
and will take precedence.– Cobra_Fast
Nov 21 '18 at 14:07
#mainNav nav a.darkBlue span:before {
is more specific than #mainNav .darkBlue span:before {
and will take precedence.– Cobra_Fast
Nov 21 '18 at 14:07
1
1
Possible duplicate of how to overwrite css style
– Fox
Nov 21 '18 at 14:10
Possible duplicate of how to overwrite css style
– Fox
Nov 21 '18 at 14:10
add a comment |
3 Answers
3
active
oldest
votes
Your declarations are being applied based on how specific they are.
Per MDN - Specificity:
Specificity is the means by which browsers decide which CSS property values are the most relevant to an element and, therefore, will be applied.
Specificity is a weight that is applied to a given CSS declaration, determined by the number of each selector type in the matching selector.
The link above also goes into the factors that determine specificity:
The following list of selector types increases by specificity:
Type selectors (e.g.,
h1
) and pseudo-elements (e.g.,::before
).
Class selectors (e.g.,
.example
), attributes selectors (e.g.,[type="radio"]
) and pseudo-classes (e.g.,:hover
).
ID selectors (e.g.,
#example
).
Universal selector (
*
), combinators (+
,>
,~
,' '
) and negation pseudo-class (:not()
) have no effect on specificity. (The selectors declared inside:not()
do, however.)
CSS chooses which rules to apply based on a few conditions. Given rules applying to the same element, they are regarded in the following order:
1. !important
span { color: red; } /* base */
#mySpan { color: blue; } /* specific */
span { color: green !important; } /* important */
<span id="mySpan" style="color: purple;">Example</span>
2. Inline styles
span { color: red; } /* base */
#mySpan { color: blue; } /* specific */
<span id="mySpan" style="color: purple;">Example</span>
3. Specificity
span { color: red; } /* base */
#mySpan { color: blue; } /* specific */
<span id="mySpan">Example</span>
4. Last declared
span { color: red; } /* base */
span { color: yellow; } /* last applied */
<span>Example</span>
It's generally best to avoid using !important
wherever possible. If you throw them around carelessly, there may come a time when you actually need to override one, but you've already used your highest order of precedence.
add a comment |
CSS has a hierarchy. If you you wanna overwrite some styles you have to use the same selectors or some more specific.
Example:
a.selector { color: blue }
.selector { color: red }
The color will not be changed.
But
.selector { color: blue }
a.selector { color: red }
will change the color to red, because the combination of TAG and class selector you are more specific.
2
And in addition, here's the MDN on CSS Specificity.
– Tyler Roper
Nov 21 '18 at 14:08
add a comment |
You have something called CSS specificity:
https://www.w3schools.com/css/css_specificity.asp
A really great read on what comes first and the order of specificity: check https://www.smashingmagazine.com/2007/07/css-specificity-things-you-should-know/
Be aware, it's a loooong article. Most people don't even have the slightest idea of how far this goes.
Simply put: If two CSS selectors apply to the same element, the one
with higher specificity wins.
That's why I follow the BEM methodology, this prevents these kinds of hassles.
thanks for the link
– Cameron A
Nov 21 '18 at 14:32
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413804%2fcss-doesnt-overwrite-style%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your declarations are being applied based on how specific they are.
Per MDN - Specificity:
Specificity is the means by which browsers decide which CSS property values are the most relevant to an element and, therefore, will be applied.
Specificity is a weight that is applied to a given CSS declaration, determined by the number of each selector type in the matching selector.
The link above also goes into the factors that determine specificity:
The following list of selector types increases by specificity:
Type selectors (e.g.,
h1
) and pseudo-elements (e.g.,::before
).
Class selectors (e.g.,
.example
), attributes selectors (e.g.,[type="radio"]
) and pseudo-classes (e.g.,:hover
).
ID selectors (e.g.,
#example
).
Universal selector (
*
), combinators (+
,>
,~
,' '
) and negation pseudo-class (:not()
) have no effect on specificity. (The selectors declared inside:not()
do, however.)
CSS chooses which rules to apply based on a few conditions. Given rules applying to the same element, they are regarded in the following order:
1. !important
span { color: red; } /* base */
#mySpan { color: blue; } /* specific */
span { color: green !important; } /* important */
<span id="mySpan" style="color: purple;">Example</span>
2. Inline styles
span { color: red; } /* base */
#mySpan { color: blue; } /* specific */
<span id="mySpan" style="color: purple;">Example</span>
3. Specificity
span { color: red; } /* base */
#mySpan { color: blue; } /* specific */
<span id="mySpan">Example</span>
4. Last declared
span { color: red; } /* base */
span { color: yellow; } /* last applied */
<span>Example</span>
It's generally best to avoid using !important
wherever possible. If you throw them around carelessly, there may come a time when you actually need to override one, but you've already used your highest order of precedence.
add a comment |
Your declarations are being applied based on how specific they are.
Per MDN - Specificity:
Specificity is the means by which browsers decide which CSS property values are the most relevant to an element and, therefore, will be applied.
Specificity is a weight that is applied to a given CSS declaration, determined by the number of each selector type in the matching selector.
The link above also goes into the factors that determine specificity:
The following list of selector types increases by specificity:
Type selectors (e.g.,
h1
) and pseudo-elements (e.g.,::before
).
Class selectors (e.g.,
.example
), attributes selectors (e.g.,[type="radio"]
) and pseudo-classes (e.g.,:hover
).
ID selectors (e.g.,
#example
).
Universal selector (
*
), combinators (+
,>
,~
,' '
) and negation pseudo-class (:not()
) have no effect on specificity. (The selectors declared inside:not()
do, however.)
CSS chooses which rules to apply based on a few conditions. Given rules applying to the same element, they are regarded in the following order:
1. !important
span { color: red; } /* base */
#mySpan { color: blue; } /* specific */
span { color: green !important; } /* important */
<span id="mySpan" style="color: purple;">Example</span>
2. Inline styles
span { color: red; } /* base */
#mySpan { color: blue; } /* specific */
<span id="mySpan" style="color: purple;">Example</span>
3. Specificity
span { color: red; } /* base */
#mySpan { color: blue; } /* specific */
<span id="mySpan">Example</span>
4. Last declared
span { color: red; } /* base */
span { color: yellow; } /* last applied */
<span>Example</span>
It's generally best to avoid using !important
wherever possible. If you throw them around carelessly, there may come a time when you actually need to override one, but you've already used your highest order of precedence.
add a comment |
Your declarations are being applied based on how specific they are.
Per MDN - Specificity:
Specificity is the means by which browsers decide which CSS property values are the most relevant to an element and, therefore, will be applied.
Specificity is a weight that is applied to a given CSS declaration, determined by the number of each selector type in the matching selector.
The link above also goes into the factors that determine specificity:
The following list of selector types increases by specificity:
Type selectors (e.g.,
h1
) and pseudo-elements (e.g.,::before
).
Class selectors (e.g.,
.example
), attributes selectors (e.g.,[type="radio"]
) and pseudo-classes (e.g.,:hover
).
ID selectors (e.g.,
#example
).
Universal selector (
*
), combinators (+
,>
,~
,' '
) and negation pseudo-class (:not()
) have no effect on specificity. (The selectors declared inside:not()
do, however.)
CSS chooses which rules to apply based on a few conditions. Given rules applying to the same element, they are regarded in the following order:
1. !important
span { color: red; } /* base */
#mySpan { color: blue; } /* specific */
span { color: green !important; } /* important */
<span id="mySpan" style="color: purple;">Example</span>
2. Inline styles
span { color: red; } /* base */
#mySpan { color: blue; } /* specific */
<span id="mySpan" style="color: purple;">Example</span>
3. Specificity
span { color: red; } /* base */
#mySpan { color: blue; } /* specific */
<span id="mySpan">Example</span>
4. Last declared
span { color: red; } /* base */
span { color: yellow; } /* last applied */
<span>Example</span>
It's generally best to avoid using !important
wherever possible. If you throw them around carelessly, there may come a time when you actually need to override one, but you've already used your highest order of precedence.
Your declarations are being applied based on how specific they are.
Per MDN - Specificity:
Specificity is the means by which browsers decide which CSS property values are the most relevant to an element and, therefore, will be applied.
Specificity is a weight that is applied to a given CSS declaration, determined by the number of each selector type in the matching selector.
The link above also goes into the factors that determine specificity:
The following list of selector types increases by specificity:
Type selectors (e.g.,
h1
) and pseudo-elements (e.g.,::before
).
Class selectors (e.g.,
.example
), attributes selectors (e.g.,[type="radio"]
) and pseudo-classes (e.g.,:hover
).
ID selectors (e.g.,
#example
).
Universal selector (
*
), combinators (+
,>
,~
,' '
) and negation pseudo-class (:not()
) have no effect on specificity. (The selectors declared inside:not()
do, however.)
CSS chooses which rules to apply based on a few conditions. Given rules applying to the same element, they are regarded in the following order:
1. !important
span { color: red; } /* base */
#mySpan { color: blue; } /* specific */
span { color: green !important; } /* important */
<span id="mySpan" style="color: purple;">Example</span>
2. Inline styles
span { color: red; } /* base */
#mySpan { color: blue; } /* specific */
<span id="mySpan" style="color: purple;">Example</span>
3. Specificity
span { color: red; } /* base */
#mySpan { color: blue; } /* specific */
<span id="mySpan">Example</span>
4. Last declared
span { color: red; } /* base */
span { color: yellow; } /* last applied */
<span>Example</span>
It's generally best to avoid using !important
wherever possible. If you throw them around carelessly, there may come a time when you actually need to override one, but you've already used your highest order of precedence.
span { color: red; } /* base */
#mySpan { color: blue; } /* specific */
span { color: green !important; } /* important */
<span id="mySpan" style="color: purple;">Example</span>
span { color: red; } /* base */
#mySpan { color: blue; } /* specific */
span { color: green !important; } /* important */
<span id="mySpan" style="color: purple;">Example</span>
span { color: red; } /* base */
#mySpan { color: blue; } /* specific */
<span id="mySpan" style="color: purple;">Example</span>
span { color: red; } /* base */
#mySpan { color: blue; } /* specific */
<span id="mySpan" style="color: purple;">Example</span>
span { color: red; } /* base */
#mySpan { color: blue; } /* specific */
<span id="mySpan">Example</span>
span { color: red; } /* base */
#mySpan { color: blue; } /* specific */
<span id="mySpan">Example</span>
span { color: red; } /* base */
span { color: yellow; } /* last applied */
<span>Example</span>
span { color: red; } /* base */
span { color: yellow; } /* last applied */
<span>Example</span>
edited Nov 23 '18 at 20:57
answered Nov 21 '18 at 14:27
Tyler RoperTyler Roper
12.9k11741
12.9k11741
add a comment |
add a comment |
CSS has a hierarchy. If you you wanna overwrite some styles you have to use the same selectors or some more specific.
Example:
a.selector { color: blue }
.selector { color: red }
The color will not be changed.
But
.selector { color: blue }
a.selector { color: red }
will change the color to red, because the combination of TAG and class selector you are more specific.
2
And in addition, here's the MDN on CSS Specificity.
– Tyler Roper
Nov 21 '18 at 14:08
add a comment |
CSS has a hierarchy. If you you wanna overwrite some styles you have to use the same selectors or some more specific.
Example:
a.selector { color: blue }
.selector { color: red }
The color will not be changed.
But
.selector { color: blue }
a.selector { color: red }
will change the color to red, because the combination of TAG and class selector you are more specific.
2
And in addition, here's the MDN on CSS Specificity.
– Tyler Roper
Nov 21 '18 at 14:08
add a comment |
CSS has a hierarchy. If you you wanna overwrite some styles you have to use the same selectors or some more specific.
Example:
a.selector { color: blue }
.selector { color: red }
The color will not be changed.
But
.selector { color: blue }
a.selector { color: red }
will change the color to red, because the combination of TAG and class selector you are more specific.
CSS has a hierarchy. If you you wanna overwrite some styles you have to use the same selectors or some more specific.
Example:
a.selector { color: blue }
.selector { color: red }
The color will not be changed.
But
.selector { color: blue }
a.selector { color: red }
will change the color to red, because the combination of TAG and class selector you are more specific.
edited Nov 21 '18 at 14:08
Cobra_Fast
11.2k73985
11.2k73985
answered Nov 21 '18 at 14:06
bokelboybokelboy
213
213
2
And in addition, here's the MDN on CSS Specificity.
– Tyler Roper
Nov 21 '18 at 14:08
add a comment |
2
And in addition, here's the MDN on CSS Specificity.
– Tyler Roper
Nov 21 '18 at 14:08
2
2
And in addition, here's the MDN on CSS Specificity.
– Tyler Roper
Nov 21 '18 at 14:08
And in addition, here's the MDN on CSS Specificity.
– Tyler Roper
Nov 21 '18 at 14:08
add a comment |
You have something called CSS specificity:
https://www.w3schools.com/css/css_specificity.asp
A really great read on what comes first and the order of specificity: check https://www.smashingmagazine.com/2007/07/css-specificity-things-you-should-know/
Be aware, it's a loooong article. Most people don't even have the slightest idea of how far this goes.
Simply put: If two CSS selectors apply to the same element, the one
with higher specificity wins.
That's why I follow the BEM methodology, this prevents these kinds of hassles.
thanks for the link
– Cameron A
Nov 21 '18 at 14:32
add a comment |
You have something called CSS specificity:
https://www.w3schools.com/css/css_specificity.asp
A really great read on what comes first and the order of specificity: check https://www.smashingmagazine.com/2007/07/css-specificity-things-you-should-know/
Be aware, it's a loooong article. Most people don't even have the slightest idea of how far this goes.
Simply put: If two CSS selectors apply to the same element, the one
with higher specificity wins.
That's why I follow the BEM methodology, this prevents these kinds of hassles.
thanks for the link
– Cameron A
Nov 21 '18 at 14:32
add a comment |
You have something called CSS specificity:
https://www.w3schools.com/css/css_specificity.asp
A really great read on what comes first and the order of specificity: check https://www.smashingmagazine.com/2007/07/css-specificity-things-you-should-know/
Be aware, it's a loooong article. Most people don't even have the slightest idea of how far this goes.
Simply put: If two CSS selectors apply to the same element, the one
with higher specificity wins.
That's why I follow the BEM methodology, this prevents these kinds of hassles.
You have something called CSS specificity:
https://www.w3schools.com/css/css_specificity.asp
A really great read on what comes first and the order of specificity: check https://www.smashingmagazine.com/2007/07/css-specificity-things-you-should-know/
Be aware, it's a loooong article. Most people don't even have the slightest idea of how far this goes.
Simply put: If two CSS selectors apply to the same element, the one
with higher specificity wins.
That's why I follow the BEM methodology, this prevents these kinds of hassles.
answered Nov 21 '18 at 14:14
GoowikGoowik
218319
218319
thanks for the link
– Cameron A
Nov 21 '18 at 14:32
add a comment |
thanks for the link
– Cameron A
Nov 21 '18 at 14:32
thanks for the link
– Cameron A
Nov 21 '18 at 14:32
thanks for the link
– Cameron A
Nov 21 '18 at 14:32
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53413804%2fcss-doesnt-overwrite-style%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Because you use !important in your first css tag
– Bo Vandersteene
Nov 21 '18 at 14:03
no, thats not the reason, the important thing is about :hover
– Cameron A
Nov 21 '18 at 14:04
#mainNav nav a.darkBlue span:before {
is more specific than#mainNav .darkBlue span:before {
and will take precedence.– Cobra_Fast
Nov 21 '18 at 14:07
1
Possible duplicate of how to overwrite css style
– Fox
Nov 21 '18 at 14:10