What is css “[class*=my-class] .my-subclass” doing?

前端 未结 2 1548
予麋鹿
予麋鹿 2021-01-18 18:14

I inherited some css and I have searched everywhere online to understand what is being expressed by a block of css that looks like:

 [class*=wrapper] .logo {         


        
相关标签:
2条回答
  • 2021-01-18 18:51

    It selects an element with class logo that has an ancestor that has wrapper somewhere in its class attribute. For example note that the class burgerwrapper also leads to the element being selected below.

    [class*=wrapper] .logo {
      color: #f99;
    }
    <div class="logo">Not selected</div>
    
    <div class="wrapper">
      <div class="logo">
        Selected
      </div>
    </div>
    
    <div class="burgerwrapper">
      <div class="logo">
        Selected
      </div>
    </div>

    See http://css-tricks.com/attribute-selectors/ for some background information on attribute selectors.

    0 讨论(0)
  • 2021-01-18 18:55

    what square brackets doing

    Attribute selectors

    CSS 2.1 allows authors to specify rules that match elements which have certain attributes defined in the source document.

    Attribute selectors w3

    What is the asterisk

    Substring matching attribute selectors

    [att*=val] Represents an element with the att attribute whose value contains at least one instance of the substring "val". If "val" is the empty string then the selector does not represent anything.

    Substring matching attribute selectors

    To sum it up in you example:

    [class*=wrapper] .logo {
      color: red;
    }
    <div class="wrapper">
      <div>not this</div>
      <div class="logo">this</div>
      <div class="logo">this</div>
    </div>
    <div>
      <div>not this</div>
      <div class="logo">not this</div>
      <div>not this</div>
    </div>

    Select child elements with class .logo that their parent element has attribute class with value wrapper appears somewhere in that attribute.

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