CSS class selector (4) and css class selector

Source: Internet
Author: User

CSS class selector (4) and css class selector
I. class selector

Class selection uses "." (English dot) for identification, followed by the class name

For example:


Class styles can be applied to multiple elements in the document, which reflects the reusability of CSS code and helps users simplify page control.

Ii. class selector and label Selector

Both the class selector and the label selector have the one-to-many feature, that is, a style can control the display effect of multiple element objects.


  • Compared with the label selector, the class selector has better adaptability and flexibility, because it can specify the element object range applied by the class style.
  • Compared with the class selector, the label selection tool has the advantages of simple operation and convenient definition, because you do not need to define the same class attribute for each element. before using the class selector, you need to define the class attribute for the element of the class style to be applied in the HTML document. This will make it more troublesome to display the duplicate.
  • The label selector is suitable for defining global display attributes for elements, while the class selector is more suitable for defining class styles. After defining the style of the label selector, it will definitely affect the same element on the page, the styles defined by the class selector are not applied and have greater mobility.
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 


@charset "utf-8";/* CSS Document */div{    width:400px;    height:200px;    background-color:blue;    }.red{background-color:red;}


Iii. class selector and ID Selector

Apart from the application range, the class selector and ID selector have different priorities. Under the same conditions, the ID selector has greater priority than the class selector.

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 


@charset "utf-8";/* CSS Document */div{    width:400px;    height:200px;    }#text{    background-color:blue;    }.red{background-color:red;}

Div will be displayed in blue

Generally speaking:

ID selector: the structure and position of the application that reflects the document, such as # header, # footer, # left, etc,

Class selector: the class attribute should reflect the descriptive style of the name, so that you can understand the style to be defined at first glance, such. red: defines a red class ,. underline defines an underline class, etc.


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.