Using regular expression in css?

前端 未结 8 1572
野的像风
野的像风 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:16

    there is another simple way to select particular elements in css too...

    #s1, #s2, #s3 {
        // set css attributes here
    }
    

    if you only have a few elements to choose from, and dont want to bother with classes, this will work easily too.

    0 讨论(0)
  • 2020-11-22 06:20

    As complement of this answer you can use $ to get the end matches and * to get matches anywhere in the value name.

    Matches anywhere: .col-md, .left-col, .col, .tricolor, etc.

    [class*="col"]
    

    Matches at the beginning: .col-md, .col-sm-6, etc.

    [class^="col-"]
    

    Matches at the ending: .left-col, .right-col, etc.

    [class$="-col"]
    
    0 讨论(0)
  • 2020-11-22 06:20

    You can' just add a class to each of your DIVs and apply the rule to the class in this way:

    HTML:

    <div class="myclass" id="s1">...</div>
    <div class="myclass" id="s2">...</div>
    

    CSS:

    //css
    .myclass
    {
       ...
    }
    
    0 讨论(0)
  • 2020-11-22 06:22

    Try my generic CSS regular expression

    (([a-z]{5,6}.*?\))|([\d.+-]?)(?![a-z\s#.()%])(\d?\.?\d?)?[a-z\d%]+)|(url\([/"'][a-z:/.]*['")]\))|(rgb|hsl)a?\(\d+%?,?\s?\d+%?,?\s?\d+%?(,\s?\d+\.?\d?)?\)|(#(\w|[\d]){3,8})|([\w]{3,8}(?=.*-))
    

    Demo https://regexr.com/4a22i

    0 讨论(0)
  • 2020-11-22 06:26

    First of all, there are many, many ways of matching items within a HTML document. Start with this reference to see some of the available selectors/patterns which you can use to apply a style rule to an element(s).

    http://www.w3.org/TR/selectors/

    Match all divs which are direct descendants of #main.

    #main > div
    

    Match all divs which are direct or indirect descendants of #main.

    #main div
    

    Match the first div which is a direct descendant of #sections.

    #main > div:first-child
    

    Match a div with a specific attribute.

    #main > div[foo="bar"]
    
    0 讨论(0)
  • 2020-11-22 06:30

    I usually use * when I want to get all the strings that contain the wanted characters.

    * used in regex, replaces all characters.

    Used in SASS or CSS would be something like [id*="s"] and it will get all DOM elements with id "s......".

    /* add red color to all div with id s .... elements */
    
    div[id^="s"] {
        color: red;
    }
    
    0 讨论(0)
提交回复
热议问题