HTML list style with varying images at the same level in a ul tag

I am working on an Application that generates output with li tags. I am trying to find a way to apply different list-style-image for each li tag of the same level within the ul, while keeping the HTML code as simple as possible. Each li with a class of green, yellow, or error should have its own unique image instead of the default bullet point. I have learned that in CSS, "list-style-image" needs to be applied to the style of the ul, but it doesn't seem to work when applied to the li directly.

<body>
  <div>     
    <ul>
      <li class="green">Lorem ipsum dolor sit amet.</li>
      <li class="yellow">Lorem ipsum dolor sit amet.</li>
      <li class="error">Lorem ipsum dolor sit amet.</li>
      <ul>
        <li class="error">Lorem ipsum dolor sit amet.</li>
          <ul>
            <li class="error"><a href="test.html">Lorem ipsum dolor sit amet.</a></li>
          </ul> 
      </ul>
      <li class="yellow">Lorem ipsum dolor sit amet.</li>
      <li class="green">Lorem ipsum dolor sit amet.</li>
      <li class="green">Lorem ipsum dolor sit amet.</li>
      <li class="error">Lorem ipsum dolor sit amet.</li>
      <ul>
        <li class="error">Lorem ipsum dolor sit amet.</li>
          <ul>
            <li class="error"><a href="test.html">Lorem ipsum dolor sit amet.</a></li>
            <li class="error"><a href="test.html">Lorem ipsum dolor sit amet.</a></li>
            <li class="error"><a href="test.html">Lorem ipsum dolor sit amet.</a></li>
          </ul>              
      </ul>            
    </ul>
  </div>

Css

.error {
  color: white;
  font-weight: bold;
  background: #FF6666;
  list-style-type: none;
  border-left: 15px solid #E82C0C;
  margin-bottom: 1px;
  padding: 5px;

}
.green {
  background: #CCFFCC;
  list-style-type: none;
  border-left: 15px solid #7FFF7F;
  margin-bottom: 1px;
  padding: 5px;
}
.yellow {
  background: #FFFF66;
  list-style-type: none;
  border-left: 15px solid #FFDE59;
  margin-bottom: 1px;
  padding: 5px;
}

div {
  padding: 10px 10px;
  margin-bottom: 20px;
  min-width: 768px;
  max-width: 1024px;
  overflow: hidden;
}

Visit this link for a demonstration. Thanks!

Answer №1

Your inquiry is somewhat confusing, but are you looking for this solution? I have applied it only to the .error class, which will display different images for each level of .error elements.

Simply add class="container" to your initial div (to establish a reference position) and include the following code in your CSS:

