back

</>CodeWithRahul

More
  • Home
  • Cources
  • Tutorials
  • Tutorials
  • Projects
  • Notes
  • Contact
  • Work With Us
  • HTML
  • CSS
  • JAVASCRIPT
  • CC++
  • PHP
  • PYTHON
  • REACT JS
  • DSA

</>Code With Rahul

Create an account
  • INTRODUCTION
    • Information & History
    • Your first CSS Website
    • How CSS Works?
    • Ways to add CSS
    • CSS Selectors
    • CSS Comments
  • CSS Properties
    • CSS colors
    • CSS backgrounds
    • CSS Borders
    • CSS Image
    • CSS Videos Embedding
    • CSS Fonts
    • CSS Text Styling
    • CSS Padding
    • CSS Margin
    • CSS Hover
    • CSS Links
    • CSS Combinators
    • CSS Pseudo Classes
    • CSS Buttons
    • CSS Overflow
    • CSS Float
    • CSS !Important
    • CSS Maths Function
    • CSS Size
    • CSS Positioning
    • CSS Z-index
    • CSS Forms
    • CSS Navigation
  • CSS Designing
    • CSS Display
    • CSS FlexBox
    • CSS Grid
    • CSS Meida Queries
  • CSS Advance Topics
    • CSS CSS 2D
    • CSS Transform
    • CSS Transition
    • CSS Border Image
    • CSS Gradients
    • CSS Inherit
    • CSS Shadows
    • CSS ToolTip Text
    • CSS Border Image
    • CSS Masking
    • CSS Pagination
    • CSS Meida Queries Advance
    • CSS Animation
  • CSS FAGS
    • CSS Questions

    CSS Combinators

    It explains the relation between multiple or single selector. There are four major combinators that we would be looking at here.

    Descendant Selector

    It selects all the elements to present inside another specified HTML element.

    Example:

    Combinators

    Output:

    Combinators

    As you can see, div had 4 paragraphs and so, CSS rules were applied on all of them.

    Child Selector

    It selects only the first generation descendants of a specified element.

    Example:

    <style>
        div>p{
            color: wheat;
            background-color: rebeccapurple;
        }
    </style>

    Just using this child selector in the same code the output changes to this:

    Output:

    Combinators

    It happened because paragraph 3 is nested in the <section> tag and therefore is 2nd generation to the <div> selector and thus wasn’t selected.

    Adjacent Sibling Selector

    As the name suggests this selector only selects the adjacent element to the specified element.

    Example:

    <style>
        div+p{
            color: wheat;
            background-color: rebeccapurple;
        }
    </style>

    Now, the <p> tag right after <div> ends, would be selected. So, the output would look like this:

    Output:

    Combinators

    General Sibling Selector:

    Unlike the adjacent selector, this one going to select all the <p> tags present after >div<.

    Example:

    <style>
        div~p{
            color: wheat;
            background-color: rebeccapurple;
        }
    </style>

    Output:

    Combinators
    Owner of this WebPage Image

    CodeWithRahul

    Copyright © 2023 CodeWithRahul.com