Search code examples
csscss-selectorslesspseudo-class

Can a LESS CSS pseudo-class pass an attribute of the selector to a function?


I'm currently trying to create a single point of definition for my gradients in LESS CSS. I've created a function that writes the cross-browser CSS code for me, but there's one issue I can't solve.

I would like to specify the gradients once and have a seperate function (that listens to the argument "flip") swap the two color variables on hover. I've posted an example below:

    selector { .background-gradient(rgba(27, 117, 185, .35), 48%, rgba(22, 97, 154, .35), 52%); }
    selector:hover { .background-gradient(flip); }

I've been looking for a solution but, of course, found nothing. To sum things up: I'd like to have a function that reads the selector's gradient values and uses them to create a hover style by swapping the colors. I hope that it's possible.

Thanks in advance!

PS: Creating the function that listens to "flip" (pattern-matching) is not the problem, but I thought it give you a better idea of what I'm actually trying to achieve.


Solution

  • Its not 100% clear what you want, but why not have something like this

    .selector {
      @startCol: rgba(27, 117, 185, .35);
      @startPercentage: 48%;
      @endCol: rgba(22, 97, 154, .35)
      @endPercentage: 52%;
      .gradient(@startCol, @startPercentage, @endCol, @endPercentage);
      &:hover {
        .gradient(@endCol, @endPercentage, @startCol, @startPercentage);
      }
    }
    

    If its common to flip on the hover you could make the above a mixin and then you would just have to call it once per class/gradient definition.

    There is no way for the mixin to know the arguments previously passed to a function or to call a mixin with an array of values.