Does the order of css stylesheet definitions matter?

前端 未结 7 875
情歌与酒
情歌与酒 2021-02-07 04:02

I have several stylesheets. The first one is a stylesheet with some styles that I would like to use as defaults. Depending on several factors, the code being used to generate

相关标签:
7条回答
  • 2021-02-07 04:11

    There is a defined cascade in which the styles are sorted and applied. When declarations have the same importance (weight), origin and specificity then the latter declaration wins. Most answers cover importance and specificity here but not origin.

    Here are some very good slides about CSS Cascades. (Overview all resources)

    0 讨论(0)
  • 2021-02-07 04:11

    Yes, that is how stylesheets work. The last one wins.

    Just be sure the specificity of the default style sheet isn't greater than your override. And yes, avoid !important if you can. It's just silly.

    0 讨论(0)
  • 2021-02-07 04:11

    yep - the clue is on the name 'cascading' stylesheets. So an inline style will overwrite an style that is defined in the head and a style in the head will overwrite a style in a style sheet. The last style sheet load could overwrite the styles in the previous one loaded. If you use something like firebug or the inspector in chrome it will show you where each style has come from or what it has overidden.

    0 讨论(0)
  • 2021-02-07 04:19

    If you have defined style for a selector in more than one css file, the style from last loaded CSS file will be taken

    0 讨论(0)
  • 2021-02-07 04:24

    If the selectors are identical, the last loaded takes precedence, just as if you declared the same class twice in the same stylesheet.

    0 讨论(0)
  • 2021-02-07 04:25

    The simple answer is yes. Any styles re-declared further down the pages will overwrite the classes declared earlier on in the page load.

    For this to work the second declaration of the class must be in the inheritance.

    Classes like .content{} and .body .content{} may behave differently when inheriting styles.

    0 讨论(0)
提交回复
热议问题