Search code examples
csstwitter-bootstraplessmedia-queries

Is it possible to pass a line of css coding into less as a variable?


I am trying to create a system that will output different font sizes and line heights for every different media query, in doing so I've been faced with a problem. Any help would be very much appreciated.

The Function

.return-size(@size) when (@size = mob) { @media-dev: "max-width: @media-mobile"; }
.return-size(@size) when (@size = xs) { @media-dev: "max-width: @screen-xs-max"; }
.return-size(@size) when (@size = sm) { @media-dev: "min-width: @screen-sm-min"; }
.return-size(@size) when (@size = md) { @media-dev: "min-width: @screen-md-min"; }
.return-size(@size) when (@size = lg) { @media-dev: "min-width: @screen-lg-min"; }

.font-size-media(@size, @font-size, @multipler: 1.5){
    .return-size(@size);
    @media (@media-dev){
    .font-size(@font-size, @multipler);
    }
}   

I am using Bootstrap for my responsive design and the variables @screen-..-min relate to pixel values when the screen will resize.

The font-size function simply takes a font size and output the font size and line height based on the multiplier.

The Call (example)

.font-size-media(xs,12);

My main problem is when I pass the string to @media-dev it is outputted in my CSS as a string, is there anyway to strip it of the string tags when it gets passed back into font-size-media.


Solution

  • Personally I would probably write such mixin something like (well, sketching mode on):

    .media(@device, @styles) {
        @mob: ~"max-width:" @media-mobile;
        @xs:  ~"max-width:" @screen-xs-max;
        @sm:  ~"min-width:" @screen-sm-min;
        @md:  ~"min-width:" @screen-md-min;
        @lg:  ~"min-width:" @screen-lg-min;
    
        @media (@@device) {@styles();}
    }
    
    .font-size-media(@device, @font-size, @multiplier: 1.5) {
        .media(@device, {
            .font-size(@font-size, @multiplier);
        });
    }
    

    (yet again, not knowing how it can be actually used, so not optimizing things to the death).