css - LESS - combine rules of media queries with same condition -
i using following less mixin optimize background images 2x screens.
definition::
.image-2x(@image, @width: auto, @height: auto, @repeat: no-repeat) { @filename : ~`/(.*)\.(jpg|jpeg|png|gif)/.exec(@{image})[1]`; @extension : ~`/(.*)\.(jpg|jpeg|png|gif)/.exec(@{image})[2]`; background-image: ~`"url(@{filename}.@{extension})"`; background-repeat: @repeat; background-size: @width @height; @media screen , (-webkit-min-device-pixel-ratio: 2), screen , ( min--moz-device-pixel-ratio: 2), screen , ( min-device-pixel-ratio: 2), screen , ( min-resolution: 192dpi), screen , ( min-resolution: 2dppx) { background-image: ~`"url(@{filename}@2x.@{extension})"`; } } }
usage::
selector1 { .image-2x("path/to/image1.png"); } selector2 { .image-2x("path/to/image2.png"); }
output::
selector1 { background-image: url(path/to/image1.png); background-repeat: no-repeat; background-size: auto auto; } @media screen , (-webkit-min-device-pixel-ratio: 2), screen , ( min--moz-device-pixel-ratio: 2), screen , ( min-device-pixel-ratio: 2), screen , ( min-resolution: 192dpi), screen , ( min-resolution: 2dppx) { selector1 { background-image: url(path/to/image1@2x.png); } } } selector2 { background-image: url(path/to/image2.png); background-repeat: no-repeat; background-size: auto auto; } @media screen , (-webkit-min-device-pixel-ratio: 2), screen , ( min--moz-device-pixel-ratio: 2), screen , ( min-device-pixel-ratio: 2), screen , ( min-resolution: 192dpi), screen , ( min-resolution: 2dppx) { selector2 { background-image: url(path/to/image2@2x.png); } } }
optimal output::
selector1 { background-image: url(path/to/image1.png); background-repeat: no-repeat; background-size: auto auto; } selector2 { background-image: url(path/to/image2.png); background-repeat: no-repeat; background-size: auto auto; } @media screen , (-webkit-min-device-pixel-ratio: 2), screen , ( min--moz-device-pixel-ratio: 2), screen , ( min-device-pixel-ratio: 2), screen , ( min-resolution: 192dpi), screen , ( min-resolution: 2dppx) { selector1 { background-image: url(path/to/image1@2x.png); } selector2 { background-image: url(path/to/image2@2x.png); } } }
to achieve thought of storing @2x background image urls in list , printing saved rulesets @ end of file within 1 media query. couldn't find way it.
can suggest solution, fine kind of approach.
i using grunt-contrib-less
compile less. if compiler optimizes output, please mention it.
Comments
Post a Comment