MOHA

남는 건 기록뿐이니까요

See how I grow

Front/CSS

[CSS] 선택자

liMoHa 2021. 7. 12. 14:16

css 선택자 연습할 수 있는 사이트https://flukeout.github.io/

 

CSS Diner

A fun game to help you learn and practice CSS selectors.

flukeout.github.io

 

위 사이트에서 언급된 선택자와 이를 이용하는 방법을 정리. 나중에 새로 알게 된 것이 있다면 추가로 정리하자.

 

      1. Type Selector
         A 
      2.  ID Selector
         #A  
      3.  Descendant Selector
         A  B 

        A>B와 차이점
        <A>
          <B>
          </B>
          <C>
            <B>
            </B>
          </C>
        </A>​
         A B 는 여기서 C안에 있는 B도 선택하지만  A > B 는 A바로 아래에 있는 자식 B만 선택함.
      4. Combine the Descendant & ID Selectors
         A#B 
      5. Class Selector
         .A 
      6. Combine the Class Selector
         A.B 
      7. Comma Combinator
         A, B 
        선택자는 comma(,)로 연결하여 사용할 수 있다. 즉,  h1태그와 special 클래스를 동시에 선택한 것이 된다.또, 가독성을 높이기 위해 다음 줄에 선택자를 작성할 수도 있다. 이때 comma는 첫 번째에 위치해도 되고 다음 줄에 위치해도 된다.
      8. The Universal Selector
          *  
      9. Combine the Universal Selector
         A * 
      10. Adjacent Sibling Selector
         A + B 
        A 다음으로 오는 B 한 개를 선택한다. 
      11. General Sibling Selector
         A ~ B 
        A 다음으로 오는 B를 '전부' 선택한다.
      12. Child Selector
         A > B 


      13. First Child Pseudo-selector
         :first-child 
        가장 위에 있는 orange를 선택하는방법: plate>orange:first-child or plate orange:first-child
      14. Only Child Pseudo-selector
         :only-child 
      15. Last Child Pseudo-selector
         :nth-last-child(A) 
      16. Nth Child Pseudo-selector
         : nth-child(A) 
      17. Nth Last Child Selector
         :nth-last-child(A) 
        뒤에서 부터 셈.


      18. First of Type Selector
         :first-of-type 
      19. Nth of Type Selector
         :nth-of-type(A) 
        A에는 even, odd와 같은 값이 들어갈 수 있음.
      20. Nth-of-type Selector with Formula
         :nth-of-type(An+B) 
        여기서 3번째, 5번째 plate선택하려면 n=0부터 시작하므로 2n+3하면 됨.
        plate:nth-of-type(2n+3)
      21. Only of Type Selector
         :only-of-type 
      22. Last of Type Selector
         :last-of-type 

      23. Empty Selector
         :empty 
      24. Negation Pseudo-class
         :not(X) 
        X안에 들어간 선택자는 제외하고 선택.

        큰 사과 2개 선택: apple:not(.small)

      25. Attribute Selector
         [attribute] 
        속성을 가지고 있는 요소를 선택할 때 사용
        어떤 태그인지는 관계 없이 지정한 속성만 가지고 있다면 모두 선택.
      26. Attribute Selector
         A[attribute] 
        속성을 가지고 있는 특정 요소를 선택할 때 사용
        예를 들어 <a href=""></a>가 있을 때 a[href]는 href속성을 가지고 있는 모든 a type을 선택.
      27. Attribute Value Selector
         [attribute="value"] 
        정확한 값이 있는 속성을 가지는 요소를 선택할 때 사용
      28. Attribute Starts With Selector
        [attribute^="value"]
        문자열이 value와 같은 문자열로 시작하면 선택
      29. Attribute Ends With Selector
        [attribute$="value"]
        문자열이 value와 같은 문자열로 나면 선택
      30. Attribute Wildcard Selector
        [attribute*="value"]
        value가 포함되기만 하면 선택

 

 

 

 

 

 

 

사진 출처: https://developer.mozilla.org/en-US/docs/Learn/CSS/Building_blocks/Selectors#what_is_a_selector

'Front > CSS' 카테고리의 다른 글

[CSS] flex-box  (0) 2021.07.17
[CSS] 레이아웃  (0) 2021.07.12