


What pseudo-class selectors are there in CSS3, and how to use the pseudo-class selector nth-child()
The new attributes in CSS3 bring us a lot of convenience, such as the pseudo-class selector nth-child(), but it is not supported by IE6-8 and FF3.0-browsers, nth-of in CSS3 -type(n) (such as nth-of-type(1)) is a special class selector that can set more personalized titles and paragraphs. However, currently nth-of-type(n) only supports Firefox 3, opera, Some browsers such as safari and chrome.
:nth-child() selects one or more specific child elements of an element; you can select this way:
:nth- child(length);/*The parameter is a specific number and length is an integer*/
:nth-child(n);/*The parameter is n, n is calculated from 0*/
: nth-child(n*length)/*Select multiples of n, n starts from 0*/
:nth-child(n length);/*Select the element after length that is greater than length*/
:nth-child(-n length)/*Select the element before length that is less than length*/
:nth-child(n*length 1);/*Indicates selecting every other element*/
Example:
li:nth-child(3){background:orange;}/*Set the background of the third li to orange*/
li:nth- child(3n){background:orange;}/*Set the background of the 3rd, 6th, 9th,..., all multiples of li to orange*/
li:nth-child(n 3){background:orange;}/*Select the li background behind the third element to be orange*/
li:nth-child(-n 3){background:orange;}/*Select Set the background to orange from the li in front of the third element*/
li:nth-child(3n 1){background:orange;}/*This method is to achieve the effect of selecting one from several* /
:fist-child selects the first child element of an element
Example:
li:first-child {background: green ;}/*Set the background of the first li to green*/
:last-child selects the last child element of an element
Example:
li:last-child {background: green;}/*Set the background of the last li to green*/
:nth-last-child() select an element One or more specific child elements, starting from the last child element of this element
:nth-last-child() selector is very similar to the previous:nth-child() , but there is an extra last here, so its function is different from the previous ":nth-child". It only needs to count from the last element to select a specific element.
Example:
li:nth-last-child(4) {background: red;}/*Set the background of the fourth last li to red*/
:nth-of-type() selects the specified element
:nth-of-type is similar to :nth-child, except that it only counts the one specified in the selector Element, in fact, our previous examples all specified specific elements. This selector is mainly useful for locating elements that contain many different types of elements. For example, there are many p elements, li elements, img elements, etc. under our div.demo, but I only need to select the p element and let every other p element have a different style. Then we can simply write:
p:nth-of-type(even) {background-color: lime;}
In addition to setting n to odd (even number) or even (odd number), you can also set n is set to an expression, for example, nth-of-type(3n 2)
:nth-last-of-type() selects the specified element and starts counting from the last element
Needless to say, everyone can imagine this selector. It is used the same as the previous :nth-last-child, except that it refers to the type of element.
Also in IE6-8 and FF3.0 - browsers do not support this selector
: first-of-type selects the first similar child under a superior element Element;
:last-of-type selects the last similar child element of a superior element;
:nth-of-type,:nth- last-of-type;:first-of-type and :last-of-type are not very meaningful in practice. Selectors such as :nth-child we mentioned earlier can achieve this function, but if you are interested You can still find out. I personally think the practical value is not very great. Such statements are for reference only.
:only-child means that an element is the only child element of its parent element
<ul> <li>1</li> <li>2</li> </ul> <ul> <li>3</li> </ul>
If I need to have only one p element in ul, Change the style of this li, then we can now use: only-child, such as:
ul li {padding-left:10px;}
ul li:only-child {padding- left:15px}
:only-of-type selects an element that is the only child element of the same type as its superior element
means that an element has There are many sub-elements, and only one of them is unique, then we can use this selection method to select the only sub-element, for example
<section> <h2>伪类选择器的用法</h2> <p>CSS3 伪类选择器only-of-type的用法</p> <p>CSS3 伪类选择器only-of-type的用法</p> </section>
If we want to only select h2 above Element, we can write like this,
h2:only-of-type{color:red;}
:empty There is no content in the selected element
:empty is used to select elements without any content. No content here means no content at all, even a space. For example, you have three paragraphs, one of which has nothing and is completely empty. Yes, if you want this p not to be displayed, then you can write it like this:
p:empty {display: none;}
The above is the detailed content of What pseudo-class selectors are there in CSS3, and how to use the pseudo-class selector nth-child(). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to achieve wave effect with pure CSS3? This article will introduce to you how to use SVG and CSS animation to create wave effects. I hope it will be helpful to you!

This article will show you how to use CSS to easily realize various weird-shaped buttons that appear frequently. I hope it will be helpful to you!

Two methods: 1. Using the display attribute, just add the "display:none;" style to the element. 2. Use the position and top attributes to set the absolute positioning of the element to hide the element. Just add the "position:absolute;top:-9999px;" style to the element.

In CSS, you can use the border-image attribute to achieve a lace border. The border-image attribute can use images to create borders, that is, add a background image to the border. You only need to specify the background image as a lace style; the syntax "border-image: url (image path) offsets the image border width inward. Whether outset is repeated;".

:hover in CSS is a pseudo-class selector used to apply specific styles when the user hovers over a specific element. When the mouse hovers over an element, you can add different styles to it through :hover to enhance user experience and interaction. This article will discuss in detail: the meaning of hover and give specific code examples. First, let us understand the basic usage of :hover in CSS. In CSS, you can use a selector to select the element to which the :hover effect is to be applied, and add after it

How to create text carousel and image carousel? The first thing everyone thinks of is whether to use js. In fact, text carousel and image carousel can also be realized using pure CSS. Let’s take a look at the implementation method. I hope it will be helpful to everyone!

There are two ways to remove dots from li tags in CSS: 1. Use the "list-style-type: none;" style; 2. Use transparent images and "list-style-image: url("transparent.png"); "style. Both methods can remove the dots of all li tags. If you only want to remove the dots of certain li tags, you can use a pseudo-class selector.

How to use:nth-child(-n+5) pseudo-class selector to select the CSS style of child elements whose position is less than or equal to 5. In CSS, the pseudo-class selector is a powerful tool that can be selected through a specific selection method. Certain elements in an HTML document. Among them, :nth-child() is a commonly used pseudo-class selector that can select child elements at specific positions. :nth-child(n) can match the nth child element in HTML, and :nth-child(-n) can match
