Tell me if you need to combine like this:

.top li,article,.top,.number,.comments{margin-bottom:10px} 

And in what cases is the class used, and in which id? And then I basically only class.

PS Maybe you are advising on optimization?

  • There are a lot of online services for css optimization - soledar10
  • @ soledar10 Well, I still want to understand myself, and not just ready to use. - rodion

4 answers 4

From my own experience I advise you to use id as rarely as possible.

First, in the later stages of development you will already have a set of styles that you may want to use on the page more than once. In the case of id, you will have to either rewrite styles, or duplicate them already for classes.

Secondly, from personal experience, I can say that sometimes I do not notice that, for example, in the jquery sample, I automatically write a dot instead of the hash $ ('. Select') / $ ('# select'). And then I'm looking for a "bug."

Thirdly, the points look neater than the hashes. And if I used Id somewhere as the parent of the rest styles, then it’s not very pleasant to look at the table.

Well, as mentioned above, id is an element identifier, so it should not be repeated. This logic should be followed when you use id, but ultimately it’s not a particularly important goal to select unique elements on the page in this way.

  • However, referring to the class from jQuery (except for each ()) will give us ambiguity. I agree that you need to use ID less often, as a rule, only in this case I use it. That is, solely to identify the item. - densmith
  • But for such cases, you need to hang control classes that are not styled in CSS and serve only to manage: .js-btn-submit, .js-btn-callback and so on. - ferrari

With layout, always type-up everything in classes. At one time, a lot was discussed. It is obvious that later you will transfer this business to the programmer and where he will need to put the IDs where he needs it. Otherwise, you will not remember a kind word. And all these semantic tales about a class are a group of elements, and id is a unique identifier, this is all verbiage. Leave it to people cut off from the real development.

    On the contrary, it is better to separate. The id is the id of the item and it must be unique on the page. A class is a set of elements that are combined by a common style set.

    I advise myself to start http://htmlbook.ru

    • @densmith integration is done using a single online service to optimize css, is it better not to do so? As I understand it: id if you need to highlight some button or some other element that will later be used with js, and the class for the whole main one. - rodion
    • You understood correctly. Optimization does not hurt (though, for an amateur business), but only after the layout is ready. Otherwise, with an increase in the number of lines of code, one day it will not work out why the layout is floating. - densmith
    • Only use classes
    • Learn to use the "preprocessor", try, say it: http://compass-style.org and forget about such constructions as in the OP post
    • Use control classes for JS, which do not depend on the style, element, space on the page.
    • About nesting of classes is nonsense. On modern devices hundredths of a second. (I mean reasonably of course)
    • Do not use the selector *