CodeGym/Adesua ahorow/Frontend SELF TW/建立複雜的佈局

建立複雜的佈局

開放

10.1 兩欄佈局

在網頁上創建複雜的佈局需要深入了解各種定位方法及其組合。 在這一節中,我們會看看使用不同CSS技術建立複雜佈局的一些實際例子, 比如Flexbox、Grid和傳統定位。

具有固定導航的博客佈局

這個佈局包括標題、固定導航欄、主要內容和側邊欄。

範例:

CSS
body {
  margin: 0;
  font-family: Arial, sans-serif;
}

.header {
  background-color: #333;
  color: white;
  padding: 20px;
  text-align: center;
}

.navbar {
  position: fixed;
  top: 0;
  width: 100%;
  background-color: #444;
  color: white;
  padding: 10px;
  box-sizing: border-box;
  z-index: 1000;
}

.container {
  display: flex;
  margin-top: 60px; /* 固定導航的高度 */
}

.main-content {
  flex: 3;
  padding: 20px;
}

.sidebar {
  flex: 1;
  padding: 20px;
  background-color: #f4f4f4;
}

.footer {
  background-color: #333;
  color: white;
  padding: 20px;
  text-align: center;
  position: relative;
  margin-top: auto;
}
HTML
<div class="header">My Blog</div>
<div class="navbar">Navigation</div>
<div class="container">
  <div class="main-content">
    <h1>Main Content</h1>
    <p>Here is the main content of the blog.</p>
  </div>

  <div class="sidebar">
    <h2>Sidebar</h2>
    <p>Links and other content.</p>
  </div>
</div>
<div class="footer">Footer</div>

在這個例子中,固定導航欄在滾動頁面時會保持不動,這是因為position: fixed。 主要內容和側邊欄使用Flexbox布局在兩列中顯示。

10.2 單頁面網站

具有粘性標題和頁腳的單頁網站

這個佈局包括標題、主要內容和頁腳。標題和頁腳在滾動頁面時保持可見。

範例:

CSS
body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  margin: 0;
  font-family: Arial, sans-serif;
}

.header {
  background-color: #333;
  color: white;
  padding: 20px;
  text-align: center;
  position: sticky;
  top: 0;
  z-index: 1000;
}

.main {
  flex: 1;
  padding: 20px;
  background-color: #f4f4f4;
}

.footer {
  background-color: #333;
  color: white;
  padding: 20px;
  text-align: center;
  position: sticky;
  bottom: 0;
  z-index: 1000;
}
HTML
<div class="header">Sticky Header</div>
<div class="main">
  <h1>Main Content</h1>
  <p>Here is the main content of the page. Scroll to see the sticky header and footer in action.</p>
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque luctus lectus eu tortor vehicula, et convallis lacus varius. Integer at orci in nisl faucibus placerat.</p>
</div>
<div class="footer">Sticky Footer</div>

在這個例子中,標題和頁腳在滾動頁面時保持可見,這是因為position: sticky

10.3 多層次導航欄

這個佈局包括一個多層次的導航欄,它使用嵌套列表和偽類來創建下拉菜單。

範例:

CSS
body {
  margin: 0;
  font-family: Arial, sans-serif;
}

.navbar {
  background-color: #333;
  overflow: hidden;
}

.navbar ul {
  list-style-type: none;
  padding: 0;
  margin: 0;
  position: relative;
}

.navbar li {
  float: left;
}

.navbar li a {
  display: block;
  color: white;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
}

.navbar li a:hover {
  background-color: #111;
}

.navbar li ul {
  display: none;
  position: absolute;
  background-color: #333;
}

.navbar li:hover ul {
  display: block;
}

.navbar li ul li {
  float: none;
}

.navbar li ul li a {
  padding: 14px 16px;
}
HTML
<div class="navbar">
  <ul>
    <li><a href="#">Home</a></li>
    <li><a href="#">Services</a>
      <ul>
        <li><a href="#">Web Design</a></li>
        <li><a href="#">SEO</a></li>
        <li><a href="#">Marketing</a></li>
      </ul>
    </li>
    <li><a href="#">About</a></li>
    <li><a href="#">Contact</a></li>
  </ul>
</div>
1
任務
Frontend SELF TW,  等級 22課堂 4
上鎖
雙欄佈局
雙欄佈局
1
任務
Frontend SELF TW,  等級 22課堂 4
上鎖
單頁網站
單頁網站
留言
  • 受歡迎
你必須登入才能留言
此頁面尚無留言