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 */ }</a>
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.
위 내용은 : 두 개의 고정 인덱스 사이의 nth-Child의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

완벽한 세상에서 우리의 프로젝트에는 무제한 자원과 시간이 있습니다. 우리 팀은 잘 생각되고 고도로 세련된 UX 디자인으로 코딩을 시작할 것입니다.

SVG에는 인라인 SVG 코드가 길고 복잡해질 수있는 정도까지 자체 요소, 속성 및 속성 세트가 있습니다. CSS와 SVG 2 사양의 다가오는 기능 중 일부를 활용하면 해당 클리너 마크 업 코드를 줄일 수 있습니다.

당신은 이것을 알지 못할 수도 있지만, JavaScript는 최근에 많은 관찰자들을 은밀하게 축적했으며, 교차로 관찰자는 그 일부입니다.

모든 CSS 애니메이션을 버릴 필요는 없습니다. 노동을 선호하는 것이 아니라 감소 된 모션을 선호한다는 것을 기억하십시오.

PWA (Progressive Web Apps)는 한동안 우리와 함께있었습니다. 그러나 고객에게 설명 할 때마다 같은 질문이 나타납니다. "내 사용자가

HTML이 다른 HTML 파일을 포함시킬 방법이 없다는 것은 매우 놀랍습니다. 수평선에 어떤 것도있는 것 같지 않습니다.

SVG를 사용하는 방법에는 여러 가지가 있습니다. 어느 길에 따라, 다른 상태 또는 조건에서 SVG를 다시 채우기위한 전술 - : 호버,


핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

Video Face Swap
완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

인기 기사

뜨거운 도구

맨티스BT
Mantis는 제품 결함 추적을 돕기 위해 설계된 배포하기 쉬운 웹 기반 결함 추적 도구입니다. PHP, MySQL 및 웹 서버가 필요합니다. 데모 및 호스팅 서비스를 확인해 보세요.

Dreamweaver Mac版
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

PhpStorm 맥 버전
최신(2018.2.1) 전문 PHP 통합 개발 도구

WebStorm Mac 버전
유용한 JavaScript 개발 도구
