html - 如何计算()网格模板中的行数?

标签 html css css-grid css-calc

我想使用 calc() 计算网格模板中的行数,但尝试使用除法获取 repeat 计数不起作用:

.grid {
  display: grid;
  grid-gap: 10px;
  grid-template-columns: 1fr;
  margin-bottom: 10px;
  background: rgba(0, 0, 0, 0.2);
}

.grid>div {
  background: tomato;
  width: 20px;
  text-align: center;
  margin: auto;
}

.grid.no-calc {
  grid-template-rows: repeat(3, 30px);
}

.grid.addition {
  grid-template-rows: repeat(calc(1 + 2), 30px);
}

.grid.subtraction {
  grid-template-rows: repeat(calc(4 - 1), 30px);
}

.grid.multiplication {
  grid-template-rows: repeat(calc(3 * 1), 30px);
}

.grid.division {
  grid-template-rows: repeat(calc(6 / 2), 30px);
}
<p>Top 4 have their row heights set correctly</p>

<div class="grid no-calc">
  <div>1</div>
  <div>2</div>
  <div>3</div>
</div>

<div class="grid addition">
  <div>1</div>
  <div>2</div>
  <div>3</div>
</div>

<div class="grid subtraction">
  <div>1</div>
  <div>2</div>
  <div>3</div>
</div>

<div class="grid multiplication">
  <div>1</div>
  <div>2</div>
  <div>3</div>
</div>

<p>Division doesn't work in setting row height</p>

<div class="grid division">
  <div>1</div>
  <div>2</div>
  <div>3</div>
</div>

关于 repeatcalc 和除法协同工作的方式,我是否遗漏了什么?这是在 Chrome 71.0.3578.98 中。

最佳答案

将除法与 calc 结合使用时结果将是 number而不是 integer因此它不会工作,因为 repeat()期待 interger

The generic form of the repeat() syntax is, approximately,

repeat( [ <positive-integer> | auto-fill | auto-fit ] , <track-list> )ref

At /, check that the right side is <number>. If the left side is <integer>, resolve to <number>. Otherwise, resolve to the type of the left side.ref

Number values are denoted by <number>, and represent real numbers, possibly with a fractional component.ref

即使我们都知道结果将是一个整数,浏览器仍会将其视为一个数字。

如果其中一侧有数字,则乘法可能会遇到同样的问题

.grid {
  display: grid;
  grid-gap: 10px;
  grid-template-columns: 1fr;
  margin-bottom: 10px;
  background: rgba(0, 0, 0, 0.2);
}

.grid>div {
  background: tomato;
  width: 20px;
  text-align: center;
  margin: auto;
}

.grid.no-calc {
  grid-template-columns: repeat(3, 30px);
  border-bottom:3px solid red;
}

.grid.multiplication {
  grid-template-columns: repeat(calc(3 * 1.0), 30px); /*will fail*/
  border-bottom:calc(3px * 1.0) solid red;
}

.grid.division {
  grid-template-columns: repeat(calc(6 / 2), 30px);
  border-bottom:calc(6px / 2) solid red; /*this will work because border accept numbers*/
}
<div class="grid no-calc">
  <div>1</div>
  <div>2</div>
  <div>3</div>
</div>


<div class="grid multiplication">
  <div>1</div>
  <div>2</div>
  <div>3</div>
</div>

<div class="grid division">
  <div>1</div>
  <div>2</div>
  <div>3</div>
</div>


Firefox 的行为不同,即使我们明确指定数字也永远不会失败。在所有情况下,Fiferox 将尝试calc() 的结果进行舍入为正整数。

以下所有示例在 Chrome 上都将失败,但可以在 Firefox 上运行:

.grid {
  display: grid;
  grid-gap: 10px;
  grid-template-columns: 1fr;
  margin-bottom: 10px;
  background: rgba(0, 0, 0, 0.2);
}

.grid>div {
  background: tomato;
  width: 20px;
  text-align: center;
  margin: auto;
}

.grid.no-calc {
  grid-template-columns: repeat(calc(2.8), 30px); /*will be converted to 3*/
  border-bottom:calc(calc(2.8) * 1px) solid red;
}

.grid.multiplication {
  grid-template-columns: repeat(calc(3 * 1.55), 30px);  /*will be converted to 4*/
  border-bottom:calc(calc(3 * 1.55) * 1px) solid red;
}

.grid.division {
  grid-template-columns: repeat(calc(6 / 2.8), 30px); /*will be converted to 2*/
  border-bottom:calc(calc(6 / 2.8) * 1px) solid red;
  
}
<div class="grid no-calc">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
</div>


<div class="grid multiplication">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
</div>

<div class="grid division">
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <div>4</div>
</div>

关于html - 如何计算()网格模板中的行数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62965233/

相关文章:

javascript - 图外的 JQPlot 图例

javascript - 通过单击外部关闭 div 时出现问题

CSS 网格,中间有一个额外的包装器

html - 仅在 Mac 上的 Chrome 中输入时输入字段变小(可能是 CSS-Grid 问题)

html - 如何修复 : even list elements not working

html - Bootstrap 4 模态背景在移动设备上没有响应

php - 从 while 循环中从表中选择单个值到字段中

html - 有没有办法截断(隐藏)宽表格单元格?

javascript - 悬停并单击 -> 更改颜色,再次单击切换为原始颜色

html - 具有可变宽度的 tile 的 Flexbox 顺序