CSS网格:确保动态行都具有特定大小。

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

CSS grid: ensure dynamic rows are all of a specific size

问题

这里有一个有点奇怪的情况。我尝试使用CSS网格将内容并排放置。问题在于内容的一侧是动态的,所以应该自动调整高度,而另一侧应该是固定大小。

这是一个示例:

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

<!-- language: lang-css -->
.page {
  display: grid;
  grid-template-columns: repeat(4, 25%);
  grid-template-rows: repeat(5, 16px);
  grid-auto-rows: fit-content(16px);
  border: 1px solid blue;
}

.text {
  grid-row-start: 1;
  grid-row-end: auto;
  grid-column-start: 1;
  grid-column-end: 4;
  border: 2px solid red;
  display: flex;
}

.description {
  grid-column-start: 4;
  grid-column-end: 5;
  grid-row-start: 1;
  grid-row-end: 5;
  border: 1px solid green;
  display: flex;
}

<!-- language: lang-html -->
<div class="page">
  <div class="text">
    Nam adipiscing. Aenean viverra rhoncus pede. Sed magna purus, fermentum eu, tincidunt eu, varius ut, felis. Donec posuere vulputate arcu. Fusce egestas elit eget lorem.

    Quisque rutrum. In consectetuer turpis ut velit. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc, quis gravida magna mi a libero. Nullam dictum felis eu pede mollis pretium. Maecenas egestas arcu quis ligula mattis placerat.

    Fusce pharetra convallis urna. Curabitur suscipit suscipit tellus. Vestibulum volutpat pretium libero. Donec mi odio, faucibus at, scelerisque quis, convallis in, nisi. Etiam sollicitudin, ipsum eu pulvinar rutrum, tellus ipsum laoreet sapien, quis venenatis ante odio sit amet eros.

    Fusce pharetra convallis urna. Curabitur suscipit suscipit tellus. Vestibulum volutpat pretium libero. Donec mi odio, faucibus at, scelerisque quis, convallis in, nisi. Etiam sollicitudin, ipsum eu pulvinar rutrum, tellus ipsum laoreet sapien, quis venenatis ante odio sit amet eros.
  </div>
  <div class="description">
    <img src="https://www.clker.com/cliparts/y/c/r/G/g/M/warning-md.png" alt='Warning clip art' style="height: 100%;" />
  </div>
</div>

<!-- end snippet -->

所以这里的想法是:我希望我的内容分为4列,每列宽度为25%,并有5行固定高度(每行16px)。这是代码片段中的蓝色区域。但由于文本内容可以变化(红色区域),我希望内容能够占用更多行,如果必要的话,但我不希望扩展影响图像(绿色区域)。

我尝试使用自动行,但这会导致图像被拉伸,因为行高是自动的。我不确定是否只使用网格就能实现这个目标。

英文:

I have a bit of a strange situation. I am trying to use a CSS grid to put content side by side. The problem is that one side of the content is dynamic so should be automatic in height while the other side should be a fixed size.

Here's an example of what this looks like:

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

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

.page {
  display: grid;
  grid-template-columns: repeat(4, 25%);
  grid-template-rows: repeat(5, 16px);
  grid-auto-rows: fit-content(16px);
  border: 1px solid blue;
}

.text {
  grid-row-start: 1;
  grid-row-end: auto;
  grid-column-start: 1;
  grid-column-end: 4;
  border: 2px solid red;
  display: flex;
}

.description {
  grid-column-start: 4;
  grid-column-end: 5;
  grid-row-start: 1;
  grid-row-end: 5;
  border: 1px solid green;
  display: flex;
}

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

&lt;div class=&quot;page&quot;&gt;
&lt;div class=&quot;text&quot;&gt;
  Nam adipiscing. Aenean viverra rhoncus pede. Sed magna purus, fermentum eu, tincidunt eu, varius ut, felis. Donec posuere vulputate arcu. Fusce egestas elit eget lorem.

Quisque rutrum. In consectetuer turpis ut velit. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc, quis gravida magna mi a libero. Nullam dictum felis eu pede mollis pretium. Maecenas egestas arcu quis ligula mattis placerat.

Fusce pharetra convallis urna. Curabitur suscipit suscipit tellus. Vestibulum volutpat pretium libero. Donec mi odio, faucibus at, scelerisque quis, convallis in, nisi. Etiam sollicitudin, ipsum eu pulvinar rutrum, tellus ipsum laoreet sapien, quis venenatis ante odio sit amet eros.

