Use HTML tag names, classes or IDs in CSS?

前端 未结 12 1695
攒了一身酷
攒了一身酷 2020-12-14 05:13

In designing the HTML and CSS for a page, when should I use

img.className

versus

.clas

相关标签:
12条回答
  • 2020-12-14 05:42

    Yes. You may want to use the same classname for two elements in the future. Be explicit and clear. This will also prevent class-rules from overlapping onto unintended elements.

    h1.title { font-size:18px; } /* My h1's are big */
     p.title { font-size:16px; } /* My p's are smaller */
    
      .title { color:#336699; }  /* All titles are blue */
    

    Use ID's only when necessary, and only once per page.

    0 讨论(0)
  • 2020-12-14 05:43

    Class selectors

    .className
    

    This is to be used when you have more than one element on the page that you would like to apply the same style to. It can be to any tag element. So in the following all will use the same style as set out by the .className.

    <a href="#" class="className"></a>
    <p class="className"></p>
    <img src="/path/to/image.png" class="className" />
    

    But you can also restrict it like so:

    img.className
    

    By placing the tag along with the style definition, you're saying that this style is only to be used when it's the class used by that particular tag, in this case, an image.

    HTML code will look like this:

    <img src="/path/to/image.png" class="className" />
    

    If you have other elements on the page using the same class style, but are not of the same tag, then the styles set out in this will not be applied and they will take on the more generic version as mentioned in the first example.

    So repeating the example above:

    <a href="#" class="className"></a>
    <p class="className"></p>
    <img src="/path/to/image.png" class="className" />
    

    Only the image will take on the style as set out by img.className whereas all the rest will take on the style rules set in .className.

    ID selectors

    #idName
    

    This is to be used when there is only one instance of a particular element that you wish to apply the style to.

    You can also force it to apply only in certain tag conditions as you have earlier with the class definitions.

    p#idName
    

    This example will only apply to the paragraph block marked with the ID:

    <p id="idName">
    

    If you were to put that id on another element, like this:

    <div id="idName"></div>
    

    Then it will not take on the style set out and be ignored.

    0 讨论(0)
  • 2020-12-14 05:45

    One thing worth noting is that some server side scripting technologies (most notably ASP.NET) don't play well with using IDs for your styling. If there is a chance your design will be used with such a technology, I recommend forgetting about #id selectors and use tag.className instead.

    The reason is that ASP.NET actually changes the ID that ends up in the HTML based on a number of criteria, if the tag is output by a server side control.

    0 讨论(0)
  • 2020-12-14 05:46

    In general you should be as specific as the item demands.

    There is no general rule, it depends on the style in question.

    A lot of people will recommend you keep to the lowest specificity with the theory that this allows the maximum cascading reuse but this is absolutely toxic in real world situations where you have multiple developers all working on slightly different versions of what a .foo might look like. Pollution from inheritance you did not want leads to massive bloat in trying to undo that locally or time-loss in refactoring.

    The best guideline I always offer is to try and think of CSS in OO terms: class selectors map to interfaces more or less, tags map to classes, and ID selectors map to instances. Consequently decide if the style you want to apply really applies to that thing, all things like it, or anything which wants it.

    I also strongly encourage you to make use of high level IDs on wrapper elements so you can write selectors in a namespace like fashion (i.e. #foo .bar, #foo .baz where #foo is unique to a page or set of page designs) which allows you both a level of specificity which reduces cross-design pollution and a level of generality which lets you make the most of cascading CSS reuse.

    Best of both worlds.

    0 讨论(0)
  • 2020-12-14 05:52

    It's good practise to use the least specific rules you can for each rule.

    How you structure your CSS will depend on the particular needs of the design.

    0 讨论(0)
  • 2020-12-14 05:55

    When to use what depends on what you want to select. img.className (type selector + class selector) selects only IMG elements that’s in the class “className” while .className (just class selector) selects any element that’s in that class and #idName (id selector) any element with the ID “idName”.

    But besides that, the selector all have a differente specificity that affects the order in which the properties of that rules overwrite the one of others.

    So if you have an IMG element with the ID “idName” that’s in the class “className”:

    <img src="…" id="idName" class="className">
    

    The properties of the rules would be applied in the following order (specificity from highest to lowest):

    1. #idName
    2. img.className
    3. .className

    But when you use a specific class only for one specific type of elements (e.g. “className” only for IMG element), you can go with only .className.

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