I would like to generate a "numbered list" of classes with z-index
value corresponding to the list number, i.e.:
.layer1 {
z-index: 10;
}
.layer2 {
z-index: 9;
}
.layer3 {
z-index: 8;
}
...
Is there a way to do this in CSS such that I don’t have to define all of the classes manually, but instead could use something like a for loop/pattern matching?
2
Answers
The question seems kind of vague, but if you need to use the z-index to stack the list elements then why not style them directly?
Creating an Array from teh children property of the parent element will ensure that the elements don’t get scrambled by using the
index
of the for loop to access the DOM elements from thelistElements
array. From there you assign the z-index to each element style starting at the top (listElements.length
) and subtracting theindex
as the loop progresses. Let me know if this works, cheers!edit: if for some reason you need to give a class to each element just add the following inside the loop:
You can’t do loops with pure
CSS
, but if you are using something likeSASS
then you can do like:Transpiled