Choose every HTML element of a specific type that appears following a designated element

Is there a way to target all p elements that follow h1, specifically those that appear later in the DOM structure? These p elements may be at the same level as h1 or nested multiple levels deep, rendering selectors like ~ or + ineffective...

In this scenario, the goal is to give 'Paragraph 3', 'Paragraph 4' and 'Paragraph 5' a red background.

I'm seeking solutions using pure CSS selectors or via Nokogiri. Thank you.

<!DOCTYPE html>
<html>
<head>

<style> 
  h1 ~ p {
    background: red;
  }

</style>
</head>
<body>
  <p>Paragraph 1</p>
  <p>Paragraph 2</p>
  <h1>Foo</h1>
  <p>Paragraph 3</p>
  <div>
    <p>Paragraph 4</p>
  </div>
  <span>
    <div>
      <p>Paragraph 5</p>
    <div>
  </span>
</body>
</html>

Answer №1

Identify and style all paragraphs that are descendants of the following elements

h1 ~ p,
h1 ~ * p {
  background: red;
}
<p>Paragraph 1</p>
<p>Paragraph 2</p>
<h1>Foo</h1>
<p>Paragraph 3</p>
<div>
  <p>Paragraph 4</p>
</div>
<span>
    <div>
      <p>Paragraph 5</p>
    <div>
  </div>

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

Python code to retrieve historical data in CSV format from a website that mandates authentication

Despite my thorough online search, I have been unable to uncover a solution to my dilemma. Specifically, I am interested in downloading daily csv files of historical data from a sunnyportal website that requires login. The login page can be found here: ht ...

The password does not conform to the pattern specified in Angular CLI

I am encountering an issue with password validation. I have implemented a regex pattern like this: '(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])' and configured my ng-form field as follows: password: ['', [Validators.pattern('(?=.*[a-z])(? ...

Modify the internal class style to set overflow property to visible for a particular class in PrimeNG

Looking for a way to customize the styles of PrimeNG or Angular2 components? The documentation may be lacking clarity, but you can find more information at this URL: http://www.primefaces.org/primeng/#/dialog So, how do you actually go about changing the ...

Coordinating several navigation tabs and navigation bars in Bootstrap 4 to work together seamlessly

I am in the process of developing a tool that will assist my colleagues in reviewing and comparing information. I aim to present this information consistently for each order, with multiple groups of information per order accessible through nav-tabs. I woul ...

Experiencing Challenges with JavaScript Implementation Within an HTML Document

Code: <!DOCTYPE html> <head> <script type="text/javascript" src="jquery-3.3.1.js"> </script> <script type="text/javascript"> $(function() { alert("Hello World"); }); </script> <meta charset ...

Testing the functionality and performance of JavaScript and HTML across numerous webpages using automated tools

Operating a webstore means offering several products to our customers. The checkout process is divided into four separate pages where users must enter their address, select a delivery method, and confirm their order across various URLs. Communication with ...

Using justify-content-between in a div container with only two items will not produce the desired effect

I'm having trouble aligning two elements on opposite ends of a div container using Bootstrap's justify-content-between class. The h4 element is not on the left and the button on the right as expected. I am using Bootstrap 5.2.3. Can anyone help m ...

Validating forms with pure JavaScript

Hello, I am currently working on my very first contact form using HTML, Bootstrap, and JavaScript. While I have managed to prevent the form from submission when any input is missing, I am facing an issue with allowing the form submission once all inputs ...

The iconic image of Captain America rendered in high-quality SVG

Is there a way to create this star shape using SVG? I have tried using points but it's not working for me. I cannot use the path element. Any suggestions would be greatly appreciated. Thank you. <!DOCTYPE html> <html> <body> ...

Implementing reCAPTCHA and PHP for Email Form Submission

Currently using GoDaddy as the web host for my website. Struggling to integrate reCAPTCHA into the email form on my site. I successfully pass the reCAPTCHA test, but emails are not being sent. So far, the website has been built using PHP and HTML only. ...

What is the process by which the browser displays pseudo-element selectors?

One thing that's been on my mind is the potential resource cost of using *:after. Would the pseudo element be the primary selector, or would all elements still be processed by the client? In simpler terms, what impact does *:after have compared to ju ...

Emails are not responsive to media queries

I've experimented with multiple media queries in an attempt to make this work on both iPhone (landscape/portrait) and desktop, but I'm struggling to achieve the desired outcome on both simultaneously. While some of my classes are functioning cor ...

Attempting to incorporate Font-Awesome Icons into the navigation bar tabs

As a newcomer to React, I've been attempting to incorporate Font Awesome icons into my secondary navigation bar. Despite using switch-case statements to iterate through each element, all the icons ended up looking the same, indicating that only the de ...

Need help setting up automatic audio playback on your Android device?

I'm aware that autoplay of audio is not supported on Android devices. However, I recently found a website that successfully autoplays music on an Android device: Can someone explain how this is being achieved? ...

How to remove the parent.parent.parent element when a button is clicked in Angular without utilizing the element's ID

I am currently developing a website where users should have the ability to delete li-elements with just one click. These li elements are contained within bootstrap containers, which means that when deleting, I want to remove the entire container along with ...

My Perl script is displaying as a text file during testing and is not executing

As a novice web programmer, I recently created an HTML form that is supposed to run a script. However, when I try to test it by opening the file on my computer (not on a web server), the script does not execute. Instead, it simply displays as a text file ...

How to override the styling of a parent element in CSS

I'm encountering an issue with my website's sidebar. I've set the background color to yellow for elements with the currentPage class. This works fine for the 'Home' tab, but when I try to do the same for a nested tab like 'tag ...

Centering items in Bootstrap 4, while excluding the first element

.c-footer { background: #000; padding: 20px 0; color: #fff; } .c-footer__logo { display: inline; } .c-footer__link { color: inherit; } .c-footer__link a { color: inherit; } .c-footer__link a:hover { color: #fff; } ...

Retrieve information stored in a JSON data field within the results of an npm

How can I properly access the value of DepDateTime from the first object? Here is my current attempt: const nodeSkanetrafiken = require('node-skanetrafiken'); const from = { name: 'Bjärred centrum', id: 62025, type: 0 }; const to = ...

The Vuetify rating system seems to be having trouble displaying

I've integrated the Vuetify rating component into my app (https://vuetifyjs.com/en/components/ratings#ratings), but I'm facing an issue. Despite having Vuetify 1.5.5 installed and working with other components like buttons, the stars in the ratin ...