Fusce pharetra convallis urna. Curabitur suscipit suscipit tellus. Vestibulum volutpat pretium libero. Donec mi odio, faucibus at, scelerisque quis, convallis in, nisi. Etiam sollicitudin, ipsum eu pulvinar rutrum, tellus ipsum laoreet sapien, quis venenatis ante odio sit amet eros.
&lt;/div&gt;
&lt;div class=&quot;description&quot;&gt;
   &lt;img src=&quot;https://www.clker.com/cliparts/y/c/r/G/g/M/warning-md.png&quot; alt=&#39;Warning clip art&#39; style=&quot;height: 100%;&quot; /&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

So the idea here is: I want my content to be 4 columns of 25% width each and 5 rows of a fixed height (16px each). This is the blue area in the snippet. However because the text content can vary (red area) I would like the content to be able to take up more rows if necessary, however I don't want the expansion to affect the image (green area).

I have tried using auto rows but that has the side-effect of stretching the image as well because the row height is automatic. I am not sure if this is even possible using only grid.

答案1

得分: 1

修正图像的高度,并不定义任何行配置:

.page {
  display: grid;
  grid-template-columns: repeat(4, 25%);
  border: 1px solid blue;
}

.text {
  grid-column: span 3;
  border: 2px solid red;
}

.description {
  border: 1px solid green;
  display: flex;
  height: 80px;
}
<div class="page">
  <div class="text">
    Nam adipiscing. Aenean viverra rhoncus pede. Sed magna purus, fermentum eu, tincidunt eu, varius ut, felis. Donec posuere vulputate arcu. Fusce egestas elit eget lorem.

    Quisque rutrum. In consectetuer turpis ut velit. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc, quis gravida magna mi a libero. Nullam dictum felis eu pede mollis pretium. Maecenas egestas arcu quis ligula mattis placerat.

    Fusce pharetra convallis urna. Curabitur suscipit suscipit tellus. Vestibulum volutpat pretium libero. Donec mi odio, faucibus at, scelerisque quis, convallis in, nisi. Etiam sollicitudin, ipsum eu pulvinar rutrum, tellus ipsum laoreet sapien, quis venenatis ante odio sit amet eros.

    Fusce pharetra convallis urna. Curabitur suscipit suscipit tellus. Vestibulum volutpat pretium libero. Donec mi odio, faucibus at, scelerisque quis, convallis in, nisi. Etiam sollicitudin, ipsum eu pulvinar rutrum, tellus ipsum laoreet sapien, quis venenatis ante odio sit amet eros.
  </div>
  <div class="description">
     <img src="https://www.clker.com/cliparts/y/c/r/G/g/M/warning-md.png" alt='Warning clip art' style="height: 100%;" />
  </div>
</div>
英文:

Fix the height of the image and don't define any row configuration:

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

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

.page {
  display: grid;
  grid-template-columns: repeat(4, 25%);
  border: 1px solid blue;
}

.text {
  grid-column: span 3;
  border: 2px solid red;
}

.description {
  border: 1px solid green;
  display: flex;
  height: 80px;
}

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

&lt;div class=&quot;page&quot;&gt;
&lt;div class=&quot;text&quot;&gt;
  Nam adipiscing. Aenean viverra rhoncus pede. Sed magna purus, fermentum eu, tincidunt eu, varius ut, felis. Donec posuere vulputate arcu. Fusce egestas elit eget lorem.

Quisque rutrum. In consectetuer turpis ut velit. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc, quis gravida magna mi a libero. Nullam dictum felis eu pede mollis pretium. Maecenas egestas arcu quis ligula mattis placerat.

Fusce pharetra convallis urna. Curabitur suscipit suscipit tellus. Vestibulum volutpat pretium libero. Donec mi odio, faucibus at, scelerisque quis, convallis in, nisi. Etiam sollicitudin, ipsum eu pulvinar rutrum, tellus ipsum laoreet sapien, quis venenatis ante odio sit amet eros.

Fusce pharetra convallis urna. Curabitur suscipit suscipit tellus. Vestibulum volutpat pretium libero. Donec mi odio, faucibus at, scelerisque quis, convallis in, nisi. Etiam sollicitudin, ipsum eu pulvinar rutrum, tellus ipsum laoreet sapien, quis venenatis ante odio sit amet eros.
&lt;/div&gt;
&lt;div class=&quot;description&quot;&gt;
   &lt;img src=&quot;https://www.clker.com/cliparts/y/c/r/G/g/M/warning-md.png&quot; alt=&#39;Warning clip art&#39; style=&quot;height: 100%;&quot; /&gt;
&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月10日 17:01:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75408888.html
匿名

发表评论

匿名网友

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

确定