详解纯css完成瀑布流(multi

日期:2021-03-01 类型:科技新闻 

关键词:微信公众号平台小程序,微信打分小程序,小程序发布,小程序大全,微信小程序编程

瀑布流的合理布局自身觉得還是很吸引住人的,近期又看到完成瀑布流这个做法,在这里纪录下,非常的,觉得flex合理布局完成瀑布流還是有点懵的模样,但是如今便可以搞清楚它的基本原理了

1.multi-column两列合理布局完成瀑布流

先简易的讲下multi-column有关的一部分特性

column-count设定列数column-gap设定列与列之间的间隔column-width设定每列的宽度

还要融合在子器皿中设定break-inside避免两列合理布局,分页查询新闻媒体和多地区左右文中的出现意外终断

break-inside特性值

  • auto 特定既不强制性也禁不住止元素内的页/列终断。
  • avoid 特定防止元素内的分页查询符。
  • avoid-page 特定防止元素内的分页查询符。
  • avoid-column 特定防止元素内的列终断。
  • avoid-region 特定防止元素内的地区终断。

截取了一部分,可自身填充

/* html文档 */
<!-- 应用multi-columns完成瀑布流 -->
<div id="root">
    <div class="item">
        <img class="itemImg" src="../images/1.jpeg" alt=""/>
        <div class="userInfo">
            <img class="avatar" src="../images/gift.png" alt=""/>
            <span class="username">牵起你的左手护着你</span>
        </div>
    </div>
    <div class="item">
        <img class="itemImg" src="../images/2.jpg" alt=""/>
        <div class="userInfo">
            <img class="avatar" src="../images/gift.png" alt=""/>
            <span class="username">牵起你的左手护着你</span>
        </div>
    </div>
    <div class="item">
        <img class="itemImg" src="../images/3.jpg" alt=""/>
        <div class="userInfo">
            <img class="avatar" src="../images/gift.png" alt=""/>
            <span class="username">牵起你的左手护着你</span>
        </div>
    </div>
    <div class="item">
        <img class="itemImg" src="../images/4.jpg" alt=""/>
        <div class="userInfo">
            <img class="avatar" src="../images/gift.png" alt=""/>
            <span class="username">牵起你的左手护着你</span>
        </div>
    </div>
    <div class="item">
        <img class="itemImg" src="../images/5.jpeg" alt=""/>
        <div class="userInfo">
            <img class="avatar" src="../images/gift.png" alt=""/>
            <span class="username">牵起你的左手护着你</span>
        </div>
    </div>
</div>
/* css款式 */
body {
    background: #e5e5e5;
}
/* 瀑布流最外层 */
#root {
     margin: 0 auto;
     width: 1200px;
     column-count: 5;
     column-width: 240px;
     column-gap: 20px;
}
/* 每列照片包括层 */
.item {
     margin-bottom: 10px;
     /* 避免两列合理布局,分页查询新闻媒体和多地区左右文中的出现意外终断 */
     break-inside: avoid;
     background: #fff;
}
.item:hover {
     box-shadow: 2px 2px 2px rgba(0, 0, 0, .5);
}
/* 照片 */
.itemImg {
     width: 100%;
     vertical-align: middle;
}
/* 照片下的信息内容包括层 */
.userInfo {
     padding: 5px 10px;
}
.avatar {
     vertical-align: middle;
     width: 30px;
     height: 30px;
     border-radius: 50%;
}
.username {
     margin-left: 5px;
     text-shadow: 2px 2px 2px rgba(0, 0, 0, .3);
}


2.flex合理布局完成瀑布流将外层设定为row合理布局,随后再设定1个器皿并设定为column合理布局,它是将列做为1个总体,随后在对列开展区划,在列里开展宽固定不动来完成的

 

