What does an “&” before a pseudo element in CSS mean?

后端 未结 3 1444
后悔当初
后悔当初 2020-12-02 11:59

In the following CSS taken from Twitter Bootstrap what does the ampersand (&) character mean?

.clearfix {
  *zoom: 1;
  &:before,
  &:after {
            


        
相关标签:
3条回答
  • 2020-12-02 12:29

    That's LESS, not CSS.

    This syntax allows you nest nest selector modifiers.

    .clearfix { 
      &:before {
        content: '';
      }
    }
    

    Will compile to:

    .clearfix:before {
      content: '';
    }
    

    With the &, the nested selectors compile to .clearfix:before.
    Without it, they compile to .clearfix :before.

    0 讨论(0)
  • 2020-12-02 12:36

    A nested & selects the parent element in both SASS and LESS. It's not just for pseudo elements, it can be used with any kind of selector.

    e.g.

    h1 {
        &.class {
    
        }
    }
    

    is equivalent to:

    h1.class {
    
    }
    
    0 讨论(0)
  • 2020-12-02 12:42

    Here is an SCSS/LESS example:

    a {
       text-decoration: underline; 
       @include padding(15px);
       display: inline-block;
    
         & img  {
                    padding-left: 7px;
                   margin-top: -4px;
                 }
     }
    

    and its equivalent in CSS:

    a {
      text-decoration: underline; 
      @include padding(15px);
      display: inline-block;
    }
    
    a img  {
         padding-left: 7px;
         margin-top: -4px;
       }
    
    0 讨论(0)
提交回复
热议问题