CSS-Less class extend class with pseudo class

后端 未结 2 527
长情又很酷
长情又很酷 2020-12-15 19:44

I was wondering how I could do something like the following with less css:

.btn {
  color : black;
}

.btn:hover {
  color : white;
}

.btn-foo {
  .btn;
  &         


        
相关标签:
2条回答
  • 2020-12-15 20:14

    UPDATE: If you can't modify external files just redefine the selectors, and add missing states:

    .btn {
      // not adding anything here, won't affect existing style
      &:hover {
        // adding my own hover state for .btn
        background: yellow;
        ...
      }
    }
    
    // this will make your foo button appear as in external style
    // and have the :hover state just as you defined it above
    .btn-foo {
      .btn;
    }
    

    Better now? :)


    You don't need pseudo class. It will just work :)

    Try this:

    .btn {
      background: yellow;
    
      &:hover { // define hover state here
        background: green;
      }
    }
    
    button {
      .btn;
    }
    

    Each <button class='btn'> element you create will inherit whatever was defined, including hover state. I think it's one of the main amazing features of LESS.

    Hope this helps.

    0 讨论(0)
  • 2020-12-15 20:17

    In Less 1.4.0(1.4.1?)

    This:

    .btn {
      color : black;
    }
    
    .btn:hover {
      color : white;
    }
    
    .btn-foo:extend(.btn all) { 
    }
    

    Expands to this:

    .btn,
    .btn-foo {
      color: black;
    }
    .btn:hover,
    .btn-foo:hover {
      color: white;
    }
    

    Be cautious though, this:

    .btn {
      color : black;
    }
    
    .btn:hover {
      color : white;
    }
    
    .abc .btn {
      margin: 2px;
    }
    
    .btn-foo:extend(.btn all) {
    
    }
    

    Will output this:

    .btn {
      color : black;
    }
    
    .btn:hover {
      color : white;
    }
    
    .abc .btn {
      margin: 2px;
    }
    
    .btn-foo:extend(.btn all) {
    
    }
    

    I have not looked into SASS more than half an hour, but I believe the later case is its default (or only) @extend behavior.

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