使网格行覆盖网格模板行

时间:2019-09-26 02:43:56

标签: html css css-grid

我要问这个是我自己的教育,如果这是不好的做法,请随便说。

我希望通过为每个元素设置单独的网格行设置来覆盖元素#one #two #three#four的父属性grid-template-rows: 100px 100px 100px 100px

元素#one#two的响应方式如下:

#one{
     grid-row: 1/ span 4 ; 
}

#two{
     grid-row: 1/ span 4 ; 
}

但是,当我尝试将相似的设置应用于#three#four时,没有得到我期望的结果。我期望#three#four的高度与#one#two完全相同。相反,它们会飞到页面的一侧。

我希望它显示类似于#one和#two的元素。它不起作用:

#three{
     grid-row: 2/ span 4 ; 
}


#four{
     grid-row: 2/ span 4 ; 
}

我想知道如何解决此问题以及正确的单个值,以设置#three#four来覆盖网格模板行,如所述。

代码:https://jsfiddle.net/ek2r6a19/

* {
  font-family: Helvetica;
  font-size: 1em;
  color: white;
  text-align: center;
}

img {
  display: block;
  margin: 20px 20px;
  margin-left: auto;
  margin-right: auto;
  border-radius: 50%;
  border-width: 5px;
  border-style: solid;
  border-color: rgb(141, 110, 99);
  max-width: 50%;
  height: auto;
}


/*_____________________________GRID */

.container {
  display: grid;
  grid-template-columns: 1fr 2fr;
  grid-template-rows: 100px 100px 100px 100px 100px 100px;
  grid-gap: 10px;
}

.container div:nth-child(even) {
  background-color: orange;
}

.container div:nth-child(odd) {
  background-color: purple;
}

#one {
  grid-row: 1/ span 4;
}

#two {
  grid-row: 1/ span 4;
}


/*

I expected this to display the elements similar to #one and #two. It doesn't work
#three{
     grid-row: 2/ span 4 ; 
}


#four{
     grid-row: 2/ span 4 ; 
}

*/
<div class="container">
  <div id="one">1
    <div class="content-container">

    </div>
  </div>
  <div id="two">
    2
    <div class="content-container">
      text
    </div>
  </div>
  <div id="three">3
    <div class="content-container">
      text
    </div>
  </div>
  <div id="four">4
    <div class="content-container">
      text
    </div>
  </div>
  <div id="five">5
    <div class="content-container">

      text
    </div>
  </div>
  <div id="six">6
    <div class="content-container">
      text
    </div>
  </div>


</div>

1 个答案:

答案 0 :(得分:1)

您需要覆盖grid auto-placement algorithm,当未明确定义网格区域时,该here会计算出渲染网格区域的“正确”位置。

您正在定义行。只需定义列即可。

.container {
  display: grid;
  grid-template-columns: 1fr 2fr;
  grid-template-rows: 100px 100px 100px 100px 100px 100px;
  grid-gap: 10px;
}

#one {
  grid-row: 1 / span 4;
  grid-column: 1; /* new */
}

#two {
  grid-row: 1 / span 4;
  grid-column: 2; /* new */
}

#three {
  grid-row: 2 / span 4;
  grid-column: 1; /* new */
}

#four {
  grid-row: 2 / span 4;
  grid-column: 2; /* new */
}

* {
  font-family: Helvetica;
  font-size: 1em;
  color: white;
  text-align: center;
}

img {
  display: block;
  margin: 20px 20px;
  margin-left: auto;
  margin-right: auto;
  border-radius: 50%;
  border-width: 5px;
  border-style: solid;
  border-color: rgb(141, 110, 99);
  max-width: 50%;
  height: auto;
}

.container div:nth-child(even) {
  background-color: orange;
}

.container div:nth-child(odd) {
  background-color: purple;
}
<div class="container">
  <div id="one">1
    <div class="content-container"></div>
  </div>
  <div id="two">2
    <div class="content-container">text</div>
  </div>
  <div id="three">3
    <div class="content-container">text</div>
  </div>
  <div id="four">4
    <div class="content-container">text</div>
  </div>
  <div id="five">5
    <div class="content-container">text</div>
  </div>
  <div id="six">6
    <div class="content-container">text</div>
  </div>
</div>

您会注意到#one#three以及#two#four的高度似乎并不相同。其实是。

问题是,您已设置#three#four开始在网格第2行开始跨越。

好吧,#one#two设置为从网格行第1行开始,并跨越4行,因此它们与#three#four重叠(反之亦然) )。您需要从网格行第5行开始#three#four

.container {
  display: grid;
  grid-template-columns: 1fr 2fr;
  grid-template-rows: 100px 100px 100px 100px 100px 100px;
  grid-gap: 10px;
  grid-auto-rows: 100px; /* added to accommodate more rows */
}

#one {
  grid-row: 1 / span 4;
  grid-column: 1;
}

#two {
  grid-row: 1 / span 4;
  grid-column: 2;
}

#three {
  grid-row: 5 / span 4;
  grid-column: 1;
}

#four {
  grid-row: 5 / span 4;
  grid-column: 2;
}

* {
  font-family: Helvetica;
  font-size: 1em;
  color: white;
  text-align: center;
}

img {
  display: block;
  margin: 20px 20px;
  margin-left: auto;
  margin-right: auto;
  border-radius: 50%;
  border-width: 5px;
  border-style: solid;
  border-color: rgb(141, 110, 99);
  max-width: 50%;
  height: auto;
}

.container div:nth-child(even) {
  background-color: orange;
}

.container div:nth-child(odd) {
  background-color: purple;
}
<div class="container">
  <div id="one">1
    <div class="content-container"></div>
  </div>
  <div id="two">2
    <div class="content-container">text</div>
  </div>
  <div id="three">3
    <div class="content-container">text</div>
  </div>
  <div id="four">4
    <div class="content-container">text</div>
  </div>
  <div id="five">5
    <div class="content-container">text</div>
  </div>
  <div id="six">6
    <div class="content-container">text</div>
  </div>
</div>