我有以下CSS网格系统,具有4种不同的像元大小: 小,宽,高和大。 在桌面版本中,我使它们看起来像拼图一样,看起来不错,但要使其易于移动,我需要一个小类的单元格来自动填充出现的空白。
这是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>
我想要一种方法来确保无论网格中的列数如何,我都不会有任何空格。
谢谢
答案 0 :(得分:1)
看看https://developer.mozilla.org/en-US/docs/Web/CSS/grid-auto-flow
grid-auto-flow 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);
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>