我可以用flexbox制作这个结构吗?
我在下面给出了一个带有网格的示例,但我想用 Flexbox 来制作它。
.container {
display: grid;
grid-template-columns: auto auto auto;
gap: 10px;
}
.grid-item {
background-color: aquamarine;
}
.small {
height: 200px;
}
.big {
grid-row: span 10;
}
<div class="container">
<div class="grid-item small">Cell 1</div>
<div class="grid-item small">Cell 2</div>
<div class="grid-item big">Cell 3</div>
<div class="grid-item small">Cell 4</div>
<div class="grid-item small">Cell 5</div>
<div class="grid-item small">Cell 6</div>
<div class="grid-item small">Cell 7</div>
<div class="grid-item small">Cell 8</div>
<div class="grid-item small">Cell 9</div>
<div class="grid-item small">Cell 10</div>
<div class="grid-item small">Cell 11</div>
<div class="grid-item small">Cell 12</div>
</div>
我想用 Flexbox 实现什么:
width: 100%;
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: flex-start;
和
<div class="item">1</div>
<div class="item">2</div>
<div class="item big">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
<div class="item">7</div>
这很有挑战性,因为
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container {
display: flex;
flex-wrap: wrap;
position: relative;
gap: var(--gap);
padding-right: 50%;
--gap: 8px;
}
.grid-item {
width: calc(50% - calc(var(--gap) / 2));
display: grid;
place-items: center;
background-color: coral;
}
.small {
height: 200px;
}
.big {
position: absolute;
inset: 0 0 0 auto;
width: calc(50% - var(--gap));
}
<div class="container">
<div class="grid-item small">Cell 1</div>
<div class="grid-item small">Cell 2</div>
<div class="grid-item big">Cell 3</div>
<div class="grid-item small">Cell 4</div>
<div class="grid-item small">Cell 5</div>
<div class="grid-item small">Cell 6</div>
<div class="grid-item small">Cell 7</div>
<div class="grid-item small">Cell 8</div>
<div class="grid-item small">Cell 9</div>
<div class="grid-item small">Cell 10</div>
<div class="grid-item small">Cell 11</div>
<div class="grid-item small">Cell 12</div>
</div>
到目前为止我知道,这是使用 Flexbox 的唯一方法,因为它是一维的。
*{
margin: 0;
padding: 0;
}
.flex-container{
display: flex;
flex-wrap: wrap;
gap: 10px;
}
.flex-inner-container {
display: flex;
flex-wrap: wrap;
gap: 10px;
flex: 1 0 calc(50% - 10px);
}
.flex-inner-container .item {
background-color: aquamarine;
flex: 1 0 calc(50% - 10px);
height: 200px;
}
.big {
flex: 1 0 calc(50% - 10px);
background-color: aquamarine;
}
<div class="flex-container">
<div class="flex-inner-container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
<div class="item">7</div>
</div>
<div class="item big">3</div>
</div>