1. 程式人生 > 實用技巧 >css頁面佈局

css頁面佈局

 Css頁面佈局:

  一:頭部區域:

<!--1.消除body與頁面之間的間隙:body{padding:0px;margin:0px;}-->
<!--2.消除body與div之間的間隙:div{position:absolute;padding:0px}-->

  二:導航區域:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>CSS 網頁佈局</title>
<meta name="viewport" content
="width=device-width, initial-scale=1"> <style> * { box-sizing: border-box; } ​ body { margin: 0; }/* 頭部樣式 */ .header { background-color: #f1f1f1; padding: 20px; text-align: center; }/* 導航條 */ .topnav { overflow: hidden; background-color: #333; } /* 導航連結 */ .topnav a { float
: left; display: block; color: #f2f2f2; text-align: center; padding: 14px 16px; text-decoration: none; } /* 連結 - 修改顏色 */ .topnav a:hover { background-color: #ddd; color: black; } </style> </head> <body><div class="header"> <h1>頭部區域</h1> </div>
<div class="topnav"> <a href="#">連結</a> <a href="#">連結</a> <a href="#">連結</a> </div></body> </html>

三:內容區域:

  • 1 列:一般用於移動端

  • 2 列:一般用於平板裝置

  • 3 列:一般用於 PC 桌面裝置

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>CSS 網頁佈局</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {
  box-sizing: border-box;
}
​
body {
  margin: 0;
}/* 頭部樣式 */
.header {
  background-color: #f1f1f1;
  padding: 20px;
  text-align: center;
}/* 導航條 */
.topnav {
  overflow: hidden;
  background-color: #333;
}/* 導航連結 */
.topnav a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}/* 連結 - 修改顏色 */
.topnav a:hover {
  background-color: #ddd;
  color: black;
}/* 建立三個相等的列 */
.column {
  float: left;
  width: 33.33%;
  border:1px solid red;
  overflow:auto;
}
 
/* 列後清除浮動 */
.row:after {
  content: "";
  display: table;
  clear: both;
}
 
/* 響應式佈局 - 小於 600 px 時改為上下佈局 */
@media screen and (max-width: 600px) {
  .column {
    width: 100%;
  }
}
</style>
</head>
<body><div class="header">
  <h1>頭部區域</h1>
  <p>重置瀏覽器大小檢視效果。</p>
</div><div class="topnav">
  <a href="#">連結</a>
  <a href="#">連結</a>
  <a href="#">連結</a>
</div><div class="row">
  <div class="column">
    <h2>第一列</h2>
    <p>xxxxxxxxxxxxxxxxxxx</p>
  </div>
  
  <div class="column">
    <h2>第二列</h2>
    <p>xxxxxxxxxxxxxxxxxxx</p>
  </div>
  
  <div class="column">
    <h2>第三列</h2>
    <p>xxxxxxxxxxxxxxxxxxx</p>
  </div>
</div></body>
</html>

  四:底部區域:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>CSS 網頁佈局</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* {
  box-sizing: border-box;
}
​
body {
  margin: 0;
}/* 頭部樣式 */
.header {
  background-color: #f1f1f1;
  padding: 20px;
  text-align: center;
}/* 導航條 */
.topnav {
  overflow: hidden;
  background-color: #333;
}/* 導航連結 */
.topnav a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}/* 連結 - 修改顏色 */
.topnav a:hover {
  background-color: #ddd;
  color: black;
}/* 建立三個相等的列 */
.column {
  float: left;
  padding: 10px;
}/* 左右兩側寬度 */
.column.side {
  width: 25%;
}/* 中間區域寬度 */
.column.middle {
  width: 50%;
}/* 列後面清除浮動 */
.row:after {
  content: "";
  display: table;
  clear: both;
}/* 響應式佈局 - 寬度小於600px時設定上下佈局 */
@media screen and (max-width: 600px) {
  .column.side, .column.middle {
    width: 100%;
  }
}
​
/* 底部樣式 */
.footer {
  background-color: #f1f1f1;
  padding: 10px;
  text-align: center;
}
</style>
</head>
<body><div class="header">
  <h1>頭部區域</h1>
  <p>重置瀏覽器大小檢視效果。</p>
