简体   繁体   中英

Where should css go in .html page?

I have a index.html page and a style.css file.

I have a style class called .slider and a style class called .logo . Should I put both .slider and .logo inside the style.css page or should i put .slider in index.html and .logo inside .css?

im asking this because i dont know if I should put styles only related to one page inside a global style.css or should i just put it inline in the page it applies to?

Typically you embed css that is page specific. Here is how I do my css:

Global css (seperate file(s)):

<link type="text/css" rel="stylesheet" href="css/style.css" media="all" />

NOTE: In today's web world we use a ton of plugins/themes and these plugins/themes tend to roll out newer versions so when you override these plugins/themes make sure you create a css file specific to that theme.

For instance,

Bootstrap 3.1

<link href="/css/bootstrap.3.1.css" rel="stylesheet" type="text/css">

Create this for all your bootstrap overrides so you can use the latest version when it comes out.

<link href="/css/bootstrap.overrides.css" rel="stylesheet" type="text/css">

Page specific css(embedded):

<head>
<!-- last element in head //-->
<style type="text/css">
        .pageSpecific
        {
         color: red;
        }

    </style>
</head>

When I am in development stages or the html element does not require css classification.

Simple style format (inline):

<th style="min-width: 65px;">Column Header</th>

You may have print specific css as well:

  <link href="/css/print.css" rel="stylesheet" type="text/css">

and inside this file wrap your css as such:

@media print{
<!-- print specific css here//-->
}

Keep in mind that you may want to use a css classifcation at a later date when at the time it may seem page specific. Always code for reusability!

Finally:

I like to use css minifier when I put my file(s) into production. I do the same with my javascript files using UglyJs . There is also LESS css that you can use but for now I would stick to these simple conventions or something similar.

Styles can go in one of three places.

  1. Inline on the element itself
  2. In the html page
  3. In a separate file

I would suggest either putting it in a <style> tag in the <head> of your html or putting it in a separate file, either will work just as well. Obviously having the styles in a separate file means you could reuse those styles on another page if needed.

Either way, it won't make a difference. Personally, I like to have all of my CSS in one place, but you can do whatever you want. If you do put all of your CSS in one document, use comments to separate it into groups, so everything will be easy to find.

You should NEVER have inline or on-page CSS. It should all go in the stylesheet (of which you should only have one per media-type) - why? Because stylesheets are cached, and the cache is way better to hold it than the HTML-files (which may also be cached, by all means, but with dynamic content, they often load quite more often than CSS).

Second, it's a nightmare to update and change, if not everything is in one file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM