Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why content size is different if we have more columns in CSS grid?

Tags:

Why does this:

div {
  width: 200px;
  display: grid;
  grid: "first second" / 1fr 1fr;
  background-color: #ccc;
  padding: 8px;
}

#first {
  grid-area: first;
}

#second {
  grid-area: second;
}
<div>
  <input id="first" />
  <input id="second" />
</div>

Behave differently from this:

div {
  width: 200px;
  display: grid;
  grid: "first first second second" / 1fr 1fr 1fr 1fr;
  background-color: #ccc;
  padding: 8px;
}

#first {
  grid-area: first;
}

#second {
  grid-area: second;
}
<div>
  <input id="first" />
  <input id="second" />
</div>

Note that the only thing I changed was the number of columns and the area each input occupies.

like image 610
gfpacheco Avatar asked Oct 09 '18 14:10

gfpacheco


2 Answers

This is an issue with the spec and how 1fr is intepreted.

From a W3C bug/issue report

The "problem" is that 1fr resolves to minmax(auto, 1fr) and this means that we need to know the size of (the parent) before resolving the size of the 1fr track (because the min track sizing function is content sized, it's auto).

There is a quick solution to this issue from the author POV, just replacing 1fr by minmax(0px, 1fr) for example.

..or in this case just setting min-width:0 to the child elements.

like image 135
Paulie_D Avatar answered Dec 08 '22 20:12

Paulie_D


I think its a rendering issue casued by the default width set by the browser for the inputs. Its fixed by adding 100% width for the inputs and setting up the border-box box model.

div {
  width: 200px;
  display: grid;
  grid: "first second" / 1fr 1fr;
  background-color: #ccc;
  padding: 8px;
}

#first {
  grid-area: first;
}

#second {
  grid-area: second;
}

input {
  width: 100%;
}

* {
  box-sizing: border-box;
}
<div>
  <input id="first" />
  <input id="second" />
</div>
like image 34
passatgt Avatar answered Dec 08 '22 19:12

passatgt