CSS complex selector: choose the sibling element after the first child within a div element

Help! I'm struggling to create a complex CSS3 selector. I want to target the second div within a child div of an item. Here is the HTML code for my test:

<!DOCTYPE html>
<html>

<head>
    <style>
        div.main > div:nth-child(3) {
            display: none;
        }
    </style>
</head>

<body>
    <div class="main">
        -> 1-level
        <div>
            -----> 2-level
            <div>--------> 3-level BAR</div>
            <div>--------> 3-level FOO</div>
        </div>
    </div>
</body>

</html>

I'm trying to select the 3rd level element with the text "FOO", but I can't add another class other than ".main". I've attempted to use 'div.main > div:nth-child(3)' without any luck. You can see my code in action on this JSFiddle link.

Answer №1

You may have misunderstood the functionality of the nth-child() selector. It is designed to target the nth-child of an element, not the "nth-level-child".

The correct way to write your selector would be:

.main > div > div:nth-child(2) {
    display: none;
}

.main > div > div:nth-child(2) {
  display: none;
}
<body>
  <div class="main">
    -> 1-level
    <div>
      -----> 2-level
      <div>--------> 3-level BAR</div>
      <div>--------> 3-level FOO</div>
    </div>
  </div>
</body>

Answer №2

Follow this example:

div.main > div > div:last-child {/*or try using div:nth-child(2)*/
            display: none;
        }

Answer №3

div:nth-child(3) represents the third child element of its parent. Here is what you are searching for:

//         vvv — this is 2nd level
//               vvv — this is 2nd level
//                            vvv FOO is a second sibling
div.main > div > div:nth-child(2)

Answer №4

Experiment with div:nth-of-type(x) selector

For example, refer to this demonstration: https://jsfiddle.net/L6xkanrm/1/

For instance: if you wish to target the SECOND div within another div, utilize:

div div:nth-of-type(2) {
        display: none;
}

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

Steps to set a default selection from a list of components in Angular 7

Currently, I am in the process of developing a messages page layout where users can select a message from a list on the left side and view its content on the right side, similar to how it is done in Outlook. Additionally, users should be able to reply or c ...

Which specific CSS attributes should be applied to create a scroll bar within this table?

Below is the table that I am working with: 'table' I want to ensure that its width remains the same even when the screen size is reduced, and only add a scroll bar to view it. I have already included overflow: scroll; in the container <div> ...

Two headings using the same tags appear distinctively rendered

Recently, we added an iframe to our company's website that I have been helping manage. This iframe is responsible for handling requests sent to our organization. Interestingly, the headings on our website are styled using the following CSS: color: # ...

Issue with Angular 9 Json pipe not showing decimal values

My JSON data looks like this: this.data = {"eoiStatistics": [ { "dateRange": { "explicitStartDate": "1997-01-01T00:00:00", "explicitEndDate": "2019-07-01T00:00:00" }, "outstandingApplicationCount": 0.0, "pendingApplicationCount": 24.0, " ...

Ways to navigate private property using the App Component in Angular 4

I have successfully implemented code in my app component to retrieve the current URL and display it in app.component.html. app.component.ts import { Component } from '@angular/core'; import { Router } from '@angular/router'; @Compone ...

Tips on choosing the initial N website elements that match a specific CSS selector using Python Selenium

Currently, I am utilizing phantomJS in a python/selenium configuration to scrape the screen. My main objective is to extract the first N elements that match a specified CSS selector. An issue I am encountering is that there are significantly more matching ...

Enforce the text inside the child div to perfectly fit within the parent container

I have a challenge with two div elements in my HTML code as shown below: <div id="parent"> <div id="child"> <p class="child-text"> ..... </p> <a class="child-link"> ..... </a> <p class="ch ...

How to visually deactivate a flat button ( <input type="button"> ) programmatically with JavaScript

I am facing an issue with my buttons. I have one regular button and another flat button created using input elements. After every click, I want to disable the buttons for 5 seconds. The disable function is working properly for the normal button, but for th ...

Enhance your dynamic php page with the use of a light box feature

Hey, I have created a PHP page that dynamically reads images from a folder and displays them on a gallery page. However, I am facing some issues - I am unable to link an external CSS file and I have to include all the CSS within the HTML. Additionally, I c ...

Selecting dropdown values using Kendo UI and Selenium

I'm currently using Selenium WebDriver to retrieve the set of option values such as Last 30 days, Last 60 days, etc. Initially, I attempted to fetch these elements using a CSS selector. var timeperiodcss = "div.k-animation-container div.k-list-conta ...

Move your cursor over one container to see the impact on another

My goal is to create a hover effect on a box that triggers another div with easing effects. Below, you'll see that the .images{} class has a 0s infinite scroll initially, but when the .box:hover > .images{} selector is activated, it changes to a 10 ...

Background image that is vertically responsive

I'm struggling with getting a full-page background image to maintain a consistent margin around all sides, especially at the bottom. I want the image to be vertically responsive using only CSS, without relying on Javascript. Any suggestions on how to ...

Annoying div unexpectedly wrapping content after using appendTo

Greetings, esteemed members of the SO community, I am here once again with a question that requires your assistance. Context: I am currently working on a grid system that allows users to drag and drop items onto specific grid fields. When the grid is re ...

Modify the appearance of the elements dynamically by applying styles from the CSS file without relying on the

In my CSS file, I have the following styles: .myClass { ... } .myClass:focus { ... } Am I able to modify these styles from my code? For instance, can I change the focused style of an input element when a button is pressed? ...

Images that adapt to different screen sizes

I'm not seeking a revolutionary way to handle the images, as I have already consumed a lot of information about loading images. My focus is on basic responsive design, where I aim to load a large image and compress it as the window size reduces. Bel ...

Is it possible to exclusively focus on the specified data attributes?

Is there a method to specifically target the 'data-season' attribute with the value "winter"? This is my HTML: <li data-season="summer">summer <ul class="groups"> <li data-item="beach">beach</li> <li data-item ...

What is preventing me from adjusting the padding of the mat-button?

Trying to adjust the default padding of a mat-button, but encountering issues with changing the component's style. Any suggestions on how to subscribe to the default padding (16px)? I've attempted modifying CSS properties to set the padding of a ...

Issue with nested tabs functionality within a pill tab panel in Bootstrap not functioning as expected

I'm facing a challenge with integrating tabs into a vertical pill tab panel in Bootstrap 4. Below is a brief code snippet: <html> <head> <link href="bootstrap.min.css" rel="stylesheet"> </head> <body> <script ...

Refreshing backdrop for navigating through lists - Sprite edition

I am facing some issues while trying to use a sprite image for my navigation icons. The background position offset is not working as expected, and the background image is stretching across the whole length of the navigation. It seems like I may need to spe ...

I'm looking to center my btn button vertically and horizontally over two images using Bootstrap 5 in a responsive manner. How can I achieve this?

Any tips on how to position my btn button both vertically and horizontally in the center on top of two images? I want it to look like this: https://i.sstatic.net/Rezrd.png This is the code for my button: <a class="btn btn-primary" href=" ...