</div><div class="topnav">
  <a href="#">連結</a>
  <a href="#">連結</a>
  <a href="#">連結</a>
</div><div class="row">
  <div class="column side">
    <h2>左側欄</h2>
    <p>xxxxxxxxxxxxxxx</p>
  </div>
  
  <div class="column middle">
    <h2>主區域內容</h2>
    <p>xxxxxxxxxxxxxxxx</p>
    <p>xxxxxxxxxxxxxxxxxx</p>
  </div>
  
  <div class="column side">
    <h2>右側欄</h2>
    <p>xxxxxxxxxxxxxxxxxx!</p>
  </div>
</div><div class="footer">
  <p>底部區域</p>
</div>
  
</body>
</html>

五:整體佈局:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>ass</title>
<style>
* {
  box-sizing: border-box;
}
 
body {
  font-family: Arial;
  padding: 10px;
  background: #f1f1f1;
}
 
/* 頭部標題 */
.header {
  padding: 30px;
  text-align: center;
  background: white;
}
 
.header h1 {
  font-size: 50px;
}
 
/* 導航條 */
.topnav {
  overflow: hidden;
  background-color: #333;
}
 
/* 導航條連結 */
.topnav a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}
 
/* 連結顏色修改 */
.topnav a:hover {
  background-color: #ddd;
  color: black;
}
 
/* 建立兩列 */
/* Left column */
.leftcolumn {   
  float: left;
  width: 75%;
}
 
/* 右側欄 */
.rightcolumn {
  float: left;
  width: 25%;
  background-color: #f1f1f1;
  padding-left: 20px;
}
 
/* 影象部分 */
.fakeimg {
  background-color: #aaa;
  width: 100%;
  padding: 20px;
}
 
/* 文章卡片效果 */
.card {
  background-color: white;
  padding: 20px;
  margin-top: 20px;
}
 
/* 列後面清除浮動 */
.row:after {
  content: "";
  display: table;
  clear: both;
}
 
/* 底部 */
.footer {
  padding: 20px;
  text-align: center;
  background: #ddd;
  margin-top: 20px;
}
 
/* 響應式佈局 - 螢幕尺寸小於 800px 時,兩列布局改為上下佈局 */
@media screen and (max-width: 800px) {
  .leftcolumn, .rightcolumn {   
    width: 100%;
    padding: 0;
  }
}
 
/* 響應式佈局 -螢幕尺寸小於 400px 時,導航等佈局改為上下佈局 */
@media screen and (max-width: 400px) {
  .topnav a {
    float: none;
    width: 100%;
  }
}
</style>
</head>
<body><div class="header">
  <h1>我的網頁</h1>
  <p>重置瀏覽器大小檢視效果。</p>
</div><div class="topnav">
  <a href="#">連結</a>
  <a href="#">連結</a>
  <a href="#">連結</a>
  <a href="#" style="float:right">連結</a>
</div><div class="row">
  <div class="leftcolumn">
    <div class="card">
      <h2>文章標題</h2>
      <h5>2019 年 4 月 17日</h5>
      <div class="fakeimg" style="height:200px;">圖片</div>
      <p>一些文字...</p>
      <p>xxxxxxxxxxxxxxxxxxx</p>
    </div>
    <div class="card">
      <h2>文章標題</h2>
      <h5>2019 年 4 月 17日</h5>
      <div class="fakeimg" style="height:200px;">圖片</div>
      <p>一些文字...</p>
      <p>xxxxxxxxxxxxxxxxxxx</p>
    </div>
  </div>
  <div class="rightcolumn">
    <div class="card">
      <h2>關於我</h2>
      <div class="fakeimg" style="height:100px;">圖片</div>
      <p>關於我的一些資訊..</p>
    </div>
    <div class="card">
      <h3>熱門文章</h3>
      <div class="fakeimg"><p>圖片</p></div>
      <div class="fakeimg"><p>圖片</p></div>
      <div class="fakeimg"><p>圖片</p></div>
    </div>
    <div class="card">
      <h3>關注我</h3>
      <p>一些文字...</p>
    </div>
  </div>
</div><div class="footer">
  <h2>底部區域</h2>
</div></body>
</html>