07. How to implement the mobile REM adaptation

How to achieve mobile terminal rem adaptation
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">

    <!--follow meta code for mobile adaptation -->
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no" />
    <title></title>
    <style type="text/css">
        *{
            margin: 0;
            padding: 0;
        }
        #box{
            width: 7.5rem;
            height: 7.5rem;
            background: red;
        }

    </style>
</head>
<body>
<div id="box"></div>
</body>
<script type="text/javascript">
    //get screen width
    var width = document.documentElement.clientWidth;

    //get html
    var htmlNode = document.querySelector('html');

    //set font size
    htmlNode.style.fontSize = width/7.5 + 'px';

</script>
</html>
Background image distance
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
        }

        #box{
            width: 100px;
            height: 200px;
            background: pink;
            padding: 100px;
            border: 80px solid blue;
            background-image: url("img/1.png");
            background-repeat: no-repeat;
            background-origin: content-box;
            background-position: -50px 0;
        }

        /*Answer:130px*/
    </style>
</head>
<body>
<div id="box"></div>
</body>
</html>

Leave a Comment

Your email address will not be published.