What is the difference between class and ID in Css:div?

Source: Internet
Author: User
1. Class is the classes that set the label, and the class property is used to specify which style the element belongs to.

such as style sheets can be added to. content1 {color:red; background: #ff80c0} usage: class= "Content1".

ID is the identity of the set label. The id attribute is used to define the unique style of an element. As a CSS rule #content2 {Font-size:larger} is used as: id= "Content2"


Conceptually not the same (the following two words are more incisive):

class is a style, first defined, then can set to multiple structures/content for easy reuse. This means that the class name can be the same.

id is a label that distinguishes between different structures and content, like your name, If a room has 2 people with the same name, there will be confusion; id is to find the structure/content first, then define the style;

2. Priority issues for attributes

ID priority is higher than class, you can write an example to see. Part of the code is as follows ( I wrote, not written by the original author of Kazakhstan ):



3. About JS

If you want to script an object in the page (JS), then you can define an ID, otherwise can only be used to traverse the page element plus specify a specific attribute to find it, this is a relatively waste of time resources, far less than an ID to simple.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.