【面试题】CSS响应式

1. rem是什么?

  • rem,CSS3新增的一个相对单位(root em,根em),相对于根元素,常用于响应式布局
  • em,相对长度单位,相对于父元素,不常用
  • px,像素(Pixel)绝对长度单位
<style>
    html{
        font-size: 100px;
    }
    div{
        background-color: #ccc;
        margin-top: 10px;
        font-size: 0.15rem;
    }
    p{background-color: #ccc;}
</style>

<body>
    <p style="font-size: 0.1rem">rem 1</p>
    <p style="font-size: 0.2rem">rem 2</p>

    <div>
        this is div1
    </div>
    <div>
        this is div2
    </div>
</body>

在这里插入图片描述

2. 响应式布局的常见方案

首先,进行media-query,根据不同的屏幕宽度设置根元素font-size。然后,通过rem,基于根元素的相对单位。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        /* iphone5 或者更小的尺寸 */
        @media only screen and (max-width: 374px){
            html{
                font-size: 86px;
            }
        }
        /* iphone6/7/8 和 iphone x */
        @media only screen and (min-width: 375px) and (max-width: 413px){
            html{
                font-size: 100px;
            }
        }
        /* iphone6p 或者更大 */
        @media only screen and (min-width: 375px){
            html{
                font-size: 110px;
            }
        }

        body{
            font-size: 0.16rem;
        }
        #div1{
            width: 1rem;
            background-color: red;
        }
    </style>
</head>
<body>
    <div id = "div1">
        this is div
    </div>
</body>
</html>

在这里插入图片描述

3. vm/vh

网页视口尺寸:

属性解释
window.screen.height屏幕高度
window.innerHeight网页视口高度
document.body.clientHeightbody高度
  • vh:网页视口高度的1/100
  • vw:网页视口宽度的1/100
  • vmax取两者最大值,vmin取两者最小值
<style>
    body{
        margin: 0;
        padding: 0;
    }

    #container{
        background-color: red;
        width: 10vw;
        height: 10vh;
    }
</style>

<body>
    <p>vw vh 测试</p>
    <div id = "container"></div>
</body>

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

周兴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值