css style the rows in a multi-level unordered list to alternate colors

My Vision:

I have a vision of a table with alternating row colors, but I need to display the data in a tree structure with multiple levels. Despite this requirement, I still want it to maintain the appearance of a table with alternating row colors. Essentially, I want the best of both worlds.

https://i.sstatic.net/7OAWW.png

Current Progress

https://i.sstatic.net/462ep.png

The challenge I'm facing is that each level has its own ul/li tags, making it complicated to use the nth-child selector for styling. The alternating row colors for each level clash with one another, resulting in a visually unpleasing design.

Current CSS:

li:nth-child(odd)>div {
  background-color: lightblue;
}

Seeking CSS Solutions

To tackle this issue, I experimented with a more complex tree structure for testing purposes.

See the JSFiddle here:

https://jsfiddle.net/yshk8dbg/

Answer №1

To enhance the precision of your css, you can simply assign an id to the root ul:

#tree > li:nth-child(2n + 1) > div {
  background-color: lightblue;
}
<ul id="tree">
  <li>
    <div>AAAA</div>
    <ul>
      <li>
        <div>Node 1</div>
      </li>
      <li>
        <div>Node 3</div>
      </li>
      <li>
        <div>Node 3</div>
      </li>
      <li>
        <div>Node 4</div>
      </li>
    </ul>
  </li>
  <li>
    <div>BBBB</div>
  </li>
  <li>
    <div>CCCC</div>
    <ul>
      <li>
        <div>Node 1</div>
      </li>
      <li>
        <div>Node 3</div>
      </li>
    </ul>
  </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

Laravel application faces issues with URL rewriting generating incorrect URLs in compiled CSS files

Having some trouble compiling Font Awesome SCSS files in my Laravel application. I installed Font Awesome using NPM, and the compiled CSS is stored in the 'public/css/' folder while a 'public/fonts/' folder was also created. However, th ...

In HTML, favoring the use of "//domain.com" over "https://domain.com" or "http://domain.com" is recommended for greater flexibility and compatibility

Related Questions: Changing all links to // Network-Path Reference URI / Scheme relative URLs I have noticed some websites using the following format: background:url(//cdn.domain.com/images/bg-normal.png) They utilize "//", which the browser aut ...

Is there a way to use a Google Chrome command line switch to simulate the dimensions of a

Seeking information on the available Google Chrome command line switches for emulating device sizes. Specifically, I need to test a component that utilizes CSS @media queries for min-device-width/min-device-height. Previous attempts with --window-size an ...

Utilize SVG images repeatedly while still retaining control over CSS styling

I have a collection of silhouette images that I need to change color for various purposes, such as displaying in different locations or using hover effects. While it's fairly straightforward to achieve this by directly embedding the SVG into the HTML ...

What is the best way to convert this JavaScript iteration function into jQuery?

I recently encountered an issue with my JavaScript function that returns a list of elements with the class ".youtube", loops through them, and calls another function. The JavaScript logic is flawless, but I wanted to convert it into jQuery for better reada ...

The navigation bar is missing from the screen

Where is the navbar in this snippet? Did I overlook something important? If so, what? <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> <div class="container"> <div class="navbar nav ...

A web address shared in a post appears within the nested frame of an iframe

I'm encountering a somewhat confusing issue. I am attempting to submit a URL to an iframe located on the same page. Upon clicking submit, the iframe replicates the current page, but with the correct URL displayed in the duplicated iframe. It's ma ...

What is the best way to convert minutes into both hours and seconds using javascript?

In order to achieve this functionality, I am trying to implement a pop-up text box where the user can choose either h for hours or s for seconds. Once they make their selection, another pop-up will display the answer. However, I am facing issues with gett ...

Displaying a hand cursor on a bar chart when hovered over in c3.js

When using pie charts in c3js, a hand cursor (pointer) is displayed by default when hovering over a pie slice. I am looking to achieve the same behavior for each bar in a bar chart. How can this be done? I attempted the CSS below, but it resulted in the h ...

Duplicate an HTML element within an iframe and display it

Attempting to print a portion of my HTML within an iframe using VueJs, I wrote the following code: Print:function(){ var element = document.getElementById('content'); printf.document.body.innerHTML = element; window.frames["printf"].focus(); w ...

Detecting the preferential usage of -webkit-calc instead of calc through JavaScript feature detection

While it's commonly advised to use feature detection over browser detection in JavaScript, sometimes specific scenarios call for the latter. An example of this can be seen with jQuery 1.9's removal of $.browser. Despite the general recommendatio ...

A guide to updating the image src with a click using CSS exclusively, without JavaScript intervention

I am looking to update the image source when clicking on it using only CSS, something along the lines of: img:active { } ...

Creating a set in AngularJS JSON data structure can be easily achieved by using the unique

Here is a JSON structure: { "list": [{ "name": "1", "type": ["A", "B"], }, { "name": "2", "type": ["A", "D", "C"], }, { "name": "3", "type": ["D", ...

Styling text within a bootstrap button using CSS

I am currently using Bootstrap and have an accordion with collapsible buttons. Each button consists of two columns of text and an icon on the right. My issue is that I'm attempting to break the text in the second column into multiple lines so that it ...

Issues with HTML warnings not functioning properly in React Multi Step Forms

In the process of creating a multistep form using React, I have been following the guidance provided by CSS Tricks and Brad Traversy's video tutorials along with his code on GitHub. While everything seems to be functioning correctly, I am encounterin ...

Adjust the height of images to be consistent

I'm currently working on creating a grid layout with 4 images in a row using DaisyUI card component. However, I've run into an issue where there is white space at the bottom of some images due to varying image heights. Is there a solution that wo ...

What is the best way to display table headers for each record on mobile devices? Is there a way to create a stacked view

I recently started working with bootstrap, specifically version 3. I am currently trying to find a way to create a layout that resembles a regular table when viewed on desktop but switches to a stacked format on mobile devices. This is the design I am aim ...

Customize the appearance of a React component depending on its unique identifier

After creating a simple TODO app with drag and drop functionality, I am now looking to apply a line-through CSS style to any task added or dragged into the second column of my app. What is the best way to target these tasks using their unique ID: <div c ...

PHPMail is failing to properly format emails in HTML

Why is it that when I attempt to send an HTML email from a PHP script, Outlook displays it the same way as it appears in my database? Currently, my tool sends an email with the content stored in the $body variable like this: <p>Dear client,</p> ...

The div does not allow elements to be centered

I am facing an issue where elements are not centering on my div. I have tried numerous solutions (so many that I finally decided to create a Stack Overflow account), but nothing seems to work. Interestingly, the elements center on PC but for some reason, ...