css pc大小屏适配及h5手机屏幕兼容 rem

  1. @media查询比较适合pc 屏幕适配

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        @media screen and (min-width:1200px) {
            .odiv {
                background: #f00;
            }
        }

        @media screen and (min-width:960px) and (max-width:1200px) {
            .odiv {
                background: #ff0;
            }
        }

        @media screen and (max-width:960px) {
            .odiv {
                background: #f0f;
            }
        }
    </style>
</head>

<body>
    <div class="odiv">
     123
    </div>
</body>

</html>

2.计算属性rem 比较适合手机屏幕适配

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1, minimum-scale=1">
    <script type="text/javascript">
        if(document.documentElement.clientWidth < 750){
            document.documentElement.style.fontSize = document.documentElement.clientWidth / 640*32 + 'px';
            console.log(document.documentElement.clientWidth)
        }else{
            document.documentElement.style.fontSize = "16px";
        }
         
    </script>
</head>
<body>
<div>
    <span style="font-size:1rem;">danny.xie</span>
    <span style="font-size:16px;">danny.xie</span>
</div>
</body>
</html>

你可能感兴趣的:(css)