Home > Article > Web Front-end > When to use class and id in css
When the css style is applied to more than one element, use class to define it; when the css style applies to only one element, use id to define it. Reason: ID cannot be repeated and can only be used once. One ID can only be used for one label element; class can be used repeatedly, and the same class can be defined on multiple label elements.
The operating environment of this tutorial: windows7 system, css3 version, Dell G3 computer.
Tutorial recommendation: css video tutorial
In addition to the selectors in CSS, you can select HTML predefined tags, such as 3499910bf9dac5ae3c52d5ede7383485, e388a4556c0f65e1904146cc1a846bee, < ;div>. You can also choose a user-defined id or class. The main difference between id and class is that id cannot be repeated and can only be used once. One id can only be used for one label. Classes can be reused, and the same class can be defined on multiple tags. In fact, the difference between id and class can be seen from the name. id is the identifier, and class is the class.
So when to use id and when to use class?
When a css style is used for more than one element, use class to define it.
When the css style is applied to only one element, use id to define it.
For example, a navigation bar. If there is only one top navigation bar in each page, you can use the id to define it.
<nav id="nav"></nav> // 或者 <div id="nav"></div>
For example, a product list has the same style. You can use class to define
<ul id="list"> <li class="list-item">1</li> <li class="list-item">2</li> <li class="list-item">3</li> </ul>
For more programming-related knowledge, please visit: Programming Teaching! !
The above is the detailed content of When to use class and id in css. For more information, please follow other related articles on the PHP Chinese website!