我有5个盒子:第一行有三个,第二行有下两个:
我希望所有方框但第二个高度相等和 5 th 框右下方3 个。
我尝试使用flexbox,现在我正在使用display: grid
进行尝试。我正在寻找一个CSS only 解决方案,如果可能的话。
.grid-container {
display: grid;
grid-template-columns: auto auto auto;
grid-auto-rows: min-content;
grid-gap: 0;
background-color: #2196F3;
}
.grid-container > div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
border: 1px solid #ccc;
font-size: 30px;
}
.item4 {
grid-column-start: 1;
grid-column-end: 3;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="grid-container">
<div class="item1"><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p></div>
<div class="item2"><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Totam sunt blanditiis facere eius placeat suscipit consequuntur ullam dolor quod tenetur. Nostrum deleniti id temporibus officia, itaque voluptatibus laudantium ad porro.</p></div>
<div class="item3"><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p></div>
<div class="item4"><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p></div>
<div class="item5"><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p></div>
</div>
我该怎样做才能达到预期的效果?
答案 0 :(得分:1)
这是使用line-based placement的一种方法。
.grid-container {
display: grid;
grid-template-columns: auto auto auto;
grid-template-rows: repeat(10, 1fr);
grid-gap: 5px;
padding: 5px;
height: 100vh;
background-color: #2196F3;
}
/* line-based placement */
.item1 { grid-column: 1 / 2; grid-row: 1 / 4; }
.item2 { grid-column: 2 / 3; grid-row: 1 / 8; }
.item3 { grid-column: 3 / 4; grid-row: 1 / 4; }
.item4 { grid-column: 1 / 3; grid-row: 8 / 11; }
.item5 { grid-column: 3 / 4; grid-row: 4 / 11; }
.grid-container > div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
border: 1px solid #ccc;
}
* { box-sizing: border-box; margin: 0; }
&#13;
<div class="grid-container">
<div class="item1"><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p></div>
<div class="item2"><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Totam sunt blanditiis facere eius placeat suscipit consequuntur ullam dolor quod tenetur. Nostrum deleniti id temporibus officia, itaque voluptatibus laudantium ad porro.</p></div>
<div class="item3"><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p></div>
<div class="item4"><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p></div>
<div class="item5"><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p></div>
</div>
&#13;
这是使用grid-template-areas
的另一种方法。
.grid-container {
display: grid;
grid-template-columns: auto auto auto;
grid-template-rows: repeat(10, 1fr);
grid-gap: 5px;
padding: 5px;
height: 100vh;
background-color: #2196F3;
grid-template-areas:
" item1 item2 item3 "
" item1 item2 item3 "
" item1 item2 item3 "
" . item2 item5 "
" . item2 item5 "
" . item2 item5 "
" . item2 item5 "
" item4 item4 item5 "
" item4 item4 item5 "
" item4 item4 item5 ";
}
.item1 { grid-area: item1; }
.item2 { grid-area: item2; }
.item3 { grid-area: item3; }
.item4 { grid-area: item4; }
.item5 { grid-area: item5; }
.grid-container > div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
border: 1px solid #ccc;
}
* { box-sizing: border-box; margin: 0; }
&#13;
<div class="grid-container">
<div class="item1"><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p></div>
<div class="item2"><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Totam sunt blanditiis facere eius placeat suscipit consequuntur ullam dolor quod tenetur. Nostrum deleniti id temporibus officia, itaque voluptatibus laudantium ad porro.</p></div>
<div class="item3"><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p></div>
<div class="item4"><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p></div>
<div class="item5"><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p></div>
</div>
&#13;
答案 1 :(得分:0)
对父容器使用grid-template-areas,并为子项指定grid-area。
.grid-container {
display: grid;
grid-template-areas: 'left middle right';
grid-template-columns: 1fr 1fr 1fr;
grid-auto-flow: row;
grid-gap: 0;
background-color: #2196F3;
}
.grid-container > div {
background-color: rgba(255, 255, 255, 0.8);
text-align: center;
border: 1px solid #ccc;
font-size: 30px;
}
.item1 {
grid-area: left;
grid-row: auto;
}
.item2 {
grid-area: middle;
grid-row: auto / span 2;
};
.item3 {
grid-area: right;
grid-row: auto;
};
.item4 {
grid-area: left;
grid-row: auto;
};
.item5 {
grid-area: right;
grid-row: auto;
}
<div class="grid-container">
<div class="item1"><p><strong>Item1</strong> Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p></div>
<div class="item2"><p><strong>Item2</strong>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Totam sunt blanditiis facere eius placeat suscipit consequuntur ullam dolor quod tenetur. Nostrum deleniti id temporibus officia, itaque voluptatibus laudantium ad porro.</p></div>
<div class="item3"><p><strong>Item3</strong> Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p></div>
<div class="item4"><p><strong>Item4</strong> Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p></div>
<div class="item5"><p><strong>Item5</strong> Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p></div>
</div>