响应式布局——CSS Media Query

前言

现在越来越多的人使用手机平板等移动设备来浏览网站,所以网站开发中响应式布局非常重要。下面记录一个简单的响应式布局方案,使用 HTML+CSS 来实现,(CSS Media Query)。并附上一个响应式简单博客。

实现

原理

本次记录的响应式开发原理非常简单,就是通过用户浏览器的尺寸来判断设备类型,然后根据不同的设备显示不同的 CSS 样式。

实现方法

  1. 首先创建 html 文件,然后引入三个 css 文件:common.css(通用属性)、desktop.css(电脑端属性)、mobile.css(手机端属性)。

  2. 为后两个 CSS 文件添加 media 属性,如下:

    <!DOCTYPE html>
    <html lang="zh-CN">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <link rel="stylesheet" href="css/common.css">
        <!--当屏幕尺寸大于 500px 时使用 desktop.css 中的样式-->
        <link media="(min-width:500px)" rel="stylesheet" href="css/desktop.css">
        <!--当屏幕尺寸小于 500px 时使用 mobile.css 中的样式-->
        <link media="(max-width:500px)" rel="stylesheet" href="css/mobile.css">
        <title>ResponsiveLayout</title>
    </head>
    <body>
        <div class="main">
            This is a text.
        </div>
    </body>
    </html>
    

实例

下面附上一个自己写的简单博客的代码。

index.html

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.0/normalize.css" rel="stylesheet">
    <link rel="stylesheet" href="css/main.css">
    <link media="(max-width:500px)" rel="stylesheet" href="css/mobile.css">
    <title>BLog</title>
</head>
<body>
    <div class="side-bar">
        <label id="menu-toggle" for="menu-checkbox">目录</label>
        <input id="menu-checkbox" type="checkbox">
        <div class="header">
            <a href="index.html" class="logo">Talon</a>
            <div class="intro">心中有党,代码理想。心中有党,代码理想。心中有党,代码理想。心中有党,代码理想。</div>
        </div>
        <div class="nav">
            <a href="#" class="item">关于我</a>
            <a href="#" class="item">联系我</a>
            <a href="#" class="item">友情链接</a>
        </div>
        <div class="tag">
            <a href="#" class="item"># 前端</a>
            <a href="#" class="item"># JavaScript</a>
            <a href="#" class="item"># Vue</a>
            <a href="#" class="item"># HTML5</a>
        </div>
    </div> 
    <div class="main">
        <div class="artical-list">
            <div class="item">
                <div class="title">ArticleTitle</div>
                <div class="status">发布于 xx | 标签: # 1</div>
                <div class="content">
                    This is a Blog
                </div>
            </div>
            <div class="item">
                    <div class="title">ArticleTitle</div>
                    <div class="status">发布于 xx | 标签: # 1</div>
                    <div class="content">
                        This is a Blog
                    </div>
                </div>
                <div class="item">
                        <div class="title">ArticleTitle</div>
                        <div class="status">发布于 xx | 标签: # 1</div>
                        <div class="content">
                            This is a Blog
                        </div>
                    </div>
        </div>
    </div>
</body>
</html>

css/main.css

* {
    box-sizing: border-box;
}
body{
    background-color: #454545;
    line-height: 1.7;
}
a{
    text-decoration: none;
}
a, body{
    color: #eeeeee;
}
.side-bar{
    float: left;
    width: 15%;
    position: fixed;
}
.side-bar > *{
    padding: 10px 20px;
}
.side-bar .nav a,
.side-bar .tag a {
    display: block;
    padding: 5px;
    color: #999999;
    transition: color 200ms;
}
.side-bar .nav a:hover,
.side-bar .tag a:hover {
    color: #eeeeee;
}
.side-bar .nav a {
    font-weight: 700;
}
.main{
    float: right;
    width: 85%;
    color: #454545
}
.header .logo{
    line-height: 1;
    border: 5px solid #eeeeee;
    padding: 10px 20px;
    font-size: 30px;
    display: inline-block;
    margin-bottom: 10px;
}
.artical-list {
    margin-right: 30%;
    background-color: #fff;
    padding: 20px 30px;
    box-shadow: 0 0 3px 2px rgba(0, 0, 0, 0.2)
}
.artical-list .item {
    margin-bottom: 20px;
}
.artical-list .item > * {
    margin: 10px 0;
}
.artical-list .item .title {
    font-size: 25px;
    font-weight: 700; 
}
.artical-list .item .status {
    font-size: 15px;
    color: #777777;
}
#menu-checkbox,
#menu-toggle {
    display: none;
}

css/mobile.css

.side-bar{
    position: relative;
    width: 100%;
    float: none;
}

.main{
    width: 100%;
    padding-left: 10px;
    padding-right: 10px;
}

.artical-list {
    margin-right: 0%;
}

.side-bar .nav,
.side-bar .tag {
    display: none;
    text-align: center;
}

#menu-checkbox:checked ~ .nav,
#menu-checkbox:checked ~ .tag {
    display: block;
}

#menu-toggle {
    display: block;
    position: absolute;
    top: 10px;
    right: 10px;;
    font-size: 20px;
    font-weight: 200;
}

猜你喜欢

转载自blog.csdn.net/TalonZhang/article/details/82928263