.container > ul > .error {
    list-style-image: url(http://placehold.it/50x30/ff0000);
}
.container > ul > ul > .error {
    list-style-image: url(http://placehold.it/50x30/cc0000);
}
.container > ul > ul > ul > .error {
    list-style-image: url(http://placehold.it/50x30/aa0000);
}

For a live demonstration, check out this example: http://jsbin.com/lavorepo/1/edit

Answer №2

It seems like you may be seeking a solution similar to the following: Utilize CSS specificity to target the desired element accurately.

.error {
    color: white;
    font-weight:bold;
    background:#FF6666;
      list-style-image: url(http://thekerrcompany.com/wp-content/uploads/2014/05/bulletpoint.png);
    border-left: 15px solid #E82C0C;
    margin-bottom:1px;
    padding: 5px;
}
ul ul .error{
    color: white;
    font-weight:bold;
    background:#FF6666;
    list-style-image: url(http://images1.wikia.nocookie.net/isleoftune/images/1/1b/Bullet_Point.png);
    border-left: 15px solid #E82C0C;
    margin-bottom:1px;
    padding: 5px;
}

Alternatively, consider utilizing the existing classes (.yellow, etc.) to modify the list images.

DEMO http://jsbin.com/jilolofi/1/edit

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

Styling each list item individually, without interdependence

I am working on a ul that contains several list items comprised of h3s and lis with the "close" class. When I hover, I want to expand the letter spacing on the h3s, but the items with the close class also expand. I have tried using nth child selectors in ...

Is it possible to execute a PHP script within a .css file and ensure it functions correctly?

Recently, I've been making some changes to the .css file in order to get my PHP codes to work. However, when I insert them as usual, they appear as regular text. I'm curious to find out if it's possible to execute a PHP file within a .css fi ...

What methods can be used to create a universal JS function for popup windows?

How can I create a more flexible code structure? I have successfully implemented the functionality using the following approach: let popup1 = document.getElementById("popup-1"); function openPopup1() { popup1.classList.add("open-popup& ...

Switch the Ionic Slide Box to the "does-continue" setting

Currently in the process of constructing a slide-box containing numerous items. I've borrowed the code from the example provided here for an infinite number of items, and it's performing well. However, I have a considerable yet finite amount of ...

Position the previous and next buttons next to the thumbnail images

I've implemented the cycle2 jQuery plugin on my website successfully, but I'm facing an issue with positioning the prev and next buttons next to my thumbnails. I want them to be aligned with the first and last thumbnail without relying on absolut ...

Embed schema information into HTML tags using JavaScript

Is there a way to insert text, specifically schema data, into an HTML div tag using JavaScript? While I know how to modify existing values within a tag like class, href, and title, I am struggling to find a method to add something entirely new. Essentiall ...

Is it possible to upload files without using AJAX and instead through synchronous drag-and-drop functionality in the foreground?

Currently, my website has a standard <input type="file"> file upload feature that sends data to the backend upon form submission. I am looking to enhance the functionality of the form by allowing users to drag and drop files from anywhere within the ...

Exploring ways to repeatedly collapse rows using HTML, CSS, and JavaScript

GOAL: I want to freeze the header, freeze the first column, and be able to collapse rows multiple times. CURRENT PROGRESS: I have achieved freezing the header, first column, but can only collapse rows once. MY CODE SNIPPET: </head> <body> &l ...

What is the optimal event to trigger a function when there is any modification in a text area using Javascript?

I need a function to trigger every time there is any modification in my textarea, such as characters being typed, deleted, cut, pasted, etc. Currently, I am using: onkeyup || onmousemove = function(); It appears that only onmousemove is being triggered. ...

Is the Mini Cart button in Woocommerce not aligned properly?

My website was functioning perfectly until yesterday. However, after updating some plugins, the Mini Cart dropdown button started to display in a weird and misaligned manner. https://i.sstatic.net/SsZee.jpg Prior to the plugin updates, the button had the ...

JavaScript fails to function properly on FireFox

I'm currently troubleshooting a script that works in Chrome but not in FireFox. I suspect it's due to the webkit syntax, so I tried converting it to a standard gradient without success. Can you help me identify what's causing the issue? Web ...

Reaching out to the Edge: Enhancing the jQuery Slider Experience

Alright, I'm really excited about using this amazing slider. What I love most is the "free mode" feature that creates this stunning sliding effect. The size and number of slides are absolutely perfect for me. But there's just one small adjustment ...

Accidental delay upon mouse exit

Upon hovering over, the transition immediately begins, but there is a delay of about 300ms before the reverse transition starts upon hovering off. (https://example.com) <div id="isa-hover-gallery"> <a href="https://example-link.com"> <div ...

Tips for customizing the CSS styling of the validation ng-class error

Seeking advice: Is there a way to customize the CSS style of the 'error' validation error for ng-class in Bootstrap v3? This is my current code: ng-class="(form.datos.$invalid) && ( form.datos.$touched || submitted) ? 'error&apos ...

Troubleshooting Flexbox: Content within a Flex Item is not displaying properly within a scrollable container

I recently embarked on converting one of my websites to flexbox and encountered an issue with vertical scrolling within a flex container. It appears that Firefox displays it correctly, while Chrome and Safari do not. You can check out the code here. . ...

Tips on ensuring a <video> element occupies the full height and width of the screen

I am currently working on an Angular 6 project where I am live streaming a user's webcam to an HTML element. My goal is to simulate the experience of capturing a video or photo on a mobile device. However, the size of the video is currently small. I ...

`Resolving CORS error when setting up an AWS Lambda function with API Gateway on AWS`

In the AWS lambda function provided below, I have set it up to call an API that lists all country names. The function is connected to an API Gateway and works as expected when tested with Postman. Here is the code snippet: import axios from 'axios&apo ...

Tips for sending all form elements via ajax without explicitly mentioning their names

I'm developing an application with a dynamic form generation feature. When a button is clicked, I need to send all the form elements to another page using Ajax. The challenge is that since the form is generated dynamically, I am unable to specify elem ...

Trying to display logo using 'content' property in CSS

I've been trying to display a logo on the header of my website, but I'm having trouble getting it to show up. I attempted to set the logo using HTML syntax as well as the following CSS code: .div { content: url (...jpg); } However, both metho ...

Unable to modify the width of textarea

I've been struggling to adjust the width of a textarea element, as neither setting rows and cols in HTML nor using CSS has worked for me. The height adjustment was successful with HTML. Any tips on changing the width? form, label { position: rel ...