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:
.red{color:red;}
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.
Note:
- 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">
Css
@charset "utf-8";/* CSS Document */div{ width:400px; height:200px; background-color:blue; }.red{background-color:red;}
Result:
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">
CSS
@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.