溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

純css如何實現(xiàn)瀑布流

發(fā)布時間:2021-03-19 11:03:48 來源:億速云 閱讀:286 作者:小新 欄目:web開發(fā)

這篇文章主要介紹了純css如何實現(xiàn)瀑布流,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

1.multi-column多列布局實現(xiàn)瀑布流

先簡單的講下multi-column相關(guān)的部分屬性

column-count設(shè)置列數(shù)column-gap設(shè)置列與列之間的間距column-width設(shè)置每列的寬度

還要結(jié)合在子容器中設(shè)置break-inside防止多列布局,分頁媒體和多區(qū)域上下文中的意外中斷

break-inside屬性值

  • auto 指定既不強制也不禁止元素內(nèi)的頁/列中斷。

  • avoid 指定避免元素內(nèi)的分頁符。

  • avoid-page 指定避免元素內(nèi)的分頁符。

  • avoid-column 指定避免元素內(nèi)的列中斷。

  • avoid-region 指定避免元素內(nèi)的區(qū)域中斷。

截取了部分,可自己填充

/* html文件 */
<!-- 使用multi-columns實現(xiàn)瀑布流 -->
<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;
     /* 防止多列布局,分頁媒體和多區(qū)域上下文中的意外中斷 */
     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);
}

純css如何實現(xiàn)瀑布流


純css如何實現(xiàn)瀑布流

2.flex布局實現(xiàn)瀑布流將外層設(shè)置為row布局,然后再設(shè)置一個容器并設(shè)置為column布局,它是將列作為一個整體,然后在對列進行劃分,在列里進行寬固定來實現(xiàn)的

/* 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如何實現(xiàn)瀑布流

實踐后發(fā)現(xiàn),純css實現(xiàn)的瀑布流只能是一列一列的排布,所以還是得用js來實現(xiàn)瀑布流更符合我們常見的瀑布流

感謝你能夠認真閱讀完這篇文章,希望小編分享的“純css如何實現(xiàn)瀑布流”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

css
AI