CSS3 -- column 实现瀑布流布局

本例使用 CSS column 实现瀑布流布局

关键点,column-count: 元素内容将被划分的最佳列数

关键点,break-inside: 避免在元素内部插入分页符

html

div.g-container
    -for(var j = 0; j<32; j++)
        div.g-item

scss

$count: 32;

@function randomNum($max, $min: 0, $u: 1) {
	@return ($min + random($max)) * $u;
}

@function randomColor() {
    @return rgb(randomNum(255), randomNum(255), randomNum(255));
}

.g-container {
    column-count: 4;
    column-gap: .5vw;
    padding-top: .5vw;
}

.g-item {
    position: relative;
     24vw;
    margin-bottom: 1vw;
    break-inside: avoid;
}

@for $i from 1 to $count+1 {
    .g-item:nth-child(#{$i}) {
        height: #{randomNum(300, 50)}px;
        background: randomColor();

        &::after {
            content: "#{$i}";
            position: absolute;
            color: #fff;
            font-size: 2vw;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
        }
    }    
}

感谢公总号:web前端开发

原文地址:https://www.cnblogs.com/lisaShare/p/10911982.html