我在下面创建了一个布局display: grid
,我使用了旧padding-top
技巧来制作流畅的方形 div,但我想知道是否有更好的方法来做到这一点,无论是使用 CSS Grid 还是 Flexbox。布局应该是原样,而不是使用 JS。
https://codepen.io/anon/pen/GWzavX
.o-container {
margin: 0 auto;
max-width: 1280px;
}
.o-grid {
box-sizing: border-box;
display: grid;
grid-gap: 1rem;
grid-template-columns: repeat(4, 1fr);
grid-template-rows: repeat(2, 1fr);
max-width: 100%;
padding: 0 1rem;
width: 100%;
}
.o-grid__content {
box-sizing: border-box;
height: 100%;
padding: 1rem;
position: absolute;
top: 0;
width: 100%;
}
.o-grid__item {
box-shadow: inset 0 0 0 1px red;
position: relative;
}
.o-grid__item--tall {
grid-row: span 2;
}
.o-grid__item--wide {
grid-column: span 2;
padding-top: 50%;
}
<div class="o-container">
<div class="o-grid">
<div class="o-grid__item o-grid__item--wide">
<div class="o-grid__content">
1. 2x1
</div>
</div>
<div class="o-grid__item">
<div class="o-grid__content">
2. 1x1
</div>
</div>
<div class="o-grid__item o-grid__item--tall">
<div class="o-grid__content">
3. 1x2
</div>
</div>
<div class="o-grid__item">
<div class="o-grid__content">
4. 1x1
</div>
</div>
<div class="o-grid__item">
<div class="o-grid__content">
5. 1x1
</div>
</div>
<div class="o-grid__item">
<div class="o-grid__content">
6. 1x1
</div>
</div>
</div>
</div>