html - 带有 CSS 的产品表
问题描述
我正在尝试使用 React 和 CSS 制作产品目录。除了最后一排产品外,一切似乎都很好。
最后一行只有 1 个元素,并且由于flex-grow: 1
设置了,它占用了所有可用宽度。
是否可以为行中的所有元素设置相同的宽度?
ul {
display: flex;
flex-wrap: wrap;
list-style: none;
}
.products__product {
flex: 1 0 20%;
margin: 1.5em 0.75em 0 0.75em;
min-height: 250px;
background:grey;
}
<div class="products">
<div class="container">
<div class="products__content">
<ul>
<a class="products__product" href="/products/21/stone-in-the-night"
><li><h1>Stone in the Night</h1></li></a
><a class="products__product" href="/products/21/the-dying-of-the-spirits"
><li><h1>The Dying of the Spirits</h1></li></a
><a class="products__product" href="/products/21/the-beginnings-guardian"
><li><h1>The Beginning&#8217;s Guardian</h1></li></a
><a class="products__product" href="/products/21/death-of-light"
><li><h1>Death of Light</h1></li></a
><a class="products__product" href="/products/21/the-lost-soul"
><li><h1>The Lost Soul</h1></li></a
><a class="products__product" href="/products/21/first-husband"
><li><h1>First Husband</h1></li></a
><a class="products__product" href="/products/21/verzliaraktis"
><li><h1>Veržliaraktis</h1></li></a
><a class="products__product" href="/products/21/raktas"
><li><h1>Raktas</h1></li></a
>
</ul>
</div>
</div>
</div>
解决方案
您始终可以将初始宽度设置为百分比:
.products__product {
...
flex: 0 0 20%
...
}
上面说,不要缩小或扩大项目,而是将其设置为容器宽度的 20%。所以即使容器中有单个元素,它也只会占据 20%。您可以根据拥有的元素数量进行调整。不幸的是,你不能使用分数符号。
推荐阅读
- amp-html - AMP 可以访问 DOM 元素吗?
- python - 将数据框转换为 json 文件时出现问题
- php - password_verify() 和 mysql 的问题,有些返回正确,有些不正确
- c# - C# - 如何在 Web API 2 上从 JWT 添加用户声明/角色
- c++ - 具有 WS_EX_LAYERED 窗口样式的 Adobe Air
- docker - Docker 中 Puppeteer/Jest 测试中的竞争条件
- docker - 在 Ubuntu 上的 VS Code 中运行 NGC 容器
- visual-studio - How Do I Delete A SSIS Log Configuration in SSDT
- javascript - MVC 5 - 在文档准备好时将下拉选项设置为选中后,为什么会重置?
- reactjs - 检查 cookie 中 JWT 令牌的有效性时如何避免不必要的 API 调用