专项:响应式布局

响应式布局的概念:一个网站能够兼容多个终端——而不是为每个终端做一个特定的版本。可以为不同终端的用户提供更加舒适的界面和更好的用户体验。

响应式布局原理:通过媒体查询显示设备显示器特性(如视口宽度、屏幕比例、设备方向、横向或纵向),并根据这些特性为其设置CSS样式。

设备类型(默认为all)

 断点:当屏幕达到某个分辨率临界点时,用上某个样式。

案例1 媒体查询

<!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>
        *{
            margin: 0px;
            padding: 0px;
        }
        html,body{
            height: 100%;
        }

        @media screen and (min-width:1000px){
            body{
                background-color:yellow;
            }
        }
        @media screen and (max-width:1000px) and (min-width:500px){
            body{
                background-color:red;
            }
        }
        @media screen and (max-width:500px){
            body{
                background-color:green;
            }
        }
    </style>
</head>
<body>
    
</body>
</html>
View Code

 案例二 横竖屏检测

<!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>
        *{
            margin: 0px;
            padding: 0px;
        }
        body{
            display: flex;
            flex-wrap: wrap;
        }
        div{
            height: 100px;
            background-color: yellow;
            border: 2px solid red;
            box-sizing: border-box;
        }
        /* 设置竖屏 */
        @media screen and (orientation:portrait) {
            div{
                width: 33.333%;
            }
        }
        /* 设置横屏 */
        @media screen and (orientation:landscape) {
            div{
                width: 20%;
            }
        }
    </style>
</head>
<body>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
</body>
</html>
View Code

 案例三

<!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>
        *{
            margin: 0px;
            padding: 0px;
        }
        html,body{
            height: 100%;
        }
        .top{
            display: flex;
            flex-wrap: wrap;
            justify-content: space-around;
            margin: 5px;
        }
        .top>div{
            width: 49%;
        }

        @media screen and (max-width:768px){
            .top>div{
                width: 100%;
            }
        }
        .top img{
            width: 100%;
            height: 100%;
        }
        .top .right{
            display: flex;
            justify-content: space-between;
        }
        .top .right>div{
            width: 49%;
        }

        .bottom{
            display: flex;
            flex-wrap: wrap;
            justify-content: space-around;
        }
        .bottom>div{
            width: 23%;
            padding: 5px;
            box-sizing: border-box;
            border: 1px solid gray;
            margin-top: 10px;
            box-shadow:  0 0 5px gray;
        }
        @media screen and (max-width:1024px) and (min-width:768px){
            .bottom>div{
                width: 31%;
            }
        }
        @media screen and (max-width:768px) and (min-width:450px){
            .bottom>div{
                width: 48%;
            }
        }
        @media screen and (max-width:450px){
            .bottom>div{
                width: 100%;
            }
        }
        .bottom img{
            width: 100%;
        }
    </style>
</head>
<body>
    <div class="top">
        <div class="left">
            <img src="../../../img/2.jpg" alt="">
        </div>
        <div class="right">
            <div><img src="../../../img/1.gif" alt=""></div>
            <div><img src="../../../img/15.gif" alt=""></div>
        </div>
    </div>
    <div class="bottom">
        <div>
            <img src="../../../img/14.jpg" alt="">
            <p>迷你微型摄影展</p>
        </div>
        <div>
            <img src="../../../img/14.jpg" alt="">
            <p>迷你微型摄影展</p>
        </div>
        <div>
            <img src="../../../img/14.jpg" alt="">
            <p>迷你微型摄影展</p>
        </div>
        <div>
            <img src="../../../img/14.jpg" alt="">
            <p>迷你微型摄影展</p>
        </div>
        <div>
            <img src="../../../img/14.jpg" alt="">
            <p>迷你微型摄影展</p>
        </div>
        <div>
            <img src="../../../img/14.jpg" alt="">
            <p>迷你微型摄影展</p>
        </div>
        <div>
            <img src="../../../img/14.jpg" alt="">
            <p>迷你微型摄影展</p>
        </div>
        <div>
            <img src="../../../img/14.jpg" alt="">
            <p>迷你微型摄影展</p>
        </div>
        <div>
            <img src="../../../img/14.jpg" alt="">
            <p>迷你微型摄影展</p>
        </div>
        <div>
            <img src="../../../img/14.jpg" alt="">
            <p>迷你微型摄影展</p>
        </div>
        <div>
            <img src="../../../img/14.jpg" alt="">
            <p>迷你微型摄影展</p>
        </div>
        <div>
            <img src="../../../img/14.jpg" alt="">
            <p>迷你微型摄影展</p>
        </div>
        <div>
            <img src="../../../img/14.jpg" alt="">
            <p>迷你微型摄影展</p>
        </div>
        <div>
            <img src="../../../img/14.jpg" alt="">
            <p>迷你微型摄影展</p>
        </div>
        <div>
            <img src="../../../img/14.jpg" alt="">
            <p>迷你微型摄影展</p>
        </div>
        <div>
            <img src="../../../img/14.jpg" alt="">
            <p>迷你微型摄影展</p>
        </div>
        <div>
            <img src="../../../img/14.jpg" alt="">
            <p>迷你微型摄影展</p>
        </div>
        <div>
            <img src="../../../img/14.jpg" alt="">
            <p>迷你微型摄影展</p>
        </div>
        <div>
            <img src="../../../img/14.jpg" alt="">
            <p>迷你微型摄影展</p>
        </div>
        <div>
            <img src="../../../img/14.jpg" alt="">
            <p>迷你微型摄影展</p>
        </div>
    </div>
</body>
</html>
View Code

 

 

 

 

 

 

posted @ 2023-12-17 14:57  孤兒教母  阅读(8)  评论(0)    收藏  举报