Using CSS to select multiple nth-child elements in a single rule is

If we take the following css code:

div div p:nth-child(1),
    div div p:nth-child(2),
    div div p:nth-child(3),
    div div p:nth-child(4),
    div div p:nth-child(5) {
    }
    

This will target elements with nth-child from 1 to 5.

Now, let's look at some shortcuts:

div div p:nth-child(n+1):nth-child(-n+5){
    }
    

OR

div div p:nth-child(-n+5){
    }
    

The question I have is,

How can we target not the first element from 1 to 5, but the second element from 2 to 5?

So, if we have this HTML structure:

<div>
        <div>
            <p></p>
            <p></p>
            <p></p>
        </div>
        <div>
            <p></p>
            <p></p>
            <p></p>
        </div>
        <div>
            <p></p>
            <p></p>
            <p></p>
        </div>
        <div>
            <p></p>
            <p></p>
            <p></p>
        </div>
        <div>
            <p></p>
            <p></p>
            <p></p>
        </div>
    </div>
    

I want to apply a background-color: red in CSS, only starting from the second div > p, and continuing until the 5th one.

Answer №1

Maybe a solution like this could be effective?

div div p:not(:first-child)

Discovered on Styling all elements except the first child

Answer №2

Does this meet your criteria? Specifically, where only the first element is excluded from selection?

div div:not(:first-child) p {
  background-color: blue;
}

Check out the Fiddle Here.

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

Error Received While Attempting to Log in using Ajax

Having an issue with logging in using ajax and php. I am able to log in successfully, but when trying to display an alert message and refresh the page upon login, it gives me an error without refreshing. However, upon manually refreshing the page, I can se ...

Error occurred: ajax resource could not be loaded due to connection refusal

When accessing the webapp/page from a browser on the same machine as the server running the express API, everything works fine. However, if trying to view the webapp from a different machine on the same network using the server's IP address and port, ...

Utilize Content Delivery Network Components in Conjunction with a Command Line Interface Build

As we progress in building our Vue applications, we have been using the standard script tag include method for Vue. However, as we transition from jQuery/Knockout-heavy apps to more complex ones, the maintenance issues that may arise if we don't switc ...

The map's content shifts with every scroll of the mouse, rather than being controlled by the

After creating a custom control in leaflet, I noticed that when interacting with it using scroll or click, the underlying map is affected instead of the control itself. <CustomMapControl> <div className="custom-c ...

Having trouble implementing a CSS style for a Kendo-checkbox within a Kendo-treeview component

I am facing a The issue I am encountering is that while the CSS for k-treeview is being applied from the scss file, it is not being applied for the kendo-checkbox I attempted to resolve the problem by using the following code: <kendo-treeview ...

Transform specific data binding values into JSON format using Knockout.js

Just dipping my toes into the world of knockoutjs, I've got this viewmodel set up: var Testing = function(){ this.Username = ko.observable(""); this.Password = ko.observable(""); this.email = ko.observable(""); } I'm tasked with ...

Lazy Highcharts and Foundation clash when loading JavaScript

I'm currently utilizing lazy_high_charts along with foundation 4. Within my view, I have included <%= high_chart("my_id", @chart) %>, which produces the following: <section> <script type="text/javascript"> (function() { ...

Analyzing the original data form versus the modified data

I'm dealing with a form in React that utilizes react-hook-form and the useFieldArray method for adding dynamic steps. The challenge I'm facing is how to compare the original data passed to the form with the edited data, in order to make correspon ...

Ways to retrieve the value of a specific key within an object nested within another object

Suppose I have a variable named x with the following structure: x = { choice1: { choice: { name: "choice1", text: "abc", key: "key1" } isChecked: true }, choice2: { choice: { name ...

Use the nobr HTML tag on Django form fields

I have been customizing Django form fields using this helpful guide. Here is the form structure: class contact(forms.Form): first_name = forms.CharField() middle_name = forms.CharField() last_name = forms.CharField() This is how the template ...

What if a website refuses to be embedded in an <IFrame>? Is there a workaround to still embed it?

I'm trying to include a 'www.youtube.com' video using the IFrame tag in HTML, but unfortunately it's not allowing me to embed it. Is there a workaround or different method I can use to achieve this? ...

CSS division nested within the center float division

In my attempt to design a 3-column home layout, I have successfully styled the center, left, and right sections. However, I am facing difficulty in placing two sliders within the middle class. My goal is to have slider1 positioned on top inside the middle ...

I am looking for a regular expression that will eliminate any leading white space while still permitting spaces between words

Below is the code I've created for an angularjs directive that removes spaces. It successfully eliminates spaces between words, but unfortunately still allows leading spaces. function customValidation() { return { require: 'ngModel&apos ...

Issue with stretched links in Bootstrap 5 specifically affecting certain recurring elements on mobile devices

I am facing an issue with a table that has a few hundred rows generated by a recurring PHP script. Utilizing Bootstrap 5, I am using a stretched link to enable the entire row of the table to be clickable. The functionality works perfectly on desktop and ev ...

Having trouble with Javascript Array Push and Splice not functioning properly?

My goal is to replace the value "3" with "4" in the array. After this operation, only "1":"2" will remain in the array. const array = []; array.push({ "1": "2" }) array.push({ "3": "4" }) const index = array.indexOf(3); if (index > -1) { array.sp ...

The JavaScript array created from parsing JSON returns a value of undefined

Once again, the gecko scenario! The JSON used in this script to fill a listbox has been validated by JSONLint. The code snippet below demonstrates how the parsed data is placed in arrays: pdata = jQuery.parseJSON(data); ctype = pdata[0]; stype = pdata[1]; ...

Insert a numerical value into a list to make a series of numbers whole

I currently have an array of objects that looks like this: var arr = [ { "code": "10", }, { "code": "14", } ] My goal is to expand this array to contain 5 elements. The numbers should ran ...

What does ngModel look like without the use of square brackets and parenthesis?

Can you explain the usage of ngModel without brackets and parentheses in Angular? <input name="name" ngModel> I am familiar with [ngModel] for one-way binding and [(ngModel)] for two-way binding, but I am unsure what it means when ngModel ...

Tips to minimize a responsive Bootstrap 4 menu by clicking elsewhere

When using my bootstrap nav menu on desktop, it closes when clicking outside of it. However, this feature doesn't work on mobile devices. I attempted to modify some code from a codepen to match the classes on my website, but it didn't work. Bel ...

Triggering HTML5 form validation after correctly setting a custom error message with setCustomValidity

I have implemented a custom error message using the setCustomValidity() function for every input with the type=number in my form to display translated error messages when an incorrect number is entered. However, I have encountered an issue where the erro ...