英文:
How to implement a product grid with same height items but a custom height banner in HTML/CSS?
问题
我正在尝试实现一个显示一些产品项目的网格。 网格的所有网格项都应具有相同的高度,我可以通过grid-auto-rows: 1fr;
来实现。 但现在我还想在中间添加一个横幅,其高度与内容相同。 当然,这不起作用,因为grid-auto-rows: 1fr;
。 如果我删除grid-auto-rows: 1fr;
或将其更改为grid-auto-rows: auto;
或grid-template-rows: 1fr;
,则横幅将具有其内容的高度,但其他网格项将基于行的高度。 目标是仍然使所有网格项和网格中的所有行具有相同的高度。
如何实现这一点?
我创建了一个CodePen: codepen.io/dsalvia/pen/Podzrjr
第一个产品有5行。 横幅有1行,其他产品有3行。 我希望所有产品项目的高度都与最高的产品项目相同。 因此,在这种情况下,所有产品项目的高度应为5行。 只有横幅的高度应为1行
这是我的HTML:
<div class="product-list">
<div class="grid-item">
<!-- 产品项目 -->
</div>
<div class="grid-item">
<!-- 产品项目 -->
</div>
<div class="grid-item">
<!-- 产品项目 -->
</div>
<div class="grid-item banner" style="grid-row-start: 2">
<!-- 横幅 -->
</div>
<div class="grid-item">
<!-- 产品项目 -->
</div>
<div class="grid-item">
<!-- 产品项目 -->
</div>
<div class="grid-item">
<!-- 产品项目 -->
</div>
</div>
和CSS:
.product-list{
display: grid;
gap: 2rem;
grid-auto-rows: 1fr;
grid-template-columns: repeat(3,1fr);
}
.product-list .grid-item.banner{
grid-row-start: 2;
grid-column: 1/-1;
}
这是我得到的结果:高度与产品项目相同的横幅
这是我想要的:高度与其内容相同的横幅
有人有办法如何实现吗?
英文:
I am trying to implement a grid that displays some product items. All grid items of the grid should have the same height which I am able to accomplish with grid-auto-rows: 1fr;
. But now I also want to add a banner in between with the height of its content. Of course this does not work because of grid-auto-rows: 1fr;
. If I remove grid-auto-rows: 1fr;
or change it to grid-auto-rows: auto;
or grid-template-rows: 1fr;
the banner will have the height of its content, but the other gris items will have the height based on the row. The goal is that still all grid items and all rows in the grid should have the same height.
How can I implement that?
I created a codepen: codepen.io/dsalvia/pen/Podzrjr
The first product has 5 rows. The banner has 1 row and other products have 3 rows. I want to have all product items the height of the tallest product item. So in this case all product items should have the height of 5 rows. Only the banner should have the height of 1 row
This is my HTML:
<div class="product-list">
<div class="grid-item">
<!-- Product Item -->
</div>
<div class="grid-item">
<!-- Product Item -->
</div>
<div class="grid-item">
<!-- Product Item -->
</div>
<div class="grid-item banner" style="grid-row-start: 2">
<!-- Banner -->
</div>
<div class="grid-item">
<!-- Product Item -->
</div>
<div class="grid-item">
<!-- Product Item -->
</div>
<div class="grid-item">
<!-- Product Item -->
</div>
</div>
And the CSS:
.product-list{
display: grid;
gap: 2rem;
grid-auto-rows: 1fr;
grid-template-columns: repeat(3,1fr);
}
.product-list .grid-item.banner{
grid-row-start: 2;
grid-column: 1/-1;
}
This is what I get:Banner with same height as the product items
And this is what I want to have: Banner with height of its content
Does someone have an idea how I can accomplish that?
答案1
得分: 2
以下是代码部分的中文翻译:
如在MDN grid-auto-rows中所解释,指定了隐式创建的网格行跟踪或跟踪模式的大小。
根据您编辑的描述,仍然可以使用grid-template-rows
来将第二行设置为auto
来实现。第三行及其后的行可以使用grid-auto-rows: 1fr
来使用隐式网格,因此如果将来产品项的高度为6行文本,每个网格项将与最高的项匹配。
我采用并修改了您的示例代码以展示结果。希望这对您有帮助并符合您的需求。
英文:
As explained in here MDN grid-auto-rows, specifies the size of an implicitly-created grid row track or pattern of tracks.
Based on your edited description, that is still achieveable with using grid-template-rows
to set second row as auto
. The third row and next can use implicit grid with grid-auto-rows: 1fr
so if you have product item with height of 6 lines of text in the future, every grid item will match the tallest item.
I take and modified your sample code to show you the result
<!-- begin snippet: js hide: false console: false babel: false -->
<!-- language: lang-css -->
.product-list {
display: grid;
gap: 2rem;
grid-auto-rows: 1fr;
grid-template-rows: 1fr auto;
grid-template-columns: repeat(3, 1fr);
}
.product-list .grid-item {
border: 1px solid black;
background-color: lightgray;
}
.product-list .grid-item.banner {
grid-row-start: 2;
grid-column: 1/-1;
background-color: red;
}
<!-- language: lang-html -->
<div class="product-list">
<div class="grid-item">
<!-- Product Item -->
row 1 <br> row 2 <br> row 3 <br> row 4 <br> row 5
</div>
<div class="grid-item">
<!-- Product Item -->
row 1 <br> row 2 <br> row 3
</div>
<div class="grid-item">
<!-- Product Item -->
row 1 <br> row 2 <br> row 3
</div>
<div class="grid-item banner">
<!-- Banner -->
banner
</div>
<div class="grid-item">
<!-- Product Item -->
row 1 <br> row 2 <br> row 3
</div>
<div class="grid-item">
<!-- Product Item -->
row 1 <br> row 2 <br> row 3
</div>
<div class="grid-item">
<!-- Product Item -->
row 1 <br> row 2 <br> row 3
</div>
<div class="grid-item">
<!-- Product Item -->
row 1 <br> row 2 <br> row 3
</div>
<div class="grid-item">
<!-- Product Item -->
row 1 <br> row 2 <br> row 3
</div>
<div class="grid-item">
<!-- Product Item -->
row 1 <br> row 2 <br> row 3 <br> row 4 <br> row 5 <br> row 6
</div>
</div>
<!-- end snippet -->
Hope it helps and fit with your case.
答案2
得分: 0
尝试使用以下代码替代:
.product-list {
grid-template-rows: 1fr;
}
而不是:
.product-list {
grid-auto-rows: 1fr;
}
英文:
Try:
.product-list {
grid-template-rows: 1fr;
}
instead of:
.product-list {
grid-auto-rows: 1fr;
}
答案3
得分: 0
这是我找到的与你寻找的最接近的内容。但这并不理想,因为它假定页面中的行数是固定的(在此示例中为5行)。如果这不是你想要的,请希望你能找到更好的解决方案。
<!-- 开始片段: js 隐藏: false 控制台: true babel: false -->
<!-- 语言: lang-css -->
.product-list{
display: grid;
gap: .5rem;
grid-template-rows: 1fr auto repeat(3,1fr);
grid-template-columns: repeat(3,1fr);
}
.product-list .grid-item.banner{
grid-row-start: 2;
grid-column: 1/-1;
background-color: lime;
}
.grid-item {
border: 1px solid;
background-color: pink;
}
<!-- 语言: lang-html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="stylesheet" href="test.css" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>test</title>
</head>
<body>
<div class="product-list">
<div class="grid-item">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quisquam aperiam porro hic ut iure eaque reprehenderit! Totam consequatur ea dicta.
</div>
<div class="grid-item">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Suscipit eligendi quod similique nesciunt quos labore, quae distinctio reprehenderit animi nostrum totam cupiditate maiores non iure magnam, perferendis autem. Velit, distinctio?Lorem ipsum dolor sit amet consectetur, adipisicing elit. Voluptatibus, rem! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quisquam aperiam porro hic ut iure eaque reprehenderit! Totam consequatur ea dicta.
</div>
<div class="grid-item">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quisquam aperiam porro hic ut iure eaque reprehenderit! Totam consequatur ea dicta.
</div>
<div class="grid-item banner" style="grid-row-start: 2">
Lorem ipsum dolor, sit amet consectetur adipisicing elit. Rem, hic!
</div>
<div class="grid-item">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quisquam aperiam porro hic ut iure eaque reprehenderit! Totam consequatur ea dicta.
</div>
<div class="grid-item">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quisquam aperiam porro hic ut iure eaque reprehenderit! Totam consequatur ea dicta.
</div>
<div class="grid-item">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quisquam aperiam porro hic ut iure eaque reprehenderit! Totam consequatur ea dicta.
</div>
<div class="grid-item">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quisquam aperiam porro hic ut iure eaque reprehenderit! Totam consequatur ea dicta.
</div>
<div class="grid-item">
Lorem ipsum dolor sit, amet consectetur adipisicing elit. Dolorum placeat quibusdam atque! Veritatis mollitia dolore possimus, explicabo omnis tenetur veniam quaerat corporis ipsam tempora rerum, autem reiciendis inventore quis. Minima?Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quisquam aperiam porro hic ut iure eaque reprehenderit! Totam consequatur ea dicta.
</div>
<div class="grid-item">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quisquam aperiam porro hic ut iure eaque reprehenderit! Totam consequatur ea dicta.
</div>
<div class="grid-item">
Lorem ipsum dolor sit, amet consectetur adipisicing elit. Dolorum placeat quibusdam atque! Veritatis mollitia dolore possimus, explicabo omnis tenetur veniam quaerat corporis ipsam tempora rerum, autem reiciendis inventore quis. Minima?Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quisquam aperiam porro hic ut iure eaque reprehenderit! Totam consequatur ea dicta.
</div>
</div>
<p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Reiciendis odit quam, necessitatibus incidunt, totam quod laborum ab, recusandae fugiat ea nemo dolor?</p>
</body>
</html>
<!-- 结束片段 -->
希望这对你有所帮助。
英文:
Here is the closest to what you are looking for that I could find. But it is not ideal since it assumes the number of rows in a page is fixed (5 rows in this example).
If that's not want you want, I hope you find better.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.product-list{
display: grid;
gap: .5rem;
grid-template-rows: 1fr auto repeat(3,1fr) ;
grid-template-columns: repeat(3,1fr);
}
.product-list .grid-item.banner{
grid-row-start: 2;
grid-column: 1/-1;
background-color: lime;
}
.grid-item {
border: 1px solid;
background-color: pink;
}
<!-- language: lang-html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="stylesheet" href="test.css" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>test</title>
</head>
<body>
<div class="product-list">
<div class="grid-item">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quisquam aperiam porro hic ut iure eaque reprehenderit! Totam consequatur ea dicta.
</div>
<div class="grid-item">
Lorem ipsum dolor sit amet consectetur adipisicing elit. Suscipit eligendi quod similique nesciunt quos labore, quae distinctio reprehenderit animi nostrum totam cupiditate maiores non iure magnam, perferendis autem. Velit, distinctio?Lorem ipsum dolor sit amet consectetur, adipisicing elit. Voluptatibus, rem! Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quisquam aperiam porro hic ut iure eaque reprehenderit! Totam consequatur ea dicta.
</div>
<div class="grid-item">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quisquam aperiam porro hic ut iure eaque reprehenderit! Totam consequatur ea dicta.
</div>
<div class="grid-item banner" style="grid-row-start: 2">
Lorem ipsum dolor, sit amet consectetur adipisicing elit. Rem, hic!
</div>
<div class="grid-item">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quisquam aperiam porro hic ut iure eaque reprehenderit! Totam consequatur ea dicta.
</div>
<div class="grid-item">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quisquam aperiam porro hic ut iure eaque reprehenderit! Totam consequatur ea dicta.
</div>
<div class="grid-item">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quisquam aperiam porro hic ut iure eaque reprehenderit! Totam consequatur ea dicta.
</div>
<div class="grid-item">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quisquam aperiam porro hic ut iure eaque reprehenderit! Totam consequatur ea dicta.
</div>
<div class="grid-item">
Lorem ipsum dolor sit, amet consectetur adipisicing elit. Dolorum placeat quibusdam atque! Veritatis mollitia dolore possimus, explicabo omnis tenetur veniam quaerat corporis ipsam tempora rerum, autem reiciendis inventore quis. Minima?Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quisquam aperiam porro hic ut iure eaque reprehenderit! Totam consequatur ea dicta.
</div>
<div class="grid-item">
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quisquam aperiam porro hic ut iure eaque reprehenderit! Totam consequatur ea dicta.
</div>
<div class="grid-item">
Lorem ipsum dolor sit, amet consectetur adipisicing elit. Dolorum placeat quibusdam atque! Veritatis mollitia dolore possimus, explicabo omnis tenetur veniam quaerat corporis ipsam tempora rerum, autem reiciendis inventore quis. Minima?Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quisquam aperiam porro hic ut iure eaque reprehenderit! Totam consequatur ea dicta.
</div>
</div>
<p>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Reiciendis odit quam, necessitatibus incidunt, totam quod laborum ab, recusandae fugiat ea nemo dolor?</p>
</body>
</html>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论