【前端】响应式布局笔记——媒体查询

一、媒体查询

为不同尺寸的屏幕设定不同的css样式(常用于移动端)。

案例1

<style>
        .content{
            width: 400px;
            height: 400px;
            border: 1px solid;
        }
        @media screen and (min-device-width:200px) and (min-device-width:300px) {
            .content{
                background: rebeccapurple;
            }
        }
        @media screen and (min-device-width:301px) and (min-device-width:600px) {
            .content{
                background: yellowgreen;
            }
        }
    </style>
    <div class="content"></div>

案例2

div {
            width: 400px;
            height: 400px;
            border: 1px solid;
        }
        @media screen and (min-width:200px) and (max-width:400px) {
            div{
                background-color: rebeccapurple;
            }
        }
        @media screen and (min-width:401px) and (max-width:600px) {
            div{
                background-color: red;
            }
        }
<div></div>

案例 3

.father {
            width: 500px;
            height: 100px;
            background-color: aquamarine;
        }

        .father > div {
            height: 100px;
            float: left;
        }

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

        .father :nth-child(2) {
            background-color: yellow;
        }

        .father :nth-child(3) {
            background-color: blue;
        }
        /* 显示3个 */
        @media screen and (min-device-width:500px) and (max-device-width:600px) {
            .father > div {
                width: 33.33%;
            }
        }
        /* 显示2个 */
        @media screen and (min-device-width:300px) and (max-device-width:400px) {
            .father > div {
                width: 50%;
            }
        }
        /* 线上1个 */
        @media screen and (min-device-width:100px) and (max-device-width:200px) {
            .father > div {
                width: 100%;
            }
        }
<div class="father">
        <div></div>
        <div></div>
        <div></div>
</div>
属性名称作用
width、height浏览器可视宽度、高度
device-width、device-height设备屏幕的宽度、高度

二、媒体查询其他引入方式

1、style内部引入

<!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: 500px;
            height: 100px;
            background-color: aquamarine;
        }

        .father>div {
            height: 100px;
            float: left;
        }

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

        .father :nth-child(2) {
            background-color: yellow;
        }

        .father :nth-child(3) {
            background-color: blue;
        }
    </style>
    <style media="(min-device-width:500px) and (max-device-width:600px) ">
        .father>div {
            width: 33.33%;
        }
    </style>
    <style media="(min-device-width:300px) and (max-device-width:400px)">
        .father>div {
            width: 50%;
        }
    </style>
    <style media="(min-device-width:100px) and (max-device-width:200px)">
        .father>div {
            width: 100%;
        }
    </style>
</head>

<body>
    <div class="father">
        <div></div>
        <div></div>
        <div></div>
    </div>
</body>

</html>

2、link引入

创建css05文件夹,并创建以下文件,如下图所示,
在这里插入图片描述
公共样式
public.css内容

.father {
    width: 500px;
    height: 100px;
    background-color: aquamarine;
}

.father>div {
    height: 100px;
    float: left;
}

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

.father :nth-child(2) {
    background-color: yellow;
}

.father :nth-child(3) {
    background-color: blue;
}

media01.css内容

.father>div {
    width: 33.33%;
}

media02.css内容

.father>div {
    width: 50%;
}

media03.css内容

.father>div {
    width: 100%;
}
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="./css05/public.css">
    <link rel="stylesheet" href="./css05/media01.css" media="(min-device-width:500px) and (max-device-width:600px)">
    <link rel="stylesheet" href="./css05/media02.css" media="(min-device-width:300px) and (max-device-width:400px)">
    <link rel="stylesheet" href="./css05/media03.css" media="(min-device-width:100px) and (max-device-width:200px)">
</head>

<body>
    <div class="father">
        <div></div>
        <div></div>
        <div></div>
    </div>
</body>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值