CSS 결합자 자세히 살펴보기

9484 단어 htmlcss

소개



CSS에는 웹 페이지에서 HTML 요소를 선택할 때 여러 옵션이 있습니다. 이러한 옵션을 선택기라고 합니다. 일부 선택기는 다른 것보다 상당히 유명하고 더 많이 사용되며 인기 있는 선택기는 모든 개발자가 CSS를 시작할 때 배우는 것입니다. 이 기사에서는 CSS Combinators라고 하는 가장 인기 없는 선택기 중 하나에 대해 배울 것입니다.

목차


  • What are combinators?
  • Descendant combinator
  • Child combinator
  • Adjacent sibling combinator
  • General sibling combinator
  • A note on combinators
  • Summary
  • Conclusion

  • 콤비네이터란?

    Combinators selectors combine selectors in the way that it is evident some relationship exists between the selectors.

    The combinators are classified into four groups:

    • Descendant combinator
    • Child combinator
    • Adjacent sibling combinator
    • General sibling combinator

    하위 조합기

    The word "descendant" should give you an idea that for the combinator to match any element, an ancestor element must exist. The least being a parent element.

    main p {
        font-size: 1.2em;
    }
    

    In the code snippet above, main is the parent element and p is a child element therefore, this selector will match all p which are descendants of main and nothing else.

    자식 결합자

    The child combinator is represented by > , and this symbol is placed between two selectors (or more). It will only match an element if the second selector is a direct child of the first selector.

    main > p {
        font-size: 1.2em;
    }
    

    The selector above will match paragraphs that are direct children of main and nothing else. Therefore, if there is any containing element like a div or and article with a paragraph within it, this selector will not match them.

    <main>
        <p>
            This paragraph is selected.
        </p>
        <article>
            <p>
            But not this paragraph.
            </p>
        </article>
        <p>
            Also, this is selected.
        </p>
    </main>
    

    인접 형제 결합자

    This combinator matches an element if it is next to an element in a document hierarchy. It is represented by the addition symbol in mathematics i.e. +

    main + p {
        font-size: 2em;
        background-color: #000000;
        color: #ffffff;
    }
    

    This will match paragraphs that is next to main in the HTML markup and not paragraphs within main .

    <main>
        <p>
            This paragraph is not selected.
        </p>
        <p>
            Also, this is not selected.
        </p>
    </main>
    <p>
        But this is selected.
    </p>
    <p>
        This is also not selected.
    </p>
    

    일반 형제 결합자

    This combinator is represented by the tilde sign ~ . When this combinator is used between two selectors, all occurrence of the second selector are matched even if they are not adjacent to the first selector.

    main ~ p {
        font-size: 2em;
        background-color: #000000;
        color: #ffffff;
    }
    

    Unlike the adjacent sibling selector that selects one sibling after the first selector, this combinator selects all occurrence of elements matched by the second selector which occurs after the first selector in the document hierarchy.

    <main>
        <p>
            This paragraph is not selected.
        </p>
        <p>
            Also, this is not selected.
        </p>
    </main>
    <p>
        But this is selected.
    </p>
    <p>
        Also, this.
    </p>
    <p>
        And this.
    </p>
    

    결합자에 대한 참고 사항

    Combinators are extremely useful when you intend to target specific parts of your document, but it's difficult to reuse the CSS code elsewhere because it's solely for that specific part in your HTML document. It is advisable to create classes with semantic names that are reusable.

    요약

    Combinator Sign Example Location of matched element Number of matched element
    Descendant combinator space main p Within the first selector All occurrence of the second selector within the first selector.
    Child combinator > main > p Within the first selector All occurrence of the second selector that is a child of the first selector only.
    Adjacent sibling combinator + main + p Outside the first selector The first occurrence of the second selector after the first selector in the document hierarchy.
    General sibling combinator ~ main ~ p Outside the first selector All occurrence of the second selector after the first selector in the document hierarchy.

    결론

    In this article we discussed combinator selectors which is a class of CSS selectors that are sparingly used compared to class selectors and ID selectors but sometimes come in handy when the markup is not large (and hopefully not) and you have no control over the HTML markup e.g. when it is generated by a Content Management System (CMS).

    좋은 웹페이지 즐겨찾기