What is the purpose of using the child selector on another child element?

While I am studying CSS, I have encountered an issue with the child selector not working properly.

<style>
    header > p{
        color: red;
    }
</style>
<header>
    <p>
        aaa
        <div>bbb</div>
    </p>
</header>

It only makes 'aaa' red and does not apply the style to 'bbb'.

 <style>
    header > div{
        color: red;
    }
</style>
<header>
    <div>
        aaa
        <p>bbb</p>
    </div>
</header>

Interestingly, when using this code, it applies the style to both 'aaa' and 'bbb'. Why is that?

Answer №1

The reason for this issue is due to the fact that your initial example does not adhere to valid HTML standards.

As outlined in the HTML specification, <p> elements do not permit block-level elements like <div> within them.

Therefore, the browser (following tag omission rules) closes the <p> element prior to the <div> and then initiates a new <p> when it encounters the closing </p> tag.

You can verify this behavior by inspecting the browser console or using certain development tools that will highlight this incorrect arrangement of DOM elements.

If you substitute the <div> with a <span>, you will notice that it retains the red color as intended.

https://i.sstatic.net/3OrNR.png

For further details, refer to this comprehensive explanation:

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for creating a hidden tab that only appears when hovered over

When hovering over the link tag .cat-link a.navlink, each subcategory tab .subcategories div is displayed. However, I would like to hide all tabs initially and have them appear only when hovered over with the mouse. Once the mouse is removed, I want the t ...

The relative positioning is currently being obstructed

Having some trouble using position:relative to shift my logo to the left of my webpage. Oddly, moving it 20px seems to have no effect, but shifting it by 300px downwards causes it to vanish completely. Here's a snippet of my current code: .container ...

Can someone explain why my button icons are not aligning to the center properly?

I have a small issue with the icons I pulled from react-icons - they appear slightly raised within the buttons. How can I position them in the middle? That's my only query, but I can't post it alone due to mostly having code in my post. What an ...

How can I adjust the alignment of the text in a select menu in jQuery Mobile?

Exploring the world of jquery for the first time, I am excited about building a mobile web application. Utilizing jquery mobile (1.4.5), I encountered a simple issue - I couldn't figure out how to change the location of the text in the select menu. De ...

How to resolve the issue of a sticky header not functioning properly with a resizable column in Primeng

Currently, I am facing an issue while trying to incorporate both column resize functionality and sticky header in my table. Interestingly, the sticky header feature works perfectly fine when used alone without the column resize. However, when I attempt to ...

Highlighting the selected value in an Angular Material mat option

I am looking to enhance the background color of the selected value when it is clicked. Unlike the example I found, which highlights multiple select values, I only want to highlight the selected value when choosing an option. Check out this example for ref ...

filtering elements with selectable functionality in jQuery UI

I'm trying to exclude the <div> children from being selected within this list item and only select the entire <li>. The structure of the HTML is as follows: <ul id="selectable"> <li> <div id="1"></div> ...

The vertical measurement of the input box within the Contact Form 7

How can I decrease the height of the "Your Message" input box on the Contact 7 form located at this webpage: I have attempted the following methods (using different heights to test) but none seem to be effective. Any assistance would be greatly appreciate ...

Create a single button that controls checkboxes with the help of HTML, CSS, SCSS, and Bootstrap 4

Is there a way to use only HTML, CSS, SCSS, and Bootstrap 4 to make all checkboxes default to the "on" condition when a button is clicked? Typically, this functionality would be achieved with JavaScript, but due to constraints, I am exploring alternative ...

Unable to use column formatting on a row using flexbox

I am working on creating a layout with rows and columns where the content in each column is vertically aligned. Here is an example of what I am trying to achieve: Currently, I am using react and have multiple components. The approach I believe will work ...

Incorporate a link to an image following a click event toggle using Jquery

I managed to create a code snippet that toggles between two images when clicked, thanks to some assistance from stackoverflow. However, I am now looking to add a link to the second image that redirects users to another webpage, like The App Store. My ques ...

Ensure the website is able to adjust its size when the top banner increases in size

On my website, there is a top banner containing contact information that expands when clicked. However, the expanded div overlaps the site content. Is there a way to make the site scale with the expanded div so that the top of the site starts below it? I ...

implementing a delay after hovering over a CSS hover effect before activating it

I'm trying to achieve a specific effect using JavaScript or jQuery, but I'm struggling to figure it out. I have created a simple CSS box with a hover effect that changes the color. What I want is for the hover effect to persist for a set amount o ...

Maximizing input spacing with Bootstrap3

I am working on a single page application using Bootstrap 3 and AngularJS. Instead of using a datepicker to select dates, I want to have separate inputs for day, month, and year. Here is the HTML code for the date fields: <div class="col-m ...

The content within my "div class=content" is intersecting with the nav-justified section

My login page features a nav-justified list that allows users to connect using either a login/password or guest mode. However, I have encountered an issue where clicking on "Login" still displays the option for "Guest," and if I click on "Guest" again, it ...

Styling jQuery Datatables Buttons with CSS Alignment

Currently, I am actively involved in developing a Sharepoint web part that leverages JQuery datatables to format specific reporting results. Here is an overview of how the report appears: https://i.sstatic.net/jXaiN.png An issue has arisen concerning the ...

Styling WordPress Ninja Tables with CSS

I am currently seeking a solution to customize each table generated through CSS. In the past, I have utilized tools like TablePress which assigned a unique "id" to each table, allowing for individual customizations when inspecting through Google Chrome - f ...

What is the process for validating the CSS background-image URL using Javascript?

The concept here is to verify the URL of the CSS element's id (.boot). If the URL matches, which it does, then it should display "hello world." However, despite everything seeming correct, the expected behavior is not occurring, and the reason behind ...

Is your PHP inquiry form malfunctioning?

Having trouble with my website creation! PHP and HTML not cooperating. Data input not sending to email, validation showing instead of placeholder in input forms. Please help!!:-) Here is a snippet of my HTML: <form class="form-horizontal" role="form" ...

The @media query appears to be functioning properly, however, it is not producing the

Conducted a basic test on @media queries to determine functionality: Visit the test page here I have set up a @media query in test.css that is supposed to change the background color from black to red on landscape tablets (1024x768). The CSS and XHTML are ...