gpt4 book ai didi

html - 使网格元素自动填充空白

转载 作者:太空宇宙 更新时间:2023-11-04 05:49:27 25 4
gpt4 key购买 nike

我有以下 CSS 网格系统,它有 4 种不同的单元格大小:小,宽,高,大。在桌面版本中,我将它们制作成拼图形状,看起来不错,但为了使其适合移动设备,我需要类为 small 的单元格自动填充出现的空白区域。

这是CSS:

.portfolio-holder {
margin: 66px auto;
display: flex;
flex-wrap: wrap;
}

.portfolio {
margin: auto;
display: grid;
height: 100%;
grid-template-rows: repeat(13, 200px);
grid-template-columns: repeat(4, 200px);
grid-gap: 10px;
padding: 24px;
grid-auto-rows: minmax(100px, auto);
}

.portfolio_hat {
background: #fd3a1e;
overflow: hidden;
color: black;
font-weight: 14px;
display: flex;
justify-content: center;
align-items: center;
font-size: 1.5em;
background-size: cover;
background-position: top left;
position: relative;
}

.portfolio_hat.tall {
grid-row: span 2;
grid-column: span 1;
}

.portfolio_hat.wide {
grid-row: span 1;
grid-column: span 2;
}

.portfolio_hat.small {
grid-row: span 1;
grid-column: span 1;
}

.portfolio_hat.tall {
grid-row: span 2;
grid-column: span 2;
}

@media (max-width:860px) {
.portfolio {
grid-template-rows: repeat(26, 40vw);
grid-template-columns: repeat(2, 40vw);
}
}

HTML 看起来像这样:

<section class="portfolio-holder">
<div id="port" class="portfolio">
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<!-- divs like the four above are generated from MYSQL DATABASE and there's a lot of them in a chaotic order -->
</div>
</section>

我想要一种方法来确保无论网格中的列数如何,我都不会得到任何空白。

谢谢

最佳答案

看看https://developer.mozilla.org/en-US/docs/Web/CSS/grid-auto-flow

The grid-auto-flow CSS property controls how the auto-placement algorithm works, specifying exactly how auto-placed items get flowed into the grid.

.portfolio-holder {
margin: 66px auto;
display: flex;
flex-wrap: wrap;
}

.portfolio {
margin: auto;
display: grid;
height: 100%;
grid-template-rows: repeat(13, 200px);
grid-template-columns: repeat(4, 200px);
grid-gap: 10px;
padding: 24px;
grid-auto-rows: minmax(100px, auto);
grid-auto-flow: row dense;/* here your CSS update */
}
.portfolio_hat {
background: #fd3a1e;
overflow: hidden;
color: black;
font-weight: 14px;
display: flex;
justify-content: center;
align-items: center;
font-size: 1.5em;
background-size: cover;
background-position: top left;
position: relative;
}

.portfolio_hat.tall {
grid-row: span 2;
grid-column: span 1;
}

.portfolio_hat.wide {
grid-row: span 1;
grid-column: span 2;
}

.portfolio_hat.small {
grid-row: span 1;
grid-column: span 1;
}

.portfolio_hat.tall {
grid-row: span 2;
grid-column: span 2;
}

@media (max-width:860px) {
.portfolio {
grid-template-rows: repeat(26, 40vw);
grid-template-columns: repeat(2, 40vw);
}
}
div {/* see them for demo */
border: solid 1px;
padding: 1em;
margin: 2px;
}
<section class="portfolio-holder">
<div id="port" class="portfolio">
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<div href="#" class="portfolio_hat tall"></div>
<div href="#" class="portfolio_hat small"></div>
<div href="#" class="portfolio_hat wide"></div>
<div href="#" class="portfolio_hat large"></div>
<!-- divs like the four above are generated from MYSQL DATABASE and there's a lot of them in a chaotic order -->
</div>
</section>

关于html - 使网格元素自动填充空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58399648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com