First, class selector
Use "." For class selection (English dot) is identified, followed by the class name
Such as:
. Red {color:red;}
Class styles can be applied to multiple elements in a document, which reflects the reusability of the CSS code and helps the user to simplify page control.
Second, class selector and Tag Selector
Both class selector and Tag selector have a one-to-many feature, that is, a style that controls the display of multiple element objects.
Precautions:
- Class selectors have better adaptability and flexibility than tag selectors, because you can specify the range of element objects to which a class's style is applied
- The label selector has the advantage of being simple and easy to define, as compared to the class selector, because there is no need to define the same class attribute for each element, and before using the class selector, you need to define the class attribute in the HTML document for the element to which you want to apply the style, which is more cumbersome for the pro.
- Tag selectors are suitable for defining global display properties for elements, and class selectors, which are more suitable for defining class styles and defining the style of tag selectors, will definitely affect the same element in the page, and the style defined by the class selector will appear to be non-applied, with greater maneuverability.
<!DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd "><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /><title>Pseudo-class and pseudo-object selectors</title><Linktype= "Text/css"rel= "stylesheet"href= "Css/demo4.css"/></Head><Body><Div></Div><Divclass= "Red"></Div></Body></HTML>
Css
@charset "Utf-8";/* CSS Document */div{ width:400px; height:200px; Background-color:blue; }. red{background-color:red;}
Results:
Third, class selector and ID selector
Class selectors and ID selectors, in addition to their scope of application, have different precedence, and the ID selector has greater precedence over the class selector under the same conditions
<!DOCTYPE HTML Public "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd "><HTMLxmlns= "http://www.w3.org/1999/xhtml"><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8" /><title>Pseudo-class and pseudo-object selectors</title><Linktype= "Text/css"rel= "stylesheet"href= "Css/demo4.css"/></Head><Body><DivID= "text"class= "Red"></Div></Body></HTML>
Css
@charset "Utf-8"; /* */div{ width:400px; Height:200px; } #text { background-color:blue; } . Red {background-color:red;}
Div will appear as blue
Generally speaking:
ID selector: Used to reflect the structure and location of the document, such as #header, #footer, #left等,
Class selector: Classes attribute should reflect the style of the name, so that a class name to understand the style to define, such as. Red, define a red class,. Underline defines an underlined class, etc.
CSS class Selector (iv)