css 瀑布流布局

2023-10-12  本文已影响0人  暴躁程序员

一、实现瀑布流

  1. 通过外层容器的 width 固定宽、 column-count 列数、column-gap 列间距属性实现瀑布流
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>瀑布流</title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      html,
      body {
        height: 100%;
      }
      .container {
        width: 300px;
        column-count: 3; 
        column-gap: 20px;
      }
      .item {
        break-inside: avoid;
        width: 100px;
        margin-bottom: 10px;
        background-color: yellowgreen;
      }
      .item > img {
        width: 100px;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <div class="item">
        <img src="./images/1.png" alt="" />
        <h1>标题</h1>
        <div>情详情详情详情详情详情</div>
      </div>
      <div class="item">
        <img src="./images/2.png" alt="" />
        <h1>标题</h1>
        <div>情详情详情详情详情详情</div>
      </div>
      <div class="item">
        <img src="./images/3.png" alt="" />
        <h1>标题</h1>
        <div>情详情详情详情详情详情</div>
      </div>
      <div class="item">
        <img src="./images/4.png" alt="" />
        <h1>标题</h1>
        <div>情详情详情详情详情详情</div>
      </div>
      <div class="item">
        <img src="./images/4.png" alt="" />
        <h1>标题</h1>
        <div>情详情详情详情详情详情</div>
      </div>
      <div class="item">
        <img src="./images/2.png" alt="" />
        <h1>标题</h1>
        <div>情详情详情详情详情详情</div>
      </div>
    </div>
  </body>
  <script></script>
</html>
上一篇下一篇

猜你喜欢

热点阅读