1. 首页 > 快讯

css超出一行省略号(蒙古元素花边)

CSS Grid 布局常见问题

问题 1:在 box1 中,如何实现一行 5 个元素而不留空白?

在 box1 中,使用 grid-template-columns: repeat(auto-fill, 20%) 指令定义列为根据内容自动填充,列宽为容器宽度的 20%。但是,由于列与列之间还存在 20px 的 grid-column-gap,导致无法一行 5 个元素。

解决方案:

立即学习“前端免费学习笔记(深入)”;

使用 grid-template-columns: repeat(auto-fit, calc((100% - 4 * 20px) / 5)),其中:

  • auto-fit 确保根据内容自动填充列。
  • calc((100% - 4 * 20px) / 5) 计算每个列的宽度(容器宽度减去 4 个 grid-column-gap(共 80px),再除以 5)。

问题 2:在 box2 中,如果 li 元素不足 5 个,其宽度会增加,如何解决?

在 box2 中,使用 grid-template-columns: auto auto auto auto auto 指令定义了 5 个固定宽度的列。当 li 元素不足 5 个时,其余宽度会平均分配给现有的元素。

解决方案:

立即学习“前端免费学习笔记(深入)”;

根据需要定义不同的列宽,例如:

  • grid-template-columns: 1fr 1fr 1fr 1fr 2fr,其中最后一列为其他列的两倍宽。
  • grid-template-columns: min-content min-content min-content 100px 1fr,其中前三列为自适应宽度,第四列为固定宽度 100px,最后一列为剩余宽度。

本文采摘于网络,不代表本站立场,转载联系作者并注明出处:https://www.iotsj.com//kuaixun/6660.html

联系我们

在线咨询:点击这里给我发消息

微信号:666666