Using regular expression in css?

前端 未结 8 1573
野的像风
野的像风 2020-11-22 06:01

I have an html page with divs that have id(s) of the form s1, s2 and so on.

相关标签:
8条回答
  • 2020-11-22 06:36

    An ID is meant to identify the element uniquely. Any styles applied to it should also be unique to that element. If you have styles you want to apply to many elements, you should add a class to them all, rather than relying on ID selectors...

    <div id="sections">
       <div id="s1" class="sec">...</div>
       <div id="s2" class="sec">...</div>
       ...
    </div>
    

    and

    .sec {
        ...
    }
    

    Or in your specific case you could select all divisions inside your parent container, if nothing else is inside it, like so:

    #sections > div {
        ...
    }
    
    0 讨论(0)
  • 2020-11-22 06:37

    You can manage selecting those elements without any form of regex as the previous answers show, but to answer the question directly, yes you can use a form of regex in selectors:

    #sections div[id^='s'] {
        color: red;  
    }
    

    That says select any div elements inside the #sections div that have an ID starting with the letter 's'.

    See fiddle here.

    W3 CSS selector docs here.

    0 讨论(0)
提交回复
热议问题