Choose the child element that comes after a specific class

Currently, I am dealing with a ul element list and my goal is to select the first 3 li elements that come after the .active class.

<ul>
 <li>The #1</li>
 <li>The #2</li>
 <li class="active">The #3</li>
 <li>The #4</li>
 <li>The #5</li>
</ul>

I have tried using the following CSS selector:

ul li.active ~ li:nth-child(n+3)

Unfortunately, this approach did not work as expected. Can anyone provide me with some guidance on how to achieve this?

Additional Note: The .active class dynamically changes, so simply using ul li:nth-child(n+3) will not suffice.

Answer №1

It seems that utilizing the nth selectors might not be the best approach based on the structure of your markup and how the hierarchy is set up.

If you want to add a red background to the three li elements following the li.active element, you can achieve this by targeting them with a single CSS rule by adding + li multiple times in succession and grouping the selectors together, like this:

li.active + li, 
li.active + li + li, 
li.active + li + li + li {
  color: red;
}
<ul>
 <li>The #1</li>
 <li>The #2</li>
 <li class="active">The #3</li>
 <li>The #4</li>
 <li>The #5</li>
</ul>

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

Is there a way to extract a particular value from a JSON URL and transfer it to a JavaScript variable?

I am looking to extract current exchange rates from a JSON URL for implementation in a webpage. Specifically, I want to retrieve a particular exchange rate (such as the US Dollar) and store it in a variable for use within a JavaScript function. Below is a ...

Getting hold of HTML elements in a div on a different page

Recently diving into the world of html/javascript, I find myself engaged in a project where I'm loading an external html page within a div. The loaded content looks like this: <div class="content" id="content"> <object ty ...

Is there a way to create a dropdown menu that stretches across the entire page width when the burger menu is hovered over?

Is there a way to create a full-width dropdown menu that appears when the burger icon is clicked, while still maintaining the close function? (The dropdown menu and burger icon should transition smoothly when clicked) Here are the JavaScript functions for ...

Customizing the colors of the Input field in the Material UI Text Field component with CSS styling can elevate the overall

import { TextField } from "@mui/material"; import { FunctionComponent } from "react"; interface IProps { type: string; label: string; color: | "error" | "primary" | "secondary" | &quo ...

Displaying negative values highlighted in red on Datatables platform

After successfully integrating the datatables on my VF page, I now have one final requirement: to display any negative values in red and bold within numerical columns. In my Salesforce implementation, I am using <apex:datatable> for my table. Each nu ...

Ways to display a GIF image as a pop-up during data loading in an MVC application

I am working on a project using the MVC framework. The application retrieves a large amount of data during loading. I would like to display a loading image - a .gif file while fetching records. Below is the code snippet I am currently using: //Loads rec ...

Retrieve nodes that are children of another node

Here is the code snippet that I am working with: <div id="page1-div" style="position:relative;width:1347px;height:1189px;"> <img width="1347" height="1189" src="target001.png" alt="bac ...

When attempting to click the submit button, the action of submitting a form using jQuery/AJAX is not functioning as expected

When attempting to submit a form using jquery/AJAX, my function does not get called when clicking on the submit button. The structure of my website is as follows: CarMenu.php <html lang="en"> <html> <head> <meta charset="ISO-8859- ...

Modify the height of one or more <span> elements within a table cell

Is it possible in this scenario to automatically adjust the row height for <span class="orange">ORANGE</span>? And if there are two or more <span> elements, can the cell be split to accommodate varying heights? (You can use: display: tab ...

Why does Chrome DevTools evaluate JavaScript before recalculating styles?

According to an article, CSS is considered render-blocking, so JavaScript will evaluate after constructing a CSSOM (also known as recalculating styles in dev tools). However, it appears in Chrome dev tools that JavaScript is evaluated before CSSOM. Why is ...

Does border-padding actually exist in web design?

Looking for some assistance with styling two SVG icons. I've applied padding and now want to round the edges using border-radius, but it seems that no matter what padding amount I use, the SVGs get cut off. Here's an example of the issue with 10 ...

Retrieve the labels associated with highlighted text in a JTextPane

Hey everyone! I'm currently working with a JTextPane that has content type 'text/html'. I have loaded the Bible into this JTextPane where verses are separated by tags. What I want to achieve is determining the selected verse when a user clic ...

Revising text for real-time editing

Most of us are familiar with the functionality on StackOverFlow that allows you to preview your text before posting a question. I'm interested in implementing a similar feature on my website and am looking for some guidance on how to get started. I ...

WordPress website displaying two identical logos

Despite not making any core changes, this morning while performing some W3 validation, the logo inexplicably began duplicating at random and I am unable to locate the error. You can view the issue here: I have reverted the header.php file back to its ori ...

Using jQuery to toggle visibility on click within WordPress

Looking for a way to make three buttons change color on hover and display different content when clicked? You're not alone! Despite searching through tutorials and forums, the solution remains elusive. The buttons are structured like this: <div i ...

How to Customize a Horizontal Rule for Internet Explorer

Greetings, as I strive to create an image light website, I am interested in developing two-tone hr elements. I have successfully implemented this feature in modern browsers, but now I aim to achieve the same effect in ie6 and 7. The current code that I a ...

Struggles encountered when choosing the initial visible item

I have a set of 3 tabs, each with its own heading and content. However, I only want to display the tabs that the user selects by checking the corresponding checkboxes. There are 3 checkboxes, one for each tab. Below is the code snippet: //Function to ...

Positioning text at the bottom of a label

My goal is to align the text at the bottom of labels with a fixed height of 3em. This means that labels with one line of text should have the text aligned at the bottom, while labels with two lines should have the second line appear at the bottom. In my l ...

Troubleshooting: Node.js with Express - Issue with Loading Static JavaScript File in HTML

Despite trying numerous tutorials and following recommendations from various sources, my issue remains unresolved. I am attempting to create a basic static website using node.js and express to leverage server-side functionalities like file system access, d ...

Displaying a JSON array response on an HTML page with JavaScript and jQuery

After receiving a JSON response with data in array format, I need to display that data on an HTML page. In my HTML structure, I have div elements for merchant name, product name, and a remove button. The JSON data consists of arrays containing the same pro ...