Fork me on GitHub
随笔 - 234  文章 - 49 评论 - 0 阅读 - 13万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

style内部使用媒体查询

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <!-- width=device-width:设置视口的宽度为设备的宽度,这样网页内容能够适应不同大小的屏幕   -->
     <!--    initial-scale=1.0:设置初始缩放比例为1,确保网页在加载时不进行缩放-->
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Title</title>
    <style>
        #div0 {
            width: 500px;
            height: 200px;
            background: #3c9;
            margin: 0 auto;
        }
        #div0 div {
            width: 100%;
            height: 50px;
            float: left;
        }

        #div0 div:nth-child(1) {
            background-color: #927;
        }

        #div0 div:nth-child(2) {
            background-color: #972;
        }

        #div0 div:nth-child(3) {
            background-color: #733;
        }
        /*使用媒体查询针对屏幕宽度小于或等于499像素的设备*/
        @media screen and (max-width: 499px) {
            #div0 div {
                width: 100%;
            }
        }

        /*使用媒体查询针对屏幕宽度在501像素到1000像素之间的设备*/
        @media screen and (min-width: 501px) and (max-width: 1000px) {
            #div0 div {
                width: 50%;
            }
        }
        
        /*使用媒体查询针对屏幕宽度大于1001像素的设备*/
        @media screen and (min-width: 1001px) {
            #div0 div {
                width: 33.33%;
            }
        }
    </style>
</head>
<body>
<div id="div0">
    <div></div>
    <div></div>
    <div></div>
</div>
</body>
</html>

link使用媒体查询

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Title</title>
    <link rel="stylesheet" href="css/css1.css">
    <link rel="stylesheet" href="css/css2.css">
</head>
<body>
<div id="div0">
    <div></div>
    <div></div>
    <div></div>
</div>
</body>
</html>

css文件配置
css1.css

#div0 {
    width: 500px;
    height: 200px;
    background: #3c9;
    margin: 0 auto;
}
#div0 div {
    width: 100%;
    height: 50px;
    float: left;
}

#div0 div:nth-child(1) {
    background-color: #927;
}

#div0 div:nth-child(2) {
    background-color: #972;
}

#div0 div:nth-child(3) {
    background-color: #733;
}

css2.css

/*使用媒体查询针对屏幕宽度小于或等于499像素的设备*/
@media screen and (max-width: 499px) {
    #div0 div {
        width: 100%;
    }
}

/*使用媒体查询针对屏幕宽度在501像素到1000像素之间的设备*/
@media screen and (min-width: 501px) and (max-width: 1000px) {
    #div0 div {
        width: 50%;
    }
}

/*使用媒体查询针对屏幕宽度大于1001像素的设备*/
@media screen and (min-width: 1001px) {
    #div0 div {
        width: 33.33%;
    }
}
posted on   anyux  阅读(3)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示