上一节:CSS3多列布局——————下一节:响应式图片

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">

  • 媒体查询常用书写方式,直接在样式表内书写
    web基础学习(十五)CSS3响应式布局

  • 媒体查询特性定义web基础学习(十五)CSS3响应式布局

  • 媒体查询使用的设备类型
    web基础学习(十五)CSS3响应式布局

  • 媒体查询用法很简洁,看下面的响应式导航示例:
    web基础学习(十五)CSS3响应式布局

<!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> 

相关文章:

  • 2021-06-03
  • 2021-05-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-08
  • 2022-01-05
  • 2022-12-23
猜你喜欢
  • 2021-11-07
  • 2021-09-30
  • 2022-03-04
  • 2021-08-30
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案