首页 > 代码库 > scss和less px 转rem

scss和less px 转rem

1 首先设置默认字符大小

<script>
            (function() {
                var html = document.documentElement;
                var hWidth = html.getBoundingClientRect().width;//根据屏宽调节大小
                //                console.log( hWidth );
                html.style.fontSize = hWidth / 15 + "px";//设置默认字符大小 25
            })()
        </script>

2 scss写法:

@function t($px) {
    @return $px / 50px * 1rem;
}
p {
    width: t(20px);
}

3 less 写法:

@r:50rem;
p{
    width: 20/@r;
}

4 编译后:

p {
  width: 0.4rem;
}

 

scss和less px 转rem