如何计算()的行数在网格模板?

问题描述 投票:5回答:1

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

html css css3 css-grid css-calc
1个回答
6
投票

当采用师带calc的结果将是一个number而不是integer这样,因为repeat()期待一个interger将无法正常工作

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

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

/,检查右侧<number>。如果左边是<integer>,决心<number>。否则,解决向左side.ref的类型

数值由<number>表示,并且表示实数,可能带有小数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将尝试qazxsw POI的结果四舍五入到一个正整数之一。

下面所有的实施将无法在Chrome和火狐将工作:

calc()
.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;
  
}
© www.soinside.com 2019 - 2024. All rights reserved.