CSS3 selector (Basic selector, attribute selector, pseudo-class selector, and Selector Policy)
CSS3 basic Selector
1. wildcard selector (*)
*{
marigin: 0;
padding: 0;
}
Ii. Element selector (E)
li {background-color: grey;color: orange;}
Iii. class selector (. className)
Iv. id selector (# ID)
#first {background: lime;color: #000;}
#last {background: #000;color: lime;}
V. Descendant selector (e f)
.demo li {color: blue;}
6. Child element selector (E> F)
ul > li {background: green;color: yellow;}
VII. Adjacent sibling element selector (E + F)
li + li {background: green;color: yellow; border: 1px solid #ccc;}
8. General brother selector (E? F)
.active ~ li {background: green;color: yellow; border: 1px solid #ccc;}
9. Group selector1, selector2,..., selectorN)
.first, .last {background: green;color: yellow; border: 1px solid #ccc;}
CSS3 attribute Selector
The attribute selectors of CSS3 mainly include the following:
- E [attr]: only attribute names are used, but no attribute values are determined;
- E [attr = "value"]: Specifies the attribute name and the attribute value;
- E [attr ~ = "Value"]: Specifies the attribute name and has an attribute value. This attribute value is a word list separated by spaces. The word list contains a value word, and the "?" Not writable;
- E [attr ^ = "value"]: Specifies the attribute name and has an attribute value. The attribute value starts with value;
- E [attr $ = "value"]: Specifies the attribute name and has an attribute value. The attribute value ends with the value;
- E [attr * = "value"]: Specifies the attribute name, has a property value, and contains value;
- E [attr | = "value"]: Specifies the attribute name, and the attribute value is a value or a value starting with "value-" (for example, zh-cn );
. Demo a [href] [title] {background: yellow; color: green;} // href and title are selected
. Demo a [id = "first"] {background: blue; color: yellow; font-weight: bold;} // id = "first" selected
CSS3 pseudo-class selector1. This is the most common dynamic pseudo class.
. Demo a: link {color: gray;}/* When the link is not accessed, the foreground color is gray */
. Demo a: visited {color: yellow;}/* after the link is accessed, the foreground color is yellow */
. Demo a: hover {color: green;}/* the foreground color is green when the mouse is suspended over the link */
. Demo a: active {color: blue;}/* click the link to activate the link. The foreground color is blue */
2. Pseudo-class UI element status": Enabled", ": disabled", ": checked" pseudo classes are called pseudo classes of the UI element State. These are mainly for Form Element operations in HTML, the most common example is "type =" text ", which has two statuses: enable and disabled. The former is writeable and the latter is not available; in addition, "type =" radio "and" type = "checkbox" have two statuses: "checked" and "unchecked. Let's look at two instances. For example, if you want to distinguish the text box of "disabled" from other text boxes, you can apply it like this; IE6-8 does not support ": checked", ": enabled ",": disabled "these three selectors
3. nth Selector
- : Fist-childSelect the first child element of an element;
- : Last-childSelect the last child element of an element;
- : Nth-child ()Select one or more specific child elements of an element;
- : Nth-last-child ()Select one or more specific child elements of an element, and start from the last child element of the element;
- : Nth-of-type ()Select the specified element;
- : Nth-last-of-type ()Select the specified Element and calculate it from the last element;
- : First-of-typeSelect the first child element of the same type under a parent element;
- : Last-of-typeSelect the last child element of the same type as the parent element;
- : Only-childThe selected element is the only element of its parent element;
- : Only-of-typeSelect an element that is the only child element of the same type of its parent element;
- : EmptyThe selected element does not contain any content. IE6-8 and FF3-Browsers Do not support ": nth-child,: nth-last-child (),: nth-of-type" Selector4. Negative selector (: not)The negative selector is exactly the same as the: not selector in jq. You can use the elements in form to describe the usage of this selector. For example, you want to add borders to all input fields in form, however, if you do not want to change the submit, you can use: not for implementation.
input:not([type="submit"]) {border: 1px solid red;}
5. pseudo elementsWe have seen the pseudo elements in CSS before: first-line,: first-letter,: before,: after; then in CSS3, he made some adjustments to the pseudo elements, in the past, a ":" is added, that is, ": first-letter,: first-line,: before ,:: in addition, ": selection" and ":" and ":" are added to "after". css3 is mainly used to distinguish between pseudo classes and pseudo elements. Currently, both methods are accepted. That is to say, no matter which method is used, the function is the same, but a different writing format is used. Let's take a brief look at their role: first-line selects the first line of the element. For example, we can use this to change the style of the first line of text in each paragraph. : Before AND: after are used to insert content before or after an element,The most common thing I have ever seen is to clear the float. .clearfix:before,
.clearfix:after {
content: ".";
display: block;
height: 0;
visibility: hidden;
}
.clearfix:after {clear: both;}
.clearfix {zoom: 1;}
CSS selector OptimizationInherent efficiency:
- Id selector (# myid)
- Class selector (. myclassname)
- Tag selector (div, h1, p)
- Adjacent selector (h1 + p)
- Sub-selector (ul> li)
- Descendant selector (li)
- Wildcard selector (*)
- Attribute selector (a [rel = "external"])
- Pseudo-class selector (a: hover, li: nth-child)