分享

web基础学习(十五)CSS3响应式布局

 印度阿三17 2019-04-02

1.视口

  • 下面这段代码控制页面不同大小的设备可以响应布局

    <meta name="viewport" content="width=device-width,initial-scale=1.0">

2. 媒体查询

  • 常用的屏幕尺寸从小到大如下所示

    老智能机: 320px-480px
    智能手机: ≥ 480px
    平板电脑: ≥ 768px
    中等屏幕(桌面显示器): ≥ 992px
    大屏幕(大桌面显示器): ≥1200px

注意: 实现过程中,遵循移动优先原则

  • 媒体查询引入式书写方式,引入的代码就是响应式代码(个人感觉此种方式用的较少)

    <link rel="stylesheet" media="(max-width:800px)" href="example.css">

  • 媒体查询常用书写方式,直接在样式表内书写
    在这里插入图片描述

  • 媒体查询特性定义在这里插入图片描述

  • 媒体查询使用的设备类型
    在这里插入图片描述

  • 媒体查询用法很简洁,看下面的响应式导航示例:
    在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <!--视口代码(必需)-->
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Document</title>
    <style type="text/css">
        body {
            margin: 0;
        }

        ul {
            list-style: none;
            margin: 0;
            padding: 0;
        }

        nav {
            height: 40px;
        }

        nav ul {
            display: flex;
            flex-direction: row;
            flex-wrap: nowrap;

        }

        nav ul li {
            height: 40px;
            text-align: center;
            line-height: 40px;
            flex: 1 1 100%;
        }

        nav ul li a {
            text-decoration: none;
            font-size: 14px;
            color: #fff;
            font-weight: bold;
        }

        nav ul li:nth-child(1) {
            background-color: #1685a9;
        }

        nav ul li:nth-child(2) {
            background-color: #fff143;
        }

        nav ul li:nth-child(3) {
            background-color: #ff2d51;
        }

        nav ul li:nth-child(4) {
            background-color: #0eb83a;
        }

        nav ul li:nth-child(5) {
            background-color: #003371;
        }

        nav ul li:nth-child(6) {
            background-color: #ff0097;
        }
        /*当屏幕小于768px时执行下面代码*/
        @media (max-width: 768px) {
            nav ul {
                flex-direction: row;
                flex-wrap: wrap;
            }

            nav ul li {
                flex: 1 1 50%;
            }
        }
        /*当屏幕小于480px时执行下面代码*/
        @media (max-width: 480px) {
            nav ul {
                flex-direction: column;
                flex-wrap: nowrap;
            }

            nav ul li {
                flex: 1 1 100%;
            }
        }
    </style>
</head>
<body>
<nav>
    <ul>
        <li><a href="">index</a></li>
        <li><a href="">index</a></li>
        <li><a href="">index</a></li>
        <li><a href="">index</a></li>
        <li><a href="">index</a></li>
        <li><a href="">index</a></li>
    </ul>
</nav>
</body>
</html> 
来源:http://www./content-4-154451.html

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多