css3 clever use of structural pseudo-class selectors

WBOY
Release: 2016-09-28 08:38:26
Original
1550 people have browsed it

I recently saw a usage of structural pseudo-class selector on a foreign website. I thought it was very practical, so I tried it myself and recorded it:

This is the most basic style:

 1 
Copy after login

Contents in body:

 1 <body>  2 <ul>  3 <li>第01个li>  4 <li>第02个li>  5 <li>第03个li>  6 <li>第04个li>  7 <li>第05个li>  8 <li>第06个li>  9 <li>第07个li> 10 <li>第08个li> 11 <li>第09个li> 12 <li>第10个li> 13 <li>第11个li> 14 ul> 15 body>
Copy after login

First is the usage of the most basic structural pseudo-class selector:

1  li:nth-child(8){ 2  background-color: #298EB2; 4 }
Copy after login

The results are displayed as:

Use:nth-child(n+6) which is equivalent to:nth-child(6) and above li tag elements:

1  li:nth-child(n+6){ 2  background-color: #298EB2; 4 }
Copy after login

The results are displayed as:

Similarly, use:nth-child(-n+6) which is equivalent to:nth-child(6) and the following li tag elements:

1  li:nth-child(-n+6){ 2  background-color: #298EB2; 4 }
Copy after login

The results are displayed as:

Based on the above principles we can come up with some advanced ones:

For example, you can use nth-child(n+4):nth-child(-n+8) to obtain the li tag elements of :nth-child(4) and above and :nth-child(8) and below:

1  li:nth-child(n+4):nth-child(-n+8){ 2  background-color: #298EB2; 3 }
Copy after login

The results are displayed as:

You can also use:nth-child(n+2):nth-child(odd):nth-child(-n+8) to get:nth-child(n+2) to:nth-child(-n+8 ) between singular li tag elements:

1  li:nth-child(n+2):nth-child(odd):nth-child(-n+8){ 2  background-color: #298EB2; 3 }
Copy after login

The results are displayed as:

Finally, we can also use:nth-child(3n+1) to get even li tag elements with numbers 1, 4, 7, and 10:

1  li:nth-child(3n+1):nth-child(even){ 2  background-color: #298EB2; 3 }
Copy after login

The results are displayed as:

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!