: nième enfant entre deux index fixes
I needed to select some elements between two fixed indexes the other day — like literally the second through fifth elements. Ironically, I have a whole post on “Useful :nth-child Recipes” but this wasn’t one of them.
The answer, it turns out, isn’t that complicated. But it did twist my brain a little bit.
Say you want to select all divs from the second one and beyond:
div:nth-child(n + 2) { } /* [ ] [x] [x] [x] [x] [x] [x] [x], etc. */
That makes logical sense to me. If n is 0, the expression is 2, and n increments upwards from there and selects everything beyond it.
But then how do you “stop” the selecting at a specific index? Like…
/* Not real */ div:nth-child(minmax(2, 5)) { } /* [ ] [x] [x] [x] [x] [x] [ ] [ ], etc. */
Well, we can do the opposite thing, selecting only the first set of elements then stopping (constraining in the other direction) by reversing the value of n.
div:nth-child(-n + 6) { } /* [x] [x] [x] [x] [x] [ ] [ ] [ ], etc. */
That will select the the first five elements and then stop because, as n gets bigger, the expression value goes to 0 and into negative numbers.
So, the CSS trick here is to combine both of those :nth-child expressions.
We know that CSS pseudo-selectors are additive in the sense that they must both be true in order to select them.
a:first-child:hover { /* selects the <a> if it is BOTH the first child and in a hover state */ }
To accomplish the idea of “2 and over” and “5 and under” we chain the pseudo-selectors:
div:nth-child(n + 2):nth-child(-n + 6) { background: green; }
That’ll do:
The part that twisted my brain was thinking about “additive” pseudo-selectors. I was thinking that selecting “2 and up” would do just that, and “5 and under” would do just that, and those things combined meant “all elements.” But that’s just wrong thinking. It’s the conditions that are additive, meaning that every element must meet both conditions.
If you found this confusing like I did, wait until you check out Quanity Queries. By nesting a lot of nth-style pseudo-selectors, you can build logic that, for example, only selects elements depending on how many of them are in the DOM.
div:nth-last-child(n+2):nth-last-child(-n+5):first-child, div:nth-last-child(n+2):nth-last-child(-n+5):first-child ~ div { /* Only select if there are at least 2 and at most 5 */ }
Una broke this down even further for us a while back.
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Outils d'IA chauds

Undresser.AI Undress
Application basée sur l'IA pour créer des photos de nu réalistes

AI Clothes Remover
Outil d'IA en ligne pour supprimer les vêtements des photos.

Undress AI Tool
Images de déshabillage gratuites

Clothoff.io
Dissolvant de vêtements AI

Video Face Swap
Échangez les visages dans n'importe quelle vidéo sans effort grâce à notre outil d'échange de visage AI entièrement gratuit !

Article chaud

Outils chauds

Bloc-notes++7.3.1
Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise
Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1
Puissant environnement de développement intégré PHP

Dreamweaver CS6
Outils de développement Web visuel

SublimeText3 version Mac
Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Sujets chauds

Il est sorti! Félicitations à l'équipe Vue pour l'avoir fait, je sais que ce fut un effort massif et une longue période à venir. Tous les nouveaux documents aussi.

Avec la récente montée du prix du bitcoin sur 20k $ USD, et pour lui, récemment en train de briser 30k, je pensais que cela valait la peine de reprendre une profonde plongeon dans la création d'Ethereum

J'ai eu quelqu'un qui écrivait avec cette question très légitime. Lea vient de bloguer sur la façon dont vous pouvez obtenir les propriétés CSS valides elles-mêmes du navigateur. C'est comme ça.

L'autre jour, j'ai repéré ce morceau particulièrement charmant sur le site Web de Corey Ginnivan où une collection de cartes se cassent les uns sur les autres pendant que vous faites défiler.

Je dirais que "Site Web" correspond mieux que "Application mobile" mais j'aime ce cadrage de Max Lynch:

Si nous devons afficher la documentation à l'utilisateur directement dans l'éditeur WordPress, quelle est la meilleure façon de le faire?

Il existe un certain nombre de ces applications de bureau où l'objectif montre votre site à différentes dimensions en même temps. Vous pouvez donc, par exemple, écrire

Questions sur les zones de slash violet dans les dispositions flexibles Lorsque vous utilisez des dispositions flexibles, vous pouvez rencontrer des phénomènes déroutants, comme dans les outils du développeur (D ...
