gpt4 book ai didi

html - 使用 float 项的 CSS 网格

转载 作者:行者123 更新时间:2023-11-27 23:52:01 27 4
gpt4 key购买 nike

我正在尝试实现一个简单的网格,每行有 3 个正方形项,每个项最多可以有 3 行/列。这是我到目前为止所拥有的:

https://jsfiddle.net/dt3u6840/

HTML:

<div class="layout-grid">
<a href="#" class="block" data-rows="2" data-cols="2"></a>
<a href="#" class="block" data-rows="1" data-cols="1"></a>
<a href="#" class="block" data-rows="1" data-cols="1"></a>
<a href="#" class="block" data-rows="1" data-cols="1"></a>
<a href="#" class="block" data-rows="1" data-cols="1"></a>
<a href="#" class="block" data-rows="1" data-cols="1"></a>
<a href="#" class="block" data-rows="1" data-cols="3"></a>
</div>

CSS:

.layout-grid { overflow: hidden; max-width: 800px; width: 100%; }
.layout-grid .block { position: relative; display: block; float: left; background-color: green; height: auto; transition: all .3s ease; }
.layout-grid .block:hover { opacity: .2; }
.layout-grid .block:nth-child(odd) { background-color: blue; }
.layout-grid .block[data-cols="1"] { width: 33.33333333333%; }
.layout-grid .block[data-cols="2"] { width: 66.66666666666%; }
.layout-grid .block[data-cols="3"] { width: 100%; }
.layout-grid .block[data-rows="1"] { padding-top: 33.33333333333%; }
.layout-grid .block[data-rows="2"] { padding-top: 66.66666666666%; }
.layout-grid .block[data-rows="3"] { padding-top: 100%; }

这很好用。但是,如果我尝试添加一个超过 1 行的元素,它不会:

https://jsfiddle.net/pz4vhjw6/

新的 HTML:

<div class="layout-grid">
<a href="#" class="block" data-rows="2" data-cols="2"></a>
<a href="#" class="block" data-rows="1" data-cols="1"></a>
<a href="#" class="block" data-rows="1" data-cols="1"></a>
<a href="#" class="block" data-rows="1" data-cols="1"></a>
<a href="#" class="block" data-rows="1" data-cols="1"></a>
<a href="#" class="block" data-rows="2" data-cols="1"></a>
<a href="#" class="block" data-rows="1" data-cols="1"></a>
<a href="#" class="block" data-rows="1" data-cols="1"></a>
</div>

有什么我可以做的吗?

最佳答案

您可以使用 CSS Grid layout此处 - 并非仅在现代浏览器中受支持(参见 support info )。在这里,您可以使用 3 列布局并跨越行和/或列以获得所需的布局 - 请参阅下面的两种布局演示:

.layout-grid {
display: grid;
grid-template-columns: 1fr 1fr 1fr; /* 3 columns */
width: 100%;
max-width: 800px;
}

.layout-grid .block {
background-color: green;
transition: all .3s ease;
padding-top: 100%; /* set height of block */
}

.layout-grid .block:hover {
opacity: .2;
}

.layout-grid .block:nth-child(odd) {
background-color: blue;
}

.layout-grid .block[data-cols="2"] {
grid-column: span 2; /* occupy 2 columns */
grid-row: span 2; /* occupy 2 rows */
}

.layout-grid .block[data-cols="3"] {
grid-column: span 3; /* occupy 3 columns */
grid-row: span 3; /* occupy 3 rows */
}

.layout-grid .block[data-rows="2"] {
grid-row: span 2; /* occupy 2 rows */
}

.layout-grid .block[data-rows="3"] {
grid-row: span 3; /* occupy 3 rows */
}
<div class="layout-grid">
<a href="#" class="block" data-rows="2" data-cols="2"></a>
<a href="#" class="block" data-rows="1" data-cols="1"></a>
<a href="#" class="block" data-rows="1" data-cols="1"></a>
<a href="#" class="block" data-rows="1" data-cols="1"></a>
<a href="#" class="block" data-rows="1" data-cols="1"></a>
<a href="#" class="block" data-rows="1" data-cols="1"></a>
<a href="#" class="block" data-rows="1" data-cols="3"></a>
</div>

<br/>

<div class="layout-grid">
<a href="#" class="block" data-rows="2" data-cols="2"></a>
<a href="#" class="block" data-rows="1" data-cols="1"></a>
<a href="#" class="block" data-rows="1" data-cols="1"></a>
<a href="#" class="block" data-rows="1" data-cols="1"></a>
<a href="#" class="block" data-rows="1" data-cols="1"></a>
<a href="#" class="block" data-rows="2" data-cols="1"></a>
<a href="#" class="block" data-rows="1" data-cols="1"></a>
<a href="#" class="block" data-rows="1" data-cols="1"></a>
</div>

关于html - 使用 float 项的 CSS 网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56465898/

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