Home Web Front-end CSS Tutorial Use the :nth-last-of-type(3) pseudo-class selector to select the style of the third-to-last element among the same type

Use the :nth-last-of-type(3) pseudo-class selector to select the style of the third-to-last element among the same type

Nov 20, 2023 am 09:04 AM
style Pseudo class selector third to last

Use the :nth-last-of-type(3) pseudo-class selector to select the style of the third-to-last element among the same type

Use the :nth-last-of-type(3) pseudo-class selector to select the third-to-last style among elements of the same type. Specific code examples are required

In CSS, we often need to style elements in web pages. Sometimes, we need to select the third to last element among the same type of elements and apply a specific style to it. At this time, we can use the :nth-last-of-type(3) pseudo-class selector to complete this task.

First, let us take a look at the syntax of the :nth-last-of-type(3) pseudo-class selector:

:nth-last-of-type(n)

In the above code, n represents an integer, indicating which element from the bottom we want to select. In our requirement, we need to select the third to last element, so we set n to 3.

Next, let’s look at a specific example:

HTML code:

<ul>
    <li>列表项1</li>
    <li>列表项2</li>
    <li>列表项3</li>
    <li>列表项4</li>
    <li>列表项5</li>
    <li>列表项6</li>
</ul>
Copy after login

CSS code:

li:nth-last-of-type(3) {
    color: red;
    font-weight: bold;
}
Copy after login

In the above example, we have An unordered list containing 6 list items. We want to select the third to last list item and apply a red font and bold style to it. We can use the :nth-last-of-type(3) selector to achieve this purpose.

When we run the above code, list item 4 will be selected as the third to last element, and red font and bold style will be applied. Other list items will not be affected.

As a reminder, when using the :nth-last-of-type(3) selector, we need to pay attention to the hierarchical relationship of the selector. If our target element is not a direct descendant element, but is nested in other elements, then we need to adjust the way the selector is written according to the specific situation. Regarding the hierarchical rules of selectors, you can refer to the relevant specifications of CSS selectors.

To sum up, using the :nth-last-of-type(3) pseudo-class selector can conveniently select the third to last element among the same type elements and apply the specified style to it. Through the above examples, we can quickly get started and understand how to use this selector, so as to better apply it in actual development.

The above is the detailed content of Use the :nth-last-of-type(3) pseudo-class selector to select the style of the third-to-last element among the same type. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

macOS: How to change the color of desktop widgets macOS: How to change the color of desktop widgets Oct 07, 2023 am 08:17 AM

In macOS Sonoma, widgets don't have to be hidden off-screen or forgotten in the Notification Center panel like they did in previous versions of Apple's macOS. Instead, they can be placed directly on your Mac’s desktop – they’re also interactive. When not in use, macOS desktop widgets fade into the background in a monochrome style, reducing distractions and allowing you to focus on the task at hand in the active application or window. However, when you click on the desktop, they return to full color. If you prefer a drab look and want to retain that aspect of uniformity on your desktop, there's a way to make it permanent. The following steps demonstrate how it is done. Open the System Settings app

Guide to solving misalignment of WordPress web pages Guide to solving misalignment of WordPress web pages Mar 05, 2024 pm 01:12 PM

Guide to solving misaligned WordPress web pages In WordPress website development, sometimes we encounter web page elements that are misaligned. This may be due to screen sizes on different devices, browser compatibility, or improper CSS style settings. To solve this misalignment, we need to carefully analyze the problem, find possible causes, and debug and repair it step by step. This article will share some common WordPress web page misalignment problems and corresponding solutions, and provide specific code examples to help develop

What does hover mean in css What does hover mean in css Feb 22, 2024 pm 01:24 PM

: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

CSS web background image design: create various background image styles and effects CSS web background image design: create various background image styles and effects Nov 18, 2023 am 08:38 AM

CSS web page background image design: Create various background image styles and effects, specific code examples are required Summary: In web design, background images are an important visual element, which can effectively enhance the attractiveness and readability of the page. This article will introduce some common CSS background image design styles and effects, and provide corresponding code examples. Readers can select and apply these background image styles and effects according to their own needs and preferences to achieve better visual effects and user experience. Keywords: CSS, background image, design style, effect, code representation

How to remove the dot in front of the li tag in css How to remove the dot in front of the li tag in css Apr 28, 2024 pm 12:36 PM

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 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 Nov 20, 2023 am 11:52 AM

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

The role of hover in html The role of hover in html Feb 20, 2024 am 08:58 AM

The role of hover in HTML and specific code examples In web development, hover refers to triggering some actions or effects when the user hovers the cursor over an element. It is implemented through the CSS :hover pseudo-class. In this article, we will introduce the role of hover and specific code examples. First, hover enables an element to change its style when the user hovers over it. For example, when hovering the mouse over a button, you can change the button's background color or text color to remind the user what to do next.

What does :: mean in css What does :: mean in css Apr 28, 2024 pm 03:45 PM

The :: pseudo-class selector in CSS is used to specify a special state or behavior of an element, and is more specific than the pseudo-class selector : and can select specific attributes or states of an element.

See all articles