如何创建:博客布局

学习如何使用 CSS 创建响应式博客布局。

学习如何创建响应式博客布局,该布局根据屏幕宽度在两列和全宽列之间变化。

请调整浏览器窗口大小,以查看响应效果:

Try it yourself

如何创建博客布局

第一步 - 添加 HTML:

<div class="header">
  <h2>Blog Name</h2>
</div>
<div class="row">
  <div class="leftcolumn">
    <div class="card">
      <h2>TITLE HEADING</h2>
      <h5>Title description, Dec 7, 2017</h5>
      <div class="fakeimg" style="height:200px;">Image</div>
      <p>Some text..</p>
    </div>
    <div class="card">
      <h2>TITLE HEADING</h2>
      <h5>Title description, Sep 2, 2017</h5>
      <div class="fakeimg" style="height:200px;">Image</div>
      <p>Some text..</p>
    </div>
  </div>
  <div class="rightcolumn">
    <div class="card">
      <h2>About Me</h2>
      <div class="fakeimg" style="height:100px;">Image</div>
      <p>Some text about me in culpa qui officia deserunt mollit anim.</p>
    </div>
    <div class="card">
      <h3>Popular Posts</h3>
      <div class="fakeimg">Image</div><br>
      <div class="fakeimg">Image</div><br>
      <div class="fakeimg">Image</div>
    </div>
    <div class="card">
      <h3>Follow Me</h3>
      <p>Some text..</p>
    </div>
  </div>
</div>
<div class="footer">
  <h2>Footer</h2>
</div>

Step 2 - Add CSS:

* {
  box-sizing: border-box;
}
body {
  font-family: Arial;
  padding: 20px;
  background: #f1f1f1;
}
/* Header/Blog title */
.header {
  padding: 30px;
  font-size: 40px;
  text-align: center;
  background: white;
}
/* Create two unequal columns that float against each other */
/* Left column */
.leftcolumn {
  float: left;
  width: 75%;
}
/* Right column */
.rightcolumn {
  float: left;
  width: 25%;
  padding-left: 20px;
}
/* Fake image */
.fakeimg {
  background-color: #aaa;
  width: 100%;
  padding: 20px;
}
/* Add card effect to the article */
.card {
  background-color: white;
  padding: 20px;
  margin-top: 20px;
}
/* Clear the float after the columns */
.row:after {
  content: "";
  display: table;
  clear: both;
}
/* Footer */
.footer {
  padding: 20px;
  text-align: center;
  background: #ddd;
  margin-top: 20px;
}
/* Responsive layout - When the screen width is less than 800px, stack the two columns together instead of side by side */
@media screen and (max-width: 800px) {
  .leftcolumn, .rightcolumn {
    width: 100%;
    padding: 0;
  }
}

Try it yourself

Related Pages

Tutorial:CSS Website Layout

Tutorial:CSS Responsive Web Design