[CSS Basics] difference between class and ID in CSS

Source: Internet
Author: User

Http://www.8go8.com/blog/post/20060612013036.html
)

These two differences are confusing, especially the class and ID. I believe many people have not carefully understood the differences. Some of my understandings are as follows:

1. Differences between class and ID

A class is used to define one or more elements according to user-defined standards. For example, a script: A class can define the story line of each character in the script. You can use this class through CSS and JavaScript. Therefore, you can use class = "Frodo", class = "gandalf", class = "Aragorn" on a page to differentiate different story lines. Another important thing is that you can use any number of classes in a document.

ID is usually used to define a tag that appears only once on the page. In structured layout of page layout (for example, a page is usually composed of a header, a header, a content area, and a footer), the ID is generally ideal, because an ID can only be used once in a document. These elements are rarely larger than once on the same page.

In one sentence, the class can be used repeatedly, and the ID can only be used once on a page. It is possible that the same ID will not be used repeatedly in many browsers, but it is definitely incorrect in terms of standards, and it may lead to practical problems in some browsers. (I tried it in Dreamweaver just now. Indeed, there is no error)

In practical application, the class may be useful for text layout, and the ID is useful for macro layout and design of various elements.

2. Differences between margin and padding

Both of them replace the most important function of the table-> A good way to split blocks, the difference is that margin is the distance between different tags, and padding is used to separate different content in the same element, this is the most visible in the table. Especially when the background style is loaded for a region, the background color of the content under the padding changes, while the margin does not.

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.