InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

CSS selectors

    Simple Selectors

    • type selectors
    • universal selector (*)
    • attribute presence and value selectors
    • attribute substring selectors
    • class selectors
    • two classes (.c1.c2)
    • id selectors

    Pseudo-classes

    • :link
    • :visited
    • :hover
    • :active
    • :focus
    • :target
    • :lang
    • :enabled and :disabled
    • :checked
    • :root
    • :nth-child
    • :nth-of-type
    • :nth-child vs :nth-of-type
    • :nth-last-child
    • :nth-last-of-type
    • :first-child
    • :first-of-type
    • :last-child
    • :last-of-type
    • :only-child
    • :only-of-type
    • :empty
    • :not

    Pseudo-elements

    • ::first-line
    • ::first-letter
    • ::before and ::after
    • ::placeholder

    Combinators

    • child vs descendant selector
    • adjacent sibling selector (E1 + E2)
    • general sibling selector (E1 ~ E2)

    Specificity

    • Selector specificity
     
    • Home
    • > Tutorials
    • > CSS
    • > CSS selectors

    CSS – child vs descendant selector examples

    By admin on Jan 15, 2016

    CSS direct child can be specified using A > B and any descendant can be specified using A B. This should be used with care as it can lead to surprises in case there are nested elements in html.
    css-direct-child-vs-descendant

    Direct child (A > B) example

    Here background color of any direct child is changed to lightgray.

    <style type="text/css">
    .foo > p {background:lightgray;}
    </style>
    <div class="foo">
    <p>direct child1</p>
    <p>direct child2</p>
    <div><p>descendant</p><div>
    </div>
    refresh done

    Any descendant (A B) example

    Here background color of any descendant is changed to lightgray. This will change color of all descendants including all direct children also.

    <style type="text/css">
    .foo p {background:lightgray;}
    </style>
    <div class="foo">
    <p>direct child1</p>
    <p>direct child2</p>
    <div><p>descendant</p><div>
    </div>
    refresh done

    Related

    • CSS adjacent sibling selector
    • CSS general sibling selector

    Suggested posts:

    1. CSS ::before and ::after examples
    2. PHP regex – word boundary examples
    3. Css :last-child selector – last child element
    4. CSS border radius and round corners examples
    5. CSS selector specificity
    6. Mysql how to dump schema of all databases
    7. awk sum examples
    8. WordPress – write custom php log to separate file
    Share this article: share on facebook share on linkedin tweet this submit to reddit
    Posted in Tutorials | Tagged CSS, CSS selectors, Web Development
    • Browse content
    • Article Topics
    • Article archives
    • Contact Us
    Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

    Copyright © 2025 InfoHeap.

    Powered by WordPress