wildcard * in CSS for classes

前端 未结 4 1925
忘掉有多难
忘掉有多难 2020-11-22 10:58

I have these divs that I\'m styling with .tocolor, but I also need the unique identifier 1,2,3,4 etc. so I\'m adding that it as another class tocolor-1

相关标签:
4条回答
  • 2020-11-22 11:18

    Yes you can this.

    *[id^='term-']{
        [css here]
    }
    

    This will select all ids that start with 'term-'.

    As for the reason for not doing this, I see where it would be preferable to select this way; as for style, I wouldn't do it myself, but it's possible.

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

    What you need is called attribute selector. An example, using your html structure, is the following:

    div[class^="tocolor-"], div[class*=" tocolor-"] {
        color:red 
    }
    

    In the place of div you can add any element or remove it altogether, and in the place of class you can add any attribute of the specified element.

    [class^="tocolor-"] — starts with "tocolor-".
    [class*=" tocolor-"] — contains the substring "tocolor-" occurring directly after a space character.

    Demo: http://jsfiddle.net/K3693/1/

    More information on CSS attribute selectors, you can find here and here. And from MDN Docs MDN Docs

    0 讨论(0)
  • 2020-11-22 11:31

    If you don't need the unique identifier for further styling of the divs and are using HTML5 you could try and go with custom Data Attributes. Read on here or try a google search for HTML5 Custom Data Attributes

    0 讨论(0)
  • 2020-11-22 11:39

    An alternative solution:

    div[class|='tocolor'] will match for values of the "class" attribute that begin with "tocolor-", including "tocolor-1", "tocolor-2", etc.

    Beware that this won't match

    <div class="foo tocolor-">
    

    Reference: https://www.w3.org/TR/css3-selectors/#attribute-representation

    [att|=val]

    Represents an element with the att attribute, its value either being exactly "val" or beginning with "val" immediately followed by "-" (U+002D)

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