如何使用 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.