CSS selector style?

Sometimes I manage to create a CSS class for a single element. So instead, I can use an identifier and set a rule for that identifier. Which method is preferable stylistically?

+3
source share
5 answers

If a style refers to only one unique element, an ID-based selector is most appropriate.

If a style can relate to multiple elements, use a class.

Another thing to keep in mind is selectors with an identifier that have a higher priority than class-based ones. This helps if any of your unique ID'd elements inherit styles from more general, classified rules.

+1
source

, .

: ? "", . , .

-, . , - :

#aboutus_team_wrapper {}

, , , , , -, .

, !

+4

! , .

: http://www.w3.org/TR/CSS2/cascade.html (6.4.3)

ID 10 , . , 11 , CSS, , , !

" , , , ? .

ID- . ( ) , .

<style type="text/css">
    #header a { /*Selector Weight: 101*/
        font-weight: normal;
    }
    .bold { /*Selector Weight: 10*/
        font-weight: bold;
    }
</style>
<div id="header">
    <a href="#">Happily not bold.</a>
    <a href="#" class="bold">Sadly, not so bold.</a>
</div>

<a href="#" class="bold">SO bold...</a>

, :

    #header .bold { /*Selector Weight: 110*/
        font-weight: bold;
    }

, . , . , -.

+3
source

There are no hard and fast rules for this, it is something like this:

  • why do you use style
  • what are you doing.
  • where are you going

more than the number of elements involved.

+2
source

I prefer to use identifiers for the most important elements that are repeated on the page, such as the Stackoverflow logo (and the entire title) on this page.

Use the CSS class when elements are independent of the page and can be repeated among different pages or many times on the same page.

0
source

Source: https://habr.com/ru/post/1770233/


All Articles