search
HomeWeb Front-endCSS TutorialDetailed explanation of examples of selectors in CSS3

Detailed explanation of examples of selectors in CSS3

May 23, 2017 am 11:06 AM
useelementmatchsupportTutorialBrowserWeb pageWeb page productionchoose

English original text: www.456bereastreet.com/archive/200601/css_3_selectors_explained/ Chinese translation: www.dudo.org/article.asp?id=197 Note: This article was written in January 2006, when IE7, IE8 and Firefox3 had not yet been released. All the browser support mentioned in the article did not include these three versions of browsers. Use in IE8 and Fir
:nth-of-type()
:nth-of-type() pseudo-class and nth-child() pseudo-class The method is very similar, except that it matches elements of the type specified in the rule. The following rule will match every p element that belongs to the third child element of the parent element (there may be many p elements belonging to the third child element in one, but they are located under different parent elements, dudo note):
p:nth-of-type(3) { background:#ff0; }
This method is useful when you want to determine whether the third p element has been pointed to. At first you may think that this has the same effect as using nth-child, but nth-child(3) will count all child elements, so the result may be different, unless all sibling elements of p are also p element.
:nth-of-type pseudo-class is currently not supported by browsers.
:nth-last-of-type pseudo-class
: The element pointed to by the nth-last-of-type pseudo-class has several elements of the same type after it. Like the :nth-last-child pseudo-class, it counts backwards from the last child element. The following rules will match the penultimate sibling element p (note: it is a sibling and node of the same level):
p:nth-last-of-type(2) { background:#ff0 ; }
:nth-last-of-type() is currently not supported by browsers.
:last-child pseudo-class
:last pseudo-class points to the last child element in the parent element. It has the same effect as :nth-last-child(1). The following rule matches all p that is the last child element in the parent element:
p:last-child { background:#ff0; }
:last-child The pseudo-class can be used in Mozilla-based used in the browser. Opera does not support the :last-childe pseudo-class, and there is a bug in Safri (the above rule will match all p elements). Surprisingly it works fine in OmniWeb (beta version 5.1.1), even though this browser is based on Safari. This may be due to the return of the latest version of Apple's WebKit, as OmniWeb typically uses a slightly lower version of WebKit than Safari.
:first-of-type pseudo-class
:first-of-type pseudo-class points to the first element among elements of the same type. It is the same as :nth-of-type(1).
p:first-of-type { background:#ff0; }
: The first-of-type pseudo-class is currently not supported by browsers.
:last-of-type pseudo-class
:last-of-type pseudo-class points to the last element among elements of the same type. It is the same as nth-last-of-type(1).
p:last-of-type { background:#ff0; }
:last-of-type pseudo-class is currently not supported by browsers.
:only-child pseudo-class
:The only-childe pseudo-class points to elements that do not have any other child elements in their parent elements. It is the same as :first-child:last-child or :nth-child(1):nth-last-child(1) (the latter is not professional and concise enough).
p:only-child { backgound:#ff0; }
:only-childe pseudo-class is currently available in Mozilla-based browsers. In Safari it seems to be interpreted as: first-child (the above rule will be matched to all first child elements p in the document).
:only-of-type pseudo-class
:only-of-type pseudo-class points to those parent elements that no longer have child elements of the same type. This is the same as :first-of-type:last-of-type or :nth-of-type(1):nth-last-of-type(1) (the latter is not professional enough).
p:only-of-type { background:#ff0; }
:only-of-type pseudo-class is currently not supported by browsers.

【Related recommendations】

1. CSS3 free video tutorial

2. Integrate 20 common CSS/CSS3 properties

3. h5 and css3 to create an input box with prompt text

4. Share several commonly used latest css3 attributes

5. Use CSS to create small sharp corners and bubble effects of the chat box

The above is the detailed content of Detailed explanation of examples of selectors in CSS3. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
The Lost CSS Tricks of Cohost.orgThe Lost CSS Tricks of Cohost.orgApr 25, 2025 am 09:51 AM

In this post, Blackle Mori shows you a few of the hacks found while trying to push the limits of Cohost’s HTML support. Use these if you dare, lest you too get labelled a CSS criminal.

Next Level CSS Styling for CursorsNext Level CSS Styling for CursorsApr 23, 2025 am 11:04 AM

Custom cursors with CSS are great, but we can take things to the next level with JavaScript. Using JavaScript, we can transition between cursor states, place dynamic text within the cursor, apply complex animations, and apply filters.

Worlds Collide: Keyframe Collision Detection Using Style QueriesWorlds Collide: Keyframe Collision Detection Using Style QueriesApr 23, 2025 am 10:42 AM

Interactive CSS animations with elements ricocheting off each other seem more plausible in 2025. While it’s unnecessary to implement Pong in CSS, the increasing flexibility and power of CSS reinforce Lee's suspicion that one day it will be a

Using CSS backdrop-filter for UI EffectsUsing CSS backdrop-filter for UI EffectsApr 23, 2025 am 10:20 AM

Tips and tricks on utilizing the CSS backdrop-filter property to style user interfaces. You’ll learn how to layer backdrop filters among multiple elements, and integrate them with other CSS graphical effects to create elaborate designs.

SMIL on?SMIL on?Apr 23, 2025 am 09:57 AM

Well, it turns out that SVG's built-in animation features were never deprecated as planned. Sure, CSS and JavaScript are more than capable of carrying the load, but it's good to know that SMIL is not dead in the water as previously

'Pretty' is in the eye of the beholder'Pretty' is in the eye of the beholderApr 23, 2025 am 09:40 AM

Yay, let's jump for text-wrap: pretty landing in Safari Technology Preview! But beware that it's different from how it works in Chromium browsers.

CSS-Tricks Chronicles XLIIICSS-Tricks Chronicles XLIIIApr 23, 2025 am 09:35 AM

This CSS-Tricks update highlights significant progress in the Almanac, recent podcast appearances, a new CSS counters guide, and the addition of several new authors contributing valuable content.

Tailwind's @apply Feature is Better Than it SoundsTailwind's @apply Feature is Better Than it SoundsApr 23, 2025 am 09:23 AM

Most of the time, people showcase Tailwind's @apply feature with one of Tailwind's single-property utilities (which changes a single CSS declaration). When showcased this way, @apply doesn't sound promising at all. So obvio

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools