Parent and Child Elements in CSS


In CSS, parent and child elements refer to the relationship between HTML elements in a document structure. Understanding this relationship is crucial for applying styles selectively to specific elements or groups of elements.

A parent element is an HTML element that contains one or more child elements. It acts as a container for its child elements and can have multiple levels of nesting. The child elements are elements directly nested within the parent element

CSS provides several selectors to target and style parent and child elements:

  1. Descendant Selector: The descendant selector allows you to target child elements based on their relationship to a parent element. It uses a space ( ) to separate the parent and child elements in the selector. For example, to select all <p> elements that are descendants of a <div> element, you can use the following selector: div p.
  2. Direct Child Selector: The direct child selector, also known as the child combinator (>), selects only the immediate child elements of a parent element. It targets elements that are direct children and ignores elements at deeper levels of nesting. For example, to select all <li> elements that are direct children of a <ul> element, you can use the following selector: ul > li.
  3. Sibling Selector: The sibling selector allows you to target elements that share the same parent and are adjacent to each other in the HTML structure. It uses the tilde (~) symbol. For example, to select all <p> elements that come after an <h2> element, you can use the following selector: h2 ~ p.

When styling parent and child elements, CSS also introduces concepts such as cascading styles, inheritance, and specificity:

  • Cascading Styles: CSS styles can be applied in multiple ways, such as inline styles, internal stylesheets, or external stylesheets. Styles defined closer to the target element take precedence over styles defined in other stylesheets.
  • Inheritance: Some CSS properties are inherited by child elements from their parent elements. This means that if a property is set on a parent element, the child elements will inherit that property value unless overridden.
  • Specificity: Specificity determines which CSS rules take precedence when multiple rules target the same element. It is calculated based on the type of selector used (e.g., class, id, element), the number of selectors, and the presence of inline styles.

By understanding the parent-child relationship and utilizing CSS selectors effectively, you can target and style specific elements within a document structure, allowing for more precise control over the appearance and layout of your web page.


Source Code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Tutor Joes</title>
    <style>
        body
	{
          color:orange;
        }

        div >p
	{
          color:red;
        }
        
        a
	{
         color:violet;
         text-decoration: none;
        }
    </style>
</head>
<body>
    <h1>Tutor Joes</h1>
    <h3>Parent And Child in CSS</h3>
    <div>
        <h4>Heading</h4>
        <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Explicabo, illum.</p>
    </div>

    <h4>Heading -2 </h4>
    <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Unde cupiditate ea cum amet sunt tenetur,
    esse incidunt officia nemo quasi!</p>
    <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Unde cupiditate ea cum amet sunt tenetur,
    esse incidunt officia nemo quasi!</p>
    <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Unde cupiditate ea cum amet sunt tenetur,
    esse incidunt officia nemo quasi!</p>
    <a href="#">Read More</a>
</body>
</html>
Live Preview

  1. body { color: orange; }: This rule targets the <body> element and sets the color property to orange. It means that all text within the <body> element, unless overridden by more specific rules, will be displayed in orange color.
  2. div > p { color: red; }: This rule targets <p> elements that are direct children of <div> elements. It uses the direct child selector (>). It sets the color property to red for such <p> elements. This rule will not affect <p> elements that are not direct children of <div> elements.
  3. a { color: violet; text-decoration: none; }: This rule targets all <a> (anchor) elements. It sets the color property to violet, meaning the text of the anchor elements will be displayed in violet color. Additionally, it sets the text-decoration property to none, removing any underlines or decorations typically applied to anchor elements.

By applying these CSS rules to an HTML document, you would see the following effects:

  • The text within the <body> element will be displayed in orange color, as defined by the body rule.
  • <p> elements that are direct children of <div> elements will have their text displayed in red color, as defined by the div > p rule.
  • All anchor (<a>) elements will have their text displayed in violet color and will have no text decoration, as defined by the a rule.

It's important to note that the actual appearance of these elements may be influenced by other stylesheets, the order of the CSS rules, and the specificity of the selectors used. CSS styles cascade and are applied based on a set of rules to resolve conflicts and determine the final rendering of elements on a web page.

CSS Tutorial


Properties Reference


Cascading Style Sheet


Flexbox Tutorial


CSS Grid Tutorial


Transitions Properties


CSS Properties with Examples


CSS Selectors


CSS Pseudo Elements


CSS Attribute Selectors


Input Pseudo Classes


CSS Examples


CSS Animation Projects