1. 程式人生 > >響應式部落格頁面

響應式部落格頁面

記錄一個自己寫的簡單部落格頁面的程式碼,使用 CSS Media Query 實現響應式佈局。原理就是根據使用者螢幕尺寸來匹配不同的 CSS 樣式。程式碼如下:

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;
}