CSS grid 跨越所有行

huangapple go评论69阅读模式
英文:

CSS grid span row to all rows

问题

以下是您提供的内容的翻译:

.mgrid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(91px, 1fr));
  grid-gap: 4px;
}

.head {
  background: blue;
  font-weight: bold;
  color: #fff;
  grid-row: 1 / -1;
}

.item {
  background: red;
}
<div class="mgrid">
  <div class="head">我的文本</div>
  <div class="item">项目</div>
  <div class="item">项目</div>
  <div class="item">项目</div>
  <div class="item">项目</div>
  <div class="item">项目</div>
  <div class="item">项目</div>
  <div class="item">项目</div>
  <div class="item">项目</div>
  <div class="item">项目</div>
  <div class="item">项目</div>
  <div class="item">项目</div>
  <div class="item">项目</div>
  <div class="item">项目</div>
  <div class="item">项目</div>
  <div class="item">项目</div>
</div>
英文:

CSS grid 跨越所有行

I want to make like this layout. but I can not make the .head element full rows. I also try with grid-row: 1 / 6, but it is not responsive when width change it not properly span rows. grid-column: 1 / -1 working fine grid-row: 1 / -1 this not working.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.mgrid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(91px, 1fr));
  grid-gap: 4px;
}

.head {
  background: blue;
  font-weight: bold;
  color: #fff;
  grid-row: 1 / -1;
}

.item {
  background: red;
}

<!-- language: lang-html -->

&lt;div class=&quot;mgrid&quot;&gt;
  &lt;div class=&quot;head&quot;&gt;My Text&lt;/div&gt;
  &lt;div class=&quot;item&quot;&gt;item&lt;/div&gt;
  &lt;div class=&quot;item&quot;&gt;item&lt;/div&gt;
  &lt;div class=&quot;item&quot;&gt;item&lt;/div&gt;
  &lt;div class=&quot;item&quot;&gt;item&lt;/div&gt;
  &lt;div class=&quot;item&quot;&gt;item&lt;/div&gt;
  &lt;div class=&quot;item&quot;&gt;item&lt;/div&gt;
  &lt;div class=&quot;item&quot;&gt;item&lt;/div&gt;
  &lt;div class=&quot;item&quot;&gt;item&lt;/div&gt;
  &lt;div class=&quot;item&quot;&gt;item&lt;/div&gt;
  &lt;div class=&quot;item&quot;&gt;item&lt;/div&gt;
  &lt;div class=&quot;item&quot;&gt;item&lt;/div&gt;
  &lt;div class=&quot;item&quot;&gt;item&lt;/div&gt;
  &lt;div class=&quot;item&quot;&gt;item&lt;/div&gt;
  &lt;div class=&quot;item&quot;&gt;item&lt;/div&gt;
  &lt;div class=&quot;item&quot;&gt;item&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

You can solve this using position: absolute.

.mgrid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(91px, 1fr));
  grid-gap: 4px;
  padding-left: 95px; /* 91px + gap */
  position: relative;
}

.head {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  width: 91px; /* same as the template */
  background: blue;
  font-weight: bold;
  color: #fff;
}

.item {
  background: red;
}
<div class="mgrid">
  <div class="head">My Text</div>
  <div class="item">item</div>
  <div class="item">item</div>
  <div class="item">item</div>
  <div class="item">item</div>
  <div class="item">item</div>
  <div class="item">item</div>
  <div class="item">item</div>
  <div class="item">item</div>
  <div class="item">item</div>
  <div class="item">item</div>
  <div class="item">item</div>
  <div class="item">item</div>
  <div class="item">item</div>
  <div class="item">item</div>
  <div class="item">item</div>
</div>
英文:

You can solve this using position: absolute

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

.mgrid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(91px, 1fr));
  grid-gap: 4px;
  padding-left: 95px; /* 91px + gap */
  position: relative;
}

.head {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  width: 91px; /* same as the template */
  background: blue;
  font-weight: bold;
  color: #fff;
}

.item {
  background: red;
}

<!-- language: lang-html -->

&lt;div class=&quot;mgrid&quot;&gt;
  &lt;div class=&quot;head&quot;&gt;My Text&lt;/div&gt;
  &lt;div class=&quot;item&quot;&gt;item&lt;/div&gt;
  &lt;div class=&quot;item&quot;&gt;item&lt;/div&gt;
  &lt;div class=&quot;item&quot;&gt;item&lt;/div&gt;
  &lt;div class=&quot;item&quot;&gt;item&lt;/div&gt;
  &lt;div class=&quot;item&quot;&gt;item&lt;/div&gt;
  &lt;div class=&quot;item&quot;&gt;item&lt;/div&gt;
  &lt;div class=&quot;item&quot;&gt;item&lt;/div&gt;
  &lt;div class=&quot;item&quot;&gt;item&lt;/div&gt;
  &lt;div class=&quot;item&quot;&gt;item&lt;/div&gt;
  &lt;div class=&quot;item&quot;&gt;item&lt;/div&gt;
  &lt;div class=&quot;item&quot;&gt;item&lt;/div&gt;
  &lt;div class=&quot;item&quot;&gt;item&lt;/div&gt;
  &lt;div class=&quot;item&quot;&gt;item&lt;/div&gt;
  &lt;div class=&quot;item&quot;&gt;item&lt;/div&gt;
  &lt;div class=&quot;item&quot;&gt;item&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月21日 03:58:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297106.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定