상추의 IT저장소

CSS) 가상 선택자 정리 본문

CSS

CSS) 가상 선택자 정리

구너상추 2022. 11. 4. 18:30

nth-child, nth-of-type 정리

:nth-child(N)= 부모안에 모든 요소 중 N번째 요소

A:nth-of-type(N)= 부모안에 A라는 요소 중 N번째 요소

:first-child= 부모안에 모든 요소 중 첫번째 요소

:last-child= 부모안에 모든 요소 중 마지막 요소

A:first-of-type= 부모안에 A라는 요소 중 첫번째 요소

A:last-of-type= 부모안에 A라는 요소 중 마지막 요소

 

예제>>

.first :nth-child(2n){
            background-color: aqua;
        }
        .first :nth-child(2n+1) {
            background-color: yellow;
        }

        /* 첫번째 요소부터 5번째 요소까지 2개마다 선택 */
        .second span:nth-child(-2n+5){
            background-color: red;
        }
        /* 6번째부터 모두 선택 */
        .second span:nth-child(n+6){
            background-color: blueviolet;
        }
        /* 선택자를 두번 사용하여 교집합인 요소만 선택할 수 있다. */
        .third :nth-child(n+3):nth-child(-n+8) {
            background-color: brown;
        }

        .fourth p:first-of-type {
            background-color: orange;
        }
        .fourth span:last-of-type {
            background-color: orangered;
        }

 

>>결과

 

'CSS' 카테고리의 다른 글

CSS) Grid 레이아웃  (0) 2023.01.24
CSS) flexbox - flex 아이템에 적용하는 속성들  (0) 2023.01.05
CSS) font-face 사용하기  (0) 2022.12.20
CSS) flexbox - flexcontainer에 적용되는 속성  (0) 2022.11.10
CSS) 선택자  (0) 2022.11.05