찾다
웹 프론트엔드CSS 튜토리얼: 두 개의 고정 인덱스 사이의 nth-Child

:nth-child Between Two Fixed Indexes

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 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
스타일링 된 구성 요소로 React 디자인을 반복합니다스타일링 된 구성 요소로 React 디자인을 반복합니다Apr 21, 2025 am 11:29 AM

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

오, 삼각형 빵 부스러기 리본을 만드는 많은 방법!오, 삼각형 빵 부스러기 리본을 만드는 많은 방법!Apr 21, 2025 am 11:26 AM

오, 삼각형 빵 부스러기 리본을 만드는 많은 방법

CSS 가이드의 SVG 특성CSS 가이드의 SVG 특성Apr 21, 2025 am 11:21 AM

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

교차로 관찰자가 요소가 시야에있을 때를 알 수있는 몇 가지 기능적 사용교차로 관찰자가 요소가 시야에있을 때를 알 수있는 몇 가지 기능적 사용Apr 21, 2025 am 11:19 AM

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

회복하는 것은 감소 된 모션을 선호합니다회복하는 것은 감소 된 모션을 선호합니다Apr 21, 2025 am 11:18 AM

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

Google Play 스토어에 진보적 인 웹 앱을 가져 오는 방법Google Play 스토어에 진보적 인 웹 앱을 가져 오는 방법Apr 21, 2025 am 11:10 AM

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

HTML을 처리하는 가장 간단한 방법에는 포함됩니다HTML을 처리하는 가장 간단한 방법에는 포함됩니다Apr 21, 2025 am 11:09 AM

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

호버에서 SVG의 색상을 변경하십시오호버에서 SVG의 색상을 변경하십시오Apr 21, 2025 am 11:04 AM

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

See all articles

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

Video Face Swap

Video Face Swap

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

뜨거운 도구

맨티스BT

맨티스BT

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

Dreamweaver Mac版

Dreamweaver Mac版

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

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

PhpStorm 맥 버전

PhpStorm 맥 버전

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

WebStorm Mac 버전

WebStorm Mac 버전

유용한 JavaScript 개발 도구