如何使用 CSS 建立響應式部落格佈局?


部落格佈局包含一個頭部。該頭部包含一個徽標,然後是選單,之後是部落格標題、內容等。此外,還可以看到其他一些部落格的連結。底部是頁尾。頁尾應包含版權文字。讓我們看看如何建立部落格佈局。

建立頭部

部落格的頭部在左側包含一個徽標,然後是網站名稱 -

<div class="header">
   <h1>Logo ↶</h1>
   <h2>Website Name</h2>
</div>

定位頭部

在這裡,我們將定位徽標和網站名稱 -

.header {
   padding: 40px;	
   background: #7b1abc;
   color: white;
}
.header h1 {
   font-size: 40px;
}
.header h2 {
   text-align: center;
   font-size: 30px;
}

建立導航選單

使用 <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>

定位導航選單

屬性的 overflow 和 height 需要設定為 auto。此外,導航選單的寬度為 100% -

nav {
   width: 100%;
   background-color: rgb(39, 39, 39);
   overflow: auto;
   height: auto;
}

建立部落格頭部和主體

首先設定部落格頭部,然後是部落格的主體。主體位於 <p> 元素下 -

<div class="post">
   <div class="blogHeader">
      <h1>Blog Heading</h1>
   </div>
   <div class="blog-body">
      <p>
         Lorem ipsum dolor sit, amet consectetur adipisicing elit. Deserunt non neque eos eaque veritatis praesentium.
      </p>
   </div>
</div>

定位部落格頭部和主體

部落格頭部和主體的 div 使用 float 屬性和 left 值向左定位 -

.blog {
   margin-top: 20px;
   width: 75%;
   margin-left: auto;
   margin-right: auto;
   height: 400px;
}
.post {
   margin-top: 20px;
   float: left;
}
.blogHeader {
   font-size: 36px;
   margin-bottom: 20px;
}

定義頁尾

部落格佈局(即 HTML 網頁)的頁尾使用 <footer> 元素設定 -

<footer>
   Copyright ©
</footer>

定位頁尾

position 屬性與 fixed 值一起使用來固定頁尾。要將頁尾定位到底部,可以使用 bottom 屬性 -

footer {
   color: white;
   position: fixed;
   width: 100%;
   bottom: 0;
   margin-left: auto;
   margin-right: auto;
   font-size: 30px;
   height: 100px;
   padding: 20px;
   background-color: rgb(9, 141, 101);
   text-align: center;
}

示例

要使用 CSS 建立部落格佈局,程式碼如下 -

<!DOCTYPE html>
<html>
<head>
   <title>My Website</title>
   <style>
      * {
         box-sizing: border-box;
      }
      body {
         font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
         margin: 0;
      }
      .header {
         padding: 40px;	
         background: #7b1abc;
         color: white;
      }
      .header h1 {
         font-size: 40px;
      }
      .header h2 {
         text-align: center;
         font-size: 30px;
      }
      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);
      }
      .blog {
         margin-top: 20px;
         width: 75%;
         margin-left: auto;
         margin-right: auto;
         height: 400px;
      }
      .post {
         margin-top: 20px;
         float: left;
      }
      .blogHeader {
         font-size: 36px;
         margin-bottom: 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>Logo ↶</h1>
      <h2>Website Name</h2>
   </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>
   <div class="blog">
      <div class="post">
         <div class="blogHeader">
            <h1>Blog Heading</h1>
         </div>
         <div class="blog-body">
            <p>
               Lorem ipsum dolor sit, amet consectetur adipisicing elit. Deserunt non neque eos eaque veritatis praesentium.
            </p>
         </div>
      </div>
   </div>
   <div class="footer">
      <h2>Footer ↷</h2>
   </div>
</body>
</html>

更新於: 2023年12月8日

853 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.