B
B
BigKompot2016-08-09 22:05:10
css
BigKompot, 2016-08-09 22:05:10

LESS. Is it possible to convert px to rem without mixins?

Hello.
Interested in the possibility of automatic conversion to other units of measurement using parameterized variables.
For example, a variable of type is created @value(), and when you write a value in parentheses px, it gives the result in remto the property in which it is specified.
Something like:

.block {
  width: @value(100);
}

After compilation:
.block {
  width: 6.25rem;
}

Using mixins allows you to define only specific properties, but I'm interested in the possibility of universal application

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Eugene, 2016-08-09
@AppFA

There is no way with standard tools, in less it seems like there are no user functions, but you can add an extension to JS, on stackoverflow somewhere there was a similar question and this answer:

em: function(less, fontsize, basefontsize){
    if (less.functions.functionRegistry.get('ispixel')){
        basefontsize = (basefontsize) ? basefontsize.value : 16
        return fontsize.value/basefontsize+'em';
    }
}

.block {
    font-size: em(16px);
}

I
Ivan Bogachev, 2016-08-09
@sfi0zy

There really are no user-defined functions, but, as an option, you can take this thing:
And multiply by it in those places where you need to convert px to rem:

p {
    font-size: @rem * 50;
    border: @rem * 5 solid #000;
    padding: @rem * 10;

    /* В CSS получится
        font-size: 3.125rem;
        border: 0.3125rem solid #000;
        padding: 0.625rem;
    */
}

Link to codepen

Z
zooks, 2016-08-10
@zooks

Here is my Sass mixin.

$browser-context: 16

@function rem($pixels, $context: $browser-context)
  @return #{$pixels / $context}rem

Challenge:
In general, I recommend switching to Sass. After LESS, the Scss syntax will be more convenient.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question