Removing the rows that do not contain a table - nested div elements within other div elements

I am trying to create stripped rows with "complex" children, but I'm having trouble figuring out how to do it. I am using bootstrap, although I'm not sure if that makes any difference!

This is what I have attempted so far: https://jsfiddle.net/1w9tv4ce/2/ My goal is to make all test-01 green, followed by test-02 black, then test-03 green, and finally test-04 black.

.rows-search div:nth-child(even) {
  background-color: #95CA44;
}

.rows-search div:nth-child(odd) {
  background-color: black;
}

.search-light-grey {
  color: darkgray;
}
<div class="container search-light-grey">
  <div class="rows-search">
    <div id="1">
      test-01
      <div>
        1
      </div>
      <div>
        <div>
          2a
        </div>
        <div>
          2b
        </div>
        <div>
          2c
        </div>
      </div>
    </div>
    <div id="2">test-02</div>
    <div id="3">test-03</div>
    <div id="4">test-04</div>
  </div>
</div>

Thank you for your help!

Answer №1

Employ the child combinator (>) within your selectors (this will exclusively target immediate child elements) - view the demonstration provided below:

.rows-search > div:nth-child(even) { /* <-- modified */
  background-color: #95CA44;
}

.rows-search > div:nth-child(odd) { /* <-- modified */
  background-color: black;
}

.search-light-grey {
  color: darkgray;
}
<div class="container search-light-grey">
  <div class="rows-search">
    <div id="1">
      test-01
      <div>
        1
      </div>
      <div>
        <div>
          2a
        </div>
        <div>
          2b
        </div>
        <div>
          2c
        </div>
      </div>
    </div>
    <div id="2">test-02</div>
    <div id="3">test-03</div>
    <div id="4">test-04</div>
  </div>
</div>

Answer №2

To follow up on my previous comment, make use of the > selector which targets direct children only.

https://developer.mozilla.org/en-US/docs/Web/CSS/Child_combinator

.rows-search > div:nth-child(even) {
  background-color: #95CA44;
}

.rows-search > div:nth-child(odd) {
  background-color: black;
}

.search-light-grey {
  color: darkgray;
}
<div class="container search-light-grey">
  <div class="rows-search">
    <div id="1">
      test-01
      <div>
        1
      </div>
      <div>
        <div>
          2a
        </div>
        <div>
          2b
        </div>
        <div>
          2c
        </div>
      </div>
    </div>
    <div id="2">test-02</div>
    <div id="3">test-03</div>
    <div id="4">test-04</div>
  </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

The pagination styles in Bootstrap are refusing to be overwritten by custom CSS variables

Incorporating Bootstrap version 5.3.3, I have implemented a customized WordPress theme where I have specified a range of color overrides. The custom WordPress theme stylesheet is loaded last, following the Bootstrap stylesheet. At the beginning of my the ...

Creating web applications using Eclipse IDE and HTML5

Seeking a reliable IDE for creating HTML5 applications, I've heard Eclipse is a good option, which I am already familiar with in my coding projects. Currently using Eclipse Helios Release, wondering if I should upgrade to Eclipse Helios Service Relea ...

Angular 4 provides the capability to reset a radio group that has been dynamically generated within a table

I am facing an issue with a dynamically created radio group inside a table in my component. I am using ngFor to generate the radio buttons, and there is a clear button outside the ngFor that should reset the radio group when clicked. However, my current im ...

Incorporating Material-UI themes within styled components

Trying to incorporate theme spacing value in a styled component but facing issues. Initially, the style was applied to the button, but now it is not working as expected. You can view the problem here: sandbox. import React from "react"; import ...

"Showcase a Pair of Images with Just a Single Click. Leveraging PHP and

With two div boxes, one for Input and the other for Output as displayed below: My goal is to upload an image using PHP and store it in both the Input and Output folders. Upon clicking the Submit button, I want the image from the Input folder to be shown ...

