【问题标题】:How to calc() the number of rows in a grid template?如何计算()网格模板中的行数?
【发布时间】:2019-06-24 03:52:31
【问题描述】:

我想使用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>

我对@9​​87654325@、calc 和部门协同工作的方式有什么遗漏吗?这是在 Chrome 71.0.3578.98 中。

【问题讨论】:

    标签: html css css-grid css-calc


    【解决方案1】:

    当对calc 使用除法时,结果将是number 而不是integer,因此它不起作用,因为repeat() 期望interger

    repeat() 语法的一般形式大约是,

    repeat( [ &lt;positive-integer&gt; | auto-fill | auto-fit ] , &lt;track-list&gt; )ref

    /,检查右侧是否为&lt;number&gt;。如果左侧是&lt;integer&gt;,则解析为&lt;number&gt;。否则,解析为左侧的类型。ref

    数值用&lt;number&gt;表示,代表实数,可能带有小数部分。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>

    【讨论】:

    • 哦,哇,我没有意识到这一点!有没有办法将 CSS 数字转换为整数?
    • @SanderMoolin 我不知道。不过,这肯定是要向 WG 提出的问题!
    • @SanderMoolin 实际上没有,但是有一个 floor() 或 round() 函数会很有趣..可能在未来的水平;)
    猜你喜欢
    • 1970-01-01
    • 2013-07-18
    • 2015-09-15
    • 2014-03-10
    • 2020-05-28
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    相关资源
    最近更新 更多