Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I know why a CSS style is ignored?

I usually trace my CSS stylesheet by using the Chrome web browser.

I can see that some of my styles are being ignored by the inspection window of Chrome - the style is struck out with a black line.

I cannot see why it is being ignored. Usually, I follow all the styles using my eyes to see why it is ignored.

Can I know the structure (inheritance) information by Chrome or by other method?

like image 378
Ashram Kane Avatar asked Jul 29 '14 01:07

Ashram Kane


People also ask

Why is my CSS being overridden?

If you can see your new CSS in the Styles pane, but your new CSS is crossed out, it means that there's some other CSS that is overriding your new CSS. In CSS terminology this concept is called Specificity. Chrome DevTools can help you find the old CSS that is causing your new CSS to not be applied.

Why is my CSS style not working?

Make sure the link tag is at the right place If you put the <link> tag inside another valid header tag like <title> or <script> tag, then the CSS won't work. The external style CAN be put inside the <body> tag, although it's recommended to put it in the <head> tag to load the style before the page content.

Why is my CSS not linking to HTML?

When your HTML and CSS files are not on the same folder, you might have some challenges linking them. You can resolve this problem by: Using the correct file path to the CSS file. So if the CSS file is in a different folder from the HTML path, you need to identify the path name and add it to the link href value.


Video Answer


1 Answers

In Short, Specificity

From CSS Specificity: Things You Should Know:

  1. Specificity determines, which CSS rule is applied by the browsers.
  2. Specificity is usually the reason why your CSS-rules don't apply to some elements, although you think they should.
  3. Every selector has its place in the specificity hierarchy.
  4. If two selectors apply to the same element, the one with higher specificity wins.
  5. There are four distinct categories which define the specificity level of a given selector: inline styles, IDs, classes+attributes and elements.
  6. You can understand specificity if you love Star Wars: CSS Specificity Wars.
  7. You can understand specificity if you love poker: CSS Specificity for Poker Players.
  8. When selectors have an equal specificity value, the latest rule is the one that counts.
  9. When selectors have an unequal specificity value, the more specific rule is the one that counts.
  10. Rules with more specific selectors have a greater specificity.
  11. The last rule defined overrides any previous, conflicting rules.
  12. The embedded style sheet has a greater specificity than other rules.
  13. ID selectors have a higher specificity than attribute selectors.
  14. You should always try to use IDs to increase the specificity.
  15. A class selector beats any number of element selectors.
  16. The universal selector and inherited selectors have a specificity of 0, 0, 0, 0.
  17. You can calculate CSS specificity with CSS Specificity Calculator.

For example

If you had a p with class a, what would you predict it's color would be based on the following rules:

p.a { color: red;  }
 .a { color: blue; }

If you said blue, you'd be wrong!

As Alochi pointed out, if you're looking for more of a Tree View of the styles hit by a particular element, in the Chrome Developer Tools, go to the computed tab and expand the property you're interested in. You'll get a full list of all the other rules that apply to that element with a link to each. From what you know about specificity and cascading, it should be clearer why the "winner" was chosen.

Chrome Inspector

like image 126
KyleMit Avatar answered Sep 30 '22 18:09

KyleMit