Designing table rows to resemble full-width cards

Looking for a way to transform the rows of a table into full-width cards while maintaining the row layout? Here's what I have tried so far: tbody>tr { display: block; padding: 1rem 0.5rem 1rem 0.5rem; margin: 1.5rem; border: 1px solid rgba( ...

Please submit the form to log in with your credentials

Here is the HTML code snippet for a form where users can enter their username and password to log in: <form Name ="form1" Method ="POST" ACTION = "userlogin.php" id="form1"> <div id="main_body" class="full-width"> <label>User ...

Striving to make edits that will reflect changes in the database, however, I continue to encounter error CS0123 stating that there is no matching overload for 'Insert_Click' with the delegate 'EventHandler'

Welcome to the front-end of my webpage: <div class="div"> <asp:Button style="background-color: #007aff; width: 100%; padding: 5px" Text="SAVE" OnClick="Insert_Click" ID="InsertID" runat="s ...

Necessary CSS selector input equivalent in Reactive Forms

Is there a similar CSS method like input:required {CSS} to style all the reactive form fields in Angular that are set as required using Validators.required in their formControl? Or is there another approach to achieve this with CSS? ...

How can I expand the notification pop-up in R Shiny?

I'm trying to figure out how to expand the notifications in R Shiny because right now they are cutting off longer error messages. Any suggestions? library(shiny) ui <- fluidPage( actionButton("test", "Test") ) server <- f ...

Creating a split screen layout using Bootstrap

I've been working hard to create a split-screen layout using Bootstrap. After using CSS to achieve it, I realized that it's not responsive for mobile users. Therefore, I'm reworking it with Bootstrap but facing issues with resizing the image ...

Ways to activate the built-in HTML5 form validation without actually submitting the form

I have a form in my application that I plan to submit using AJAX. However, I would like to implement HTML5 for client-side validation. Is it possible to trigger form validation without actually submitting the form? Here is an example of the HTML code: &l ...

How to retrieve and delete a row based on the search criteria in an HTML table using jQuery

Here is the HTML code snippet: <table class="table" id="myTable"> <tr> <th>Type</th> <th>Counter</th> <th>Remove</th> <th style="display:none;">TypeDistribution</t ...

Ways to resize column widths in tree views on Odoo version 10?

I have been struggling to adjust the column width of the columns in my tree view. I have attempted a few different solutions: Trying to change the width attribute in the field tag: width="100px" or width="15%%" Using the style att ...

Can you define the "tab location" in an HTML document using React?

Consider this component I have: https://i.stack.imgur.com/rAeHZ.png React.createClass({ getInitialState: function() { return {pg: 0}; }, nextPage: function(){ this.setState({ pg: this.state.pg+1} ) }, rend ...

By default, use jQuery to load the content of a div

Upon page load, I am looking to display the content within #destiny2. This section includes text and images, as well as additional content for three categories. Initially, the first category should be shown without requiring a click. Subsequently, clicking ...

Using Four Different Colour Borders in CSS

Is it possible to create a CSS border with 4 different colors on one side? Currently, I have the following: #header { border-color:#88a9eb; } I aim to achieve a border featuring four solid colors split equally at 25% each. Can this be done? I am looking ...

How can I add rows to the tbody of a table that is already inside a div container?

I have an existing table and I want to append a tbody element to it. Below is an example of the HTML table: <div id="myDiv"> <table class="myTable"> <thead> <tr> <th>ID</th> ...

Enhancing Plotly Graphs with Custom Node Values

Encountering an issue while attempting to assign values to nodes on a plotly graph. Code: <html> <head> <script src="https://cdn.plot.ly/plotly-latest.min.js"></script> <style> .graph-container { ...

Issue with border in a nested table within an HTML table

I am faced with an issue in styling nested tables. My goal is to have borders on both tables, but I specifically need only the bottom border of the inner table without the top, right, and left borders. The default border style can be achieved using the fol ...