/* html文档(只截取多列合理布局)*/
<div id="root">
    <div class="itemContainer">
        <div class="item">
            <img class="itemImg" src="../images/1.jpeg" alt=""/>
            <div class="userInfo">
                <img class="avatar" src="../images/gift.png" alt=""/>
                <span class="username">牵起你的左手护着你</span>
            </div>
        </div>
        <div class="item">
            <img class="itemImg" src="../images/2.jpg" alt=""/>
            <div class="userInfo">
                <img class="avatar" src="../images/gift.png" alt=""/>
                <span class="username">牵起你的左手护着你</span>
            </div>
        </div>
        <div class="item">
            <img class="itemImg" src="../images/3.jpg" alt=""/>
            <div class="userInfo">
                <img class="avatar" src="../images/gift.png" alt=""/>
                <span class="username">牵起你的左手护着你</span>
            </div>
        </div>
        <div class="item">
            <img class="itemImg" src="../images/4.jpg" alt=""/>
            <div class="userInfo">
                <img class="avatar" src="../images/gift.png" alt=""/>
                <span class="username">牵起你的左手护着你</span>
            </div>
        </div>
        <div class="item">
            <img class="itemImg" src="../images/5.jpeg" alt=""/>
            <div class="userInfo">
                <img class="avatar" src="../images/gift.png" alt=""/>
                <span class="username">牵起你的左手护着你</span>
            </div>
        </div>
        <div class="item">
            <img class="itemImg" src="../images/6.jpeg" alt=""/>
            <div class="userInfo">
                <img class="avatar" src="../images/gift.png" alt=""/>
                <span class="username">牵起你的左手护着你</span>
            </div>
        </div>
        <div class="item">
            <img class="itemImg" src="../images/7.jpeg" alt=""/>
            <div class="userInfo">
                <img class="avatar" src="../images/gift.png" alt=""/>
                <span class="username">牵起你的左手护着你</span>
            </div>
        </div>
    </div>
    <div class="itemContainer">
        <div class="item">
            <img class="itemImg" src="../images/5.jpeg" alt=""/>
            <div class="userInfo">
                <img class="avatar" src="../images/gift.png" alt=""/>
                <span class="username">牵起你的左手护着你</span>
            </div>
        </div>
        <div class="item">
            <img class="itemImg" src="../images/7.jpeg" alt=""/>
            <div class="userInfo">
                <img class="avatar" src="../images/gift.png" alt=""/>
                <span class="username">牵起你的左手护着你</span>
            </div>
        </div>
        <div class="item">
            <img class="itemImg" src="../images/6.jpeg" alt=""/>
            <div class="userInfo">
                <img class="avatar" src="../images/gift.png" alt=""/>
                <span class="username">牵起你的左手护着你</span>
            </div>
        </div>
        <div class="item">
            <img class="itemImg" src="../images/5.jpeg" alt=""/>
            <div class="userInfo">
                <img class="avatar" src="../images/gift.png" alt=""/>
                <span class="username">牵起你的左手护着你</span>
            </div>
        </div>
        <div class="item">
            <img class="itemImg" src="../images/6.jpeg" alt=""/>
            <div class="userInfo">
                <img class="avatar" src="../images/gift.png" alt=""/>
                <span class="username">牵起你的左手护着你</span>
            </div>
        </div>
        <div class="item">
            <img class="itemImg" src="../images/6.jpeg" alt=""/>
            <div class="userInfo">
                <img class="avatar" src="../images/gift.png" alt=""/>
                <span class="username">牵起你的左手护着你</span>
            </div>
        </div>
    </div>
</div>
/* css文档 */
body{
   background: #e5e5e5;
}
#root{
    display: flex;
    flex-direction: row;
    margin: 0 auto;
    width: 1200px;
}
.itemContainer{
    margin-right: 10px;
    flex-direction: column;
    width: 240px;
}
.item{
   margin-bottom: 10px;
   background: #fff;
}
.itemImg{
   width: 100%;
}
.userInfo {
   padding: 5px 10px;
}
.avatar {
   vertical-align: middle;
   width: 30px;
   height: 30px;
   border-radius: 50%;
}
.username {
   margin-left: 5px;
   text-shadow: 2px 2px 2px rgba(0, 0, 0, .3);
}

实践活动后发现,纯css完成的瀑布流只能是1列1列的排布,因此還是得用js来完成瀑布流更合乎大家普遍的瀑布流

以上便是本文的所有內容,期待对大伙儿的学习培训有一定的协助,也期待大伙儿多多适用脚本制作之家。