如何創建:作品集畫廊

學習如何使用 CSS 創建響應式作品集畫廊網格。

作品集畫廊

學習如何創建響應式作品集畫廊,該畫廊根據屏幕寬度在 4 列、2 列和全寬列之間變化:

親自試一試

如何創建作品集網站

第一步 - 添加 HTML:

<!-- 主內容(居中網站) -->
<div class="main">
<h1>MYLOGO.COM</h1>
<hr>
<h2>PORTFOLIO</h2>
<p>Resize the browser window to see the responsive effect.</p>
<!-- 作品集畫廊網格 -->
<div class="row">
  <div class="column">
    <div class="content">
      <img src="beijing.jpg" alt="Beijing" style="width:100%">
      <h3>My Work</h3>
      <p>Lorem ipsum..</p>
    </div>
  </div>
  <div class="column">
    <div class="content">
      <img src="hangzhou.jpg" alt="Hangzhou" style="width:100%">
      <h3>My Work</h3>
      <p>Lorem ipsum..</p>
    </div>
  </div>
  <div class="column">
    <div class="content">
      <img src="chongqing.jpg" alt="Chongqing" style="width:100%">
      <h3>My Work</h3>
      <p>Lorem ipsum..</p>
    </div>
  </div>
  <div class="column">
    <div class="content">
      <img src="shenzhen.jpg" alt="Shenzhen" style="width:100%">
      <h3>My Work</h3>
      <p>Lorem ipsum..</p>
    </div>
  </div>
</div>
<div class="content">
  <img src="bear.jpg" alt="Bear" style="width:100%">
  <h3>Some Other Work</h3>
  <p>Lorem ipsum..</p>
</div>
<!-- 主內容結束 -->
</div>

第二步 - 添加 CSS:

* {
  box-sizing: border-box;
}
body {
  background-color: #f1f1f1;
  padding: 20px;
  font-family: Arial;
}
/* 居中網站 */
.main {
  max-width: 1000px;
  margin: auto;
}
h1 {
  font-size: 50px;
  word-break: break-all;
}
.row {
  margin: 8px -16px;
}
/* 在每列之間添加內邊距(如果需要) */
.row,
.row > .column {
  padding: 8px;
}
/* 創建四個并排的等寬列。 */
.column {
  float: left;
  width: 25%;
}
/* 清除行后的浮動 */
.row:after {
  content: "";
  display: table;
  clear: both;
}
/* 內容 */
.content {
  background-color: white;
  padding: 10px;
}
/* 響應式布局 - 制作兩列布局而不是四列 */
@media screen and (max-width: 900px) {
  .column {
    width: 50%;
  }
}
/* 響應式布局 - 使兩列堆疊,而不是并排 */
@media screen and (max-width: 600px) {
  .column {
    width: 100%;
  }
}

親自試一試

相關頁面

教程:如何創建帶過濾功能的作品集圖庫