CSS3 Selector Learning Notes

Source: Internet
Author: User

CSS3 Selector

This article for my CSS3 study notes, the text of the content and source code are derived from:
Graphic CSS3 Core technology and case study
W3cplus
Highly Recommended!!!

To apply a style to a particular HTML element, you first need to find the element. In CSS, the performance rule that performs this process is called a CSS selector, which provides a great deal of flexibility in applying styles after acquiring the target element.

The CSS3 selector adds a new property selector, Pseudo-class selector, and filter selector to the general selector to help reduce dependency on HTML class names or ID names in development, as well as structure dependencies on HTML elements, making it easier to write code.

First, the basic selector

The basic selector is the most frequently used, most basic selector in CSS, and the basic selector allows you to determine most of the DOM element nodes in the HTML tree structure.

<! DOCTYPE html>

The above code uses the basic selector, the initial effect of the page is

1. Wildcard Selector

.demo *{    background: orange;}

2. Element Selector

ul{    background: gray;}

3.ID Selector

#first{    background: lime;    color: #000;}#last{    background: #000;    color: lime;}

4. Class Selector

.item{    background: green;    color: #fff;    font-weight: bold;}

Second, level selector

The hierarchical selector acquires elements through hierarchical relationships between DOM elements in HTML, and its main hierarchical relationships include descendants, parent-child, neighbor-sibling, and general-sibling relationships, which can be easily and quickly selected for the desired element through a class of seven relationships.

<!DOCTYPE html>

1. Descendant Selector

div div{    background: orange;}

2. Sub-selector

body > div{    background: green;}

3. Adjacent Sibling selector

.active + div{    background: lime;}

4. Universal Brotherhood Selector

The generic sibling selector is a new addition to CSS3, which selects all sibling elements behind an element.

.active ~ div{    background: red;}

Three, dynamic pseudo-class selector

Dynamic pseudo-classes do not exist in HTML, only when the user interacts with the site. The dynamic pseudo-class contains two kinds, the first one is the anchor pseudo-class that is often seen in the link, and the other is the pseudo-class of user behavior. respectively: E:link,e:visited,e:active,e:hover,e:focus.

<! DOCTYPE html>

Default state:

Suspension Status:

Click Status:

Iv. Target pseudo-class selector

Target Pseudo class selector ": Target" is used to match the target element of an identifier in the document's URI. Specifically speaking. Identifiers in URIs usually contain a # number followed by an identifier name, such as "#contact" and ": Target" to match the element with the id "contact".

Use pure CSS to achieve vertical accordion effects.

<! DOCTYPE html>

V. Structure pseudo-Class selector
<! DOCTYPE html>

Page Initial effect

1. :first-child Use of

ul > li:first-child{    background-color: green;}

2. :last-child Use of

ul > li:last-child{    background-color: blue;}


3. :nth-child Use of

ul > li:nth-child(3){    background-color: yellow;}

ul > li:nth-child(n){    background-color: yellow;}

ul > li:nth-child(2n){    background-color: yellow;}

ul > li:nth-child(2n+1){    background-color: yellow;}

ul > li:nth-child(n+5){    background-color: blue;}

ul > li:nth-child(-n+5){    background-color: blue;}

ul > li:nth-child(4n+1){    background-color: blue;}

4. :nth-last-child Use of

:nth-last-childand :nth-child similar, just :nth-last-child starting from the last child element of a parent element to select a specific element.

ul > li:nth-last-child(4){    background-color: blue;}

5. :nth-of-type Use of

:nth-of-typeand :nth-child Similar, the difference is that it only calculates the child elements of a certain type specified in the parent element. It :nth-of-type is useful and convenient to use selectors to locate child elements of a certain type in a parent element when child elements in an element are not just child elements of the same type.

6. :nth-last-of-type Use of

:nth-last-of-type:nth-of-typeis used to select a SEED element type specified in the parent element, but it starts from the last child element.

7. :first-of-type and :last-of-type the use of

:first-of-type:last-of-typesimilar to and to these two selectors :first-child , the :last-child difference is that the type of the element is specified. In other words, :first-of-type it is used to locate the first child element of a type under a parent element. It is :last-of-type used to locate the last child element of a type under a parent element.

8. :only-child Use of

:only-childIndicates that an element is the only child element of its parent element. In other words, there is only one child element in the parent element of the matching element.

<!DOCTYPE html>

Page Initial effect:

.post > p:only-child{    border-width: 2px;    background-color: #000;}

9. :only-of-type Use of

:only-of-typeThe only child element of the same type that is used to select an element that is its parent element. In other words, it :only-of-type means that an element has many child elements, and that only one child element is unique and :only-of-type can be used to select a child element of that unique type.

:emptyuse of ten.

:emptyThe element used to select any content, "no content" here refers to a little bit of content, even if it is a space.

VI. Pseudo-Elements

Pseudo-elements can be used to locate text contained in a document, but cannot be positioned in the document tree. CSS3 the pseudo-elements in a certain adjustment, on the basis of the previous addition of a colon, corresponding to the,,,, and ::first-letter ::first-line ::before ::after also added a ::selection .

1. ::first-line and ::first-letter the use of

<!DOCTYPE html>

Seven, attribute selector

The format of the property selector is the following:,,,,, E[attr] E[attr=val] E[attr|=val] E[attr~=val] E[attr*=val] E[attr^=val] , E[attr$=val] .

<! DOCTYPE html>

Page Initial effect

1.E[attr]

a[id]{    background-color: yellow;}

a[id][title]{    background-color: red;}

2.E[attr=val]

a[id=first]{    background-color: red;}

3.E[attr|=val]

a[lang|=zh]{    background-color: yellow;}

4.E[attr~=val]

a[title~=website]{    background-color: yellow;}

5.E[attr*=val]

a[class*=links]{    background-color: red;}

6.E[attr^=val]

a[href^=http]{    background-color: yellow;}

7.E[attr$=val]

a[href$=png]{    background-color:yellow;}

CSS3 Selector Learning Notes

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.