简体   繁体   中英

How to color each li in css in different colors

every body I have this css color every li it's work You can see example in this article https://devlopertechnology.blogspot.com/2019/11/software-engineering-software-project.html

This the code my question is in the end

.post ul li:nth-of-type(12){
  background-color: #9FA4C4;
}
.post ul li:nth-of-type(13) {
  background:   #B3CDD1 ;
}
.post ul li:nth-of-type(14) {
  background:   #9E768F ;
}
.post ul li:nth-of-type(15) {
  background:   #9FA4C4 ;
}
.post ul li:nth-of-type(16) {
  background:   #B279A7 ;
}
.post ul li:nth-of-type(17) {
  background:   #D387AB ;
}
.post ul li:nth-of-type(18) {
  background:   #864BA2 ;
}
.post ul li:nth-of-type(21) {
  background:   #BF3A30 ;
}
.post ul li:nth-of-type(10) {
  background:   #6E45E1 ;
}
.post ul li:nth-of-type(11) {
  background:   #89D4CF ;
}
.post ul li:nth-of-type(2) {
  background:   #A7ACD9 ;
}
.post ul li:nth-of-type(3) {
  background:   #9E8FB2;
}
.post ul li:nth-of-type(4) {
  background:   #A88BEB ;
}
.post ul li:nth-of-type(5) {
  background:   #F8CEEC ;
}

.post ul li:nth-of-type(6) {
  background:   #B58ECC ;
}
.post ul li:nth-of-type(7) {
  background:   #5DE6DE ;
}
.post ul li:nth-of-type(8) {
  background:   #647DEE ;
}
.post ul li:nth-of-type(9) {
  background:   #7F53AC ;
}
.post ul li:nth-of-type(19) {
  background:   #77EED8 ;
}
.post ul li:nth-of-type(20) {
  background:   #9EABE4 ;
}

But I want to convert it to dynamic, using loop or function If there is idea, thank you for your help.

You can try this:

    // Fetch the list of elements matching the selector
    const listElements = document.querySelectorAll('.post ul li');
    // Store the colors in an array
    const colors = [here you can store the colors you want]; // Assuming this has the same number of values as the list in your html, otherwise the below loop will fail if the values are less.
    // Map each color with the corresponding list element
    for(let index in listElements) {
        listElements[index].style.backgroundColor = colors[index];
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM