How to Select Child Elements with the Sibling Selector?

<p>hello
    <div>test</div>
</p>

<div>yo
    <p>hi</p>
</div>

<p>hello</p>

When I apply the styles below to the code above, I get unexpected results.

p + div {
    color:orange;
}

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

The style targets the div inside the p tag, but I expected it to only target immediate siblings on the same level. However, when I try:

div + p {
     color:orange;
}

The paragraph inside the div is not styled as shown here:

https://i.sstatic.net/C8Shd.png

Can anyone explain this behavior?

Answer №1

Looks like your HTML code is a bit wonky. The <div> element should not be nested inside a <p> tag, but don't worry - the browser will try to fix it for you by rearranging things like this:

<p>hello
  <div>test</div>
</p>
<div>yo
  <p>hi</p>
</div>
<p>hello</p>

The corrected version will look something like this:

<p>hello</p>
<div>test</div>
<p></p>
<div>yo
  <p>hi</p>
</div>
<p>hello</p>

Now the selector p + div will target the elements with content test and yo, while hi will inherit its parent's color.

Answer №2

When you place a div inside a p tag, your code breaks.

For example, <div>test</div> is treated as adjacent to the p tag and given an orange color.

Similarly, <div>yo<p>hi</p> </div> is seen as adjacent to another p tag and also receives an orange color.

To understand this better, take a look at the DOM structure created by the code above.

https://i.sstatic.net/XQOos.png

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

"Upon clicking the submit button, no action is triggered and no _POST values are being received

When a user clicks on my submit button, nothing seems to happen. The HTML code for the button is as follows: <td colspan="3"> <div align="center"> <input name="Decline" id="Decline" value="Decline" type="submit"> ...

What is the best way to convert CSS to JSS for Material-UI styling?

I need to convert some basic CSS to JSS and include it in the global Styles.js file. The original CSS is as follows: .formdetail { display: grid; grid-row-gap: 10px; grid-template-columns: 1fr 3fr; margin: 20px; } .formdetail .cell { display: ...

Is it possible to still alter options in a read-only Select component in Twitter Bootstrap

Even though I have set my bootstrap select to readonly="true", I am still able to change the selected option. I tried using disabled="true", but then the select is not included when submitted. Is there a way to achieve a combination where the selected op ...

What is the best way to position a div in relation to a table header (th) but not inside it?

I'm trying to create a table header that displays a help text (div) when clicked on. However, the help div is causing the table header to be larger than the other headers. Below is the code snippet: #help_box { border-radius: 20px; ba ...

Creating a JSON-based verification system for a login page

First time seeking help on a programming platform, still a beginner in the field. I'm attempting to create a basic bank login page using a JSON file that stores all usernames and passwords. I have written an if statement to check the JSON file for m ...

Show the nested div when hovering over the containing div using JavaScript

I have a situation where I have multiple divs within a parent div, all using the same class. Here is an example: <div class="deck-content"> <div class="deck-box">TEST< <div class="deck-hidden">< <span class= ...

I'm having trouble with my tablet view taking priority over my desktop view - what could be causing this issue?

Styling for different screen sizes: @media (min-width: 991px) and (max-width:768px){} .container, .container1, .container2 .container3{ float: left; } .container1{ width: 50%; } .container2{ width: 50%; } .container3{ width: 100%; } /**** ...

Display the AJAX reply within a designated div based on the selected value

I currently have two dropdown menus where the options in the second menu depend on what is selected in the first one. The behavior I want is for the second menu to update as soon as the user makes a selection in the first one, without needing to submit the ...

When attempting to read text from an HTML file, JTextPane fails to include a newline character in the output

I am currently working on a project that involves extracting text from an HTML file based on the element's id and displaying it in a JTextPane. However, I am facing an issue where the displayed text does not go to a new line. Here is a snippet of the ...

What is the best way to prevent text wrapping once the viewport border is reached?

Consider the following code snippet (feel free to click and drag outside the text area) - the text wraps when it reaches the viewport border. let isCaptureActive = false; let offset = { x: 0, y: 0 } const fooDOM = document.querySelector('#f ...

Dynamically transforming JSON data into an HTML table

I'm new to HTML and web development, and I'm trying to create a website where a server-side Python script returns JSON data that needs to be displayed in a table on the webpage. The structure of the JSON can vary each time, so the table must be d ...

Identifying bots with Python's Requests and BeautifulSoup in web scraping tasks

Attempting to scrape all the HTML elements from a page using requests & beautifulsoup. Utilizing ASIN (Amazon Standard Identification Number) to extract product details from a page. The code snippet is as shown below: from urllib.request import urlope ...

Customizing the select form arrow in Bootstrap 4: A step-by-step guide

I'm currently working on a form using Bootstrap 4 and I want to customize the style of the arrow select element. I'd like it to look like the second version shown below: https://i.sstatic.net/oUBBI.png I've tried various methods (reference ...

Storing Form Input in Browser's Local Memory

I am currently working on a form section where individuals can input their email addresses. However, I have encountered a couple of issues: (1) After submitting an email address, the page refreshes. While I understand that this is inevitable without usin ...

Next.js - Anticipated that the server HTML would include a corresponding <div> within <div> tag

For a live demonstration, please click here In my current project, I am experimenting with creating a simple layout that adjusts based on the user's screen size. Specifically, on mobile devices, only the latest posts should be displayed. On desktops, ...

Challenges of Python Web Scraping: Insufficient Extraction of HTML Code

As someone new to Python, I am currently experiencing difficulties with my web scraping code. The script is able to access the website and bypass cookies successfully. However, it is failing to capture the entire HTML code. Here is a snippet of the HTML c ...

Customizing package CSS styles in Angular

When working with Angular cli, it automatically loads css files from the node_module directory. I am currently using @swimlane/ngx-dnd, which comes with its own css styles. However, I would like to use bootstrap styles for my components instead. What is ...

Having trouble transferring a variable from getJSON data to PHP

I am encountering an issue with my jQuery getJSON method when trying to pass data to PHP. Strangely, I am unable to pass a variable. In another part of my code, passing the variable works fine. Additionally, hardcoding the string or using a valid variable ...

Unique CSS shadow design & alternative solutions

I'm currently working on some HTML/CSS code and I am looking to implement a unique shadow effect with a custom width and height around a div element. https://i.sstatic.net/3SnXV.png Could anyone provide guidance on the most effective method to ach ...

Tips for implementing click events on multiple links iteratively

I am currently working on an asp.net application, and I have encountered an issue with the website names displayed on the dashboard. These names are retrieved from a SQL database and rendered to the view using the following code snippet: <h1>My Webs ...