Page-specific css rules - where to put them?

后端 未结 11 1916
粉色の甜心
粉色の甜心 2021-02-05 05:52

Often when I\'m designing a site, I have a need for a specific style to apply to a specific element on a page and I\'m absolutely certain it will only ever apply to that element

相关标签:
11条回答
  • 2021-02-05 06:30

    It's not worth it to load a page-specific CSS file for one or two specific rules. I would place it in tags in the head of the document. What I usually do is have my site-wide CSS file and then using comments, section it up based on the pages and apply specific rules there.

    0 讨论(0)
  • 2021-02-05 06:32

    Put it in the place you would look if you wanted to know where the style was defined.

    For me, that's exactly the same place as I would place styles that were used 2 times, 5 times, or 170 times - I see no reason to exclude styles from the main stylesheet(s) based on number of uses.

    0 讨论(0)
  • 2021-02-05 06:38

    Defining the style in the consuming page or inlineing your style are two sides of the same coin - in both cases you are using page bandwidth to get the style in there. I don't think one is necessarily better than the other.

    I would advocate making an #Selector for it in your site-wide main stylesheet. The pollution is minimal and if you really have that many truly unique cases, you may want to rethink they way you mark-up your sites.

    0 讨论(0)
  • 2021-02-05 06:40

    I think you should definitely expand the thought process to include some doubt for "page specific css". This should be a very very rare thing to have. I'd say go for the global style sheets anyway, but refactor your css / html in a way that pages don't have to have super-specific styling. And if in the end there's a few lines of page-specific markup in the global css, who cares. It's better to have it in a consistent place anyway.

    0 讨论(0)
  • 2021-02-05 06:40

    In that case I typically place it at the top of the page. I have a page definition framework in PHP that I use which carries local variables for each page, one of which is page-specific CSS styles.

    0 讨论(0)
  • 2021-02-05 06:41

    As you know style-sheet files are static files and cached at client. Also they can be compressed by web server. So putting them in an external file is my choice.

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