4x3网格,两个项目加倍高度

时间:2018-07-27 04:15:34

标签: html css css3 css-grid

我正在尝试创建两个高度加倍的4x3网格。

.grida {
	width:540px;
	display: grid;
	row-gap: 14px;
	grid-template-columns: repeat(4, 120px);
	justify-content:space-evenly;
	align-content: space-evenly;
	background-color: #fff;
	color: #444;
}

.card {
	background-color: #ddd;
	color: #555;
	border-radius: 5px;
	padding: 5px;
	font-size: 150%;
	height:68px;
}

.a {
	grid-row: auto / span 2;
}

.g {
	grid-row: auto / span 2;
}
<div class='grida'>
  <div class="card a">A</div>
  <div class="card b">B</div>
  <div class="card c">C</div>
  <div class="card d">D</div>
  <div class="card e">E</div>
  <div class="card f">F</div>
  <div class="card g">G</div>
  <div class="card h">H</div>
  <div class="card i">I</div>
  <div class="card j">J</div>
</div>

为什么有空块?
我希望ag的高度加倍。
有帮助吗?

2 个答案:

答案 0 :(得分:6)

grid-row不起作用,因为您正在card div上设置高度。为了调整card div的“高度”,我使用了填充技巧。这样做:

.grida {
	width:540px;
	display: grid;
	row-gap: 14px;
	grid-template-columns: repeat(4, 120px);
	justify-content:space-evenly;
	align-content: space-evenly;
	background-color: #fff;
	color: #444;
}

.card {
	background-color: #ddd;
	color: #555;
	border-radius: 5px;
	font-size: 150%;
	padding: 30px 5px
}

.a, .g {
	grid-row: auto / span 2 !important;
}
<div class='grida'>
  <div class="card a">A</div>
  <div class="card b">B</div>
  <div class="card c">C</div>
  <div class="card d">D</div>
  <div class="card e">E</div>
  <div class="card f">F</div>
  <div class="card g">G</div>
  <div class="card h">H</div>
  <div class="card i">I</div>
  <div class="card j">J</div>
</div>

答案 1 :(得分:0)

已经回答了,但是使用grid-auto-rows:68px可能是比较简单的解决方案。

    .grida {
        width:540px;
        display: grid;
        row-gap: 14px;
        grid-template-columns: repeat(4, 120px);
        justify-content:space-evenly;
        align-content: space-evenly;
        background-color: #fff;
        color: #444;
        /*setting the height of automatically/implicitly created rows*/
        grid-auto-rows:68px;
    }

    .card {
        background-color: #ddd;
        color: #555;
        border-radius: 5px;
        padding: 5px;
        font-size: 150%;
        /*height:68px;*/

    }

    .a,.g {
        grid-row: auto / span 2;
    }
<div class='grida'>
    <div class="card a">A</div>
    <div class="card b">B</div>
    <div class="card c">C</div>
    <div class="card d">D</div>
    <div class="card e">E</div>
    <div class="card f">F</div>
    <div class="card g">G</div>
    <div class="card h">H</div>
    <div class="card i">I</div>
    <div class="card j">J</div>
</div>