如何建立一個適用於所有裝置(PC、筆記型電腦、平板電腦和手機)的響應式網站?
響應式網站可以在任何裝置上執行,無論是桌上型電腦、平板電腦還是手機。為了設定響應性,我們使用媒體查詢。
建立頁首容器
為頁首建立一個div
<div class="header"> <h1>Website ↶</h1> </div>
設定頁首樣式
使用text-align屬性將頁首文字居中 −
.header {
padding: 80px;
text-align: center;
background: #7b1abc;
color: white;
}
建立導航選單
<nav>用於建立導航選單 −
<nav> <a class="links selected" href="#"> Home</a> <a class="links" href="#"> Login</a> <a class="links" href="#"> Register</a> <a class="links" href="#"> Contact Us</a> <a class="links" href="#">More Info</a> </nav>
建立正文內容
使用<main>並放置您希望在網站上顯示的文字 −
<main>
<h2>Sample Heading</h2>
<h5>Published in January</h5>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eum, autem.
</p>
<br />
<h2>Sample Heading</h2>
<h5>Published in march</h5>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Unde quo minus fugiat tempora quae ratione dignissimos exercitationem voluptate officiis reiciendis.
</p>
</main>
建立頁尾
頁尾位於底部。版權文字也可以在頁尾中看到 −
<div class="footer"> <h2>Footer ↷</h2> </div>
設定頁尾樣式
使用text-align屬性和center值將頁尾文字居中 −
.footer {
padding: 20px;
text-align: center;
color: white;
background: rgb(255, 0, 0);
}
設定響應性
媒體查詢用於設定響應性。當螢幕尺寸小於830px時,寬度設定為100%。此外,連結的display屬性值設定為block −
@media screen and (max-width: 830px) {
div, main {
width: 100%;
text-align: center;
}
.links {
display: block;
}
示例
要建立一個適用於所有裝置的響應式網站,程式碼如下所示:
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<style>
* {
box-sizing: border-box;
}
body {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
margin: 0;
}
.header {
padding: 80px;
text-align: center;
background: #7b1abc;
color: white;
}
.header h1 {
font-size: 40px;
}
nav {
width: 100%;
background-color: rgb(39, 39, 39);
overflow: auto;
height: auto;
}
.links {
display: inline-block;
text-align: center;
padding: 14px;
color: rgb(178, 137, 253);
text-decoration: none;
font-size: 17px;
}
.links:hover {
background-color: rgb(100, 100, 100);
}
.selected {
background-color: rgb(0, 18, 43);
}
main {
background-color: white;
padding: 20px;
}
.footer {
padding: 20px;
text-align: center;
color: white;
background: rgb(255, 0, 0);
}
@media screen and (max-width: 830px) {
div, main {
width: 100%;
text-align: center;
}
.links {
display: block;
}
}
</style>
</head>
<body>
<div class="header">
<h1>Website ↶</h1>
</div>
<nav>
<a class="links selected" href="#"> Home</a>
<a class="links" href="#"> Login</a>
<a class="links" href="#"> Register</a>
<a class="links" href="#"> Contact Us</a>
<a class="links" href="#">More Info</a>
</nav>
<main>
<h2>Sample Heading</h2>
<h5>Published in January</h5>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eum, autem.
</p>
<br />
<h2>Sample Heading</h2>
<h5>Published in march</h5>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Unde quo minus fugiat tempora quae ratione dignissimos exercitationem voluptate officiis reiciendis.
</p>
</main>
<div class="footer">
<h2>Footer ↷</h2>
</div>
</body>
</html>
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP