How match ID number with CSS regexp

I’m customising a WordPress theme and am stuck with the following CSS issue. I have a list of elements as follows:

li.block1, li.block2, li.block3, li.block4 {  top: 0; }
li.block5, li.block6, li.block7, li.block8 {  top: 150px; }
li.block9, li.block10, li.block11, li.block12 {  top: 300px; }

I’m generating the code using PHP and wish to use regexp to style these elements. I’d like to achieve the following:

Read More
- li.block[1-4] {style 1}
- li.block[5-9] {style 2}
- li.block[10-14] {style 3}
- and so on...

As I’m generating the li.block[number] dynamically based on user input, which could run into a few hundreds of elements, I believe that using regexp is the right way to go.

I’ve tried the following:

  • Tried to use $ to match the last element, but I’d like some help on how to match on a range of values
  • Tried using last-child attribute match, but as it’s dynamic, I can’t fix the last-child in advance
  • Tried to use in-range to match the range, but I couldn’t figure out how to check whether the number is in the range
  • and, several other DOM matching criteria using CSS

I think the CSS match condition I’m using is erroneous, so I’d like to request experts in this forum to show me how to properly use $ and range:

How do I use regexp based on: li.block[number,i, in range a-b] {Apply style number, i}?

I’m also open to other ideas that help me keep the theme dynamic.

Thanks in advance,
Mithun Sridharan

Related posts

2 comments

  1. Another way is chaining :nth-child pseudoclasses e.g.

    Example: https://jsfiddle.net/pg0c3qq3/2/

       li:nth-child(-n + 4) { top: 0 }
       li:nth-child(n+5):nth-child(-n + 8) { top: 150px; }
       li:nth-child(n+9):nth-child(-n + 12) {  top: 300px; }
    

    Compared to the general sibling solution, every rule now just defines the style only to the expected range of li elements so, with this method every rule is not actually reverting the style applied from the previous rules.

    Also this doesn’t require to apply a specific class to each list-item so the markup can be simplified.

Comments are closed.