【前端】响应式布局笔记——自适应布局

自适应布局

自适应布局是不同设备对应不同的html(局部自适应),通过判断设备的类型或控制局部的变化。

1、获取设备是移动端还是pc端

// 获取设备的信息
    let userAgent = navigator.userAgent.toLowerCase();
    // 使用正则表达式来判断类型
    let device = /ipad|iphone|midp|rv:1.2.3.4|ucweb|android|windows ce|windows mobile/;
    if (device.test(userAgent)) {
        console.log('移动端')
    } else {
        console.log('PC端')
    }

2、flex与media结合

在这里插入图片描述

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .father {
            width: 100%;
            display: flex;
            font-size: 30px;
            text-align: center;
        }

        .father :nth-child(1) {
            background-color: yellow;
            flex: 0 0 80px;
        }

        .father :nth-child(2) {
            background-color: red;
            flex: 1 1 auto;
        }

        .father :nth-child(3) {
            background-color: yellow;
            flex: 0 0 80px;
        }

        @media screen and (min-device-width:400px) and (max-device-width:500px) {
            .father :nth-child(2) {
                background-color: rgb(0, 247, 255);
                flex: 1 1 auto;
            }
        }

        @media screen and (min-device-width:200px) and (max-device-width:399px) {
            .father :nth-child(2) {
                background-color: rgb(179, 255, 0);
                flex: 1 1 auto;
            }
        }
    </style>
</head>

<body>
    <div class="father">
        <div>1</div>
        <div>2</div>
        <div>3</div>
    </div>
</body>

</html>
  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值