使用 HTML 和 CSS 建立帶側欄和主要內容區域的頁面


透過將 html 和 body 的大小設定為 100%,可以建立一個具有流動側欄和主要內容區域的網頁。

以下示例對此進行了說明。

示例

 線上演示

<!DOCTYPE html>
<html>
<head>
<style>
html,body {
   height: 100%;
   color: white;
   font-size: 2em;
   line-height: 200px;
}
#parent {
   display: table;
   width: 100%;
   height: 100%;
}
#side {
   display: table-cell;
   background-color: turquoise;
   width: 20%;
   vertical-align: top;
   box-shadow: inset 0 0 10px black;
}
#main {
   display: table-cell;
   width: 80%;
   background: url("https://images.unsplash.com/photo-1611944444060- b50a1d80cfe6?crop=entropy&cs=tinysrgb&fit=crop&fm=jpg&h=800&ixlib=rb1.2.1&q=80&w=600");
}
</style>
</head>
<body>
<div id="parent">
<div id="side">Side</div>
<div id="main">Main</div>
</div>
</body>
</html>

輸出

這將產生以下結果 −

更新於:2021-3-13

2K+ 次瀏覽

開啟您的 職業 生涯

完成課程以獲得認證

開始
廣告
© . All rights reserved.