Note that this problem is not really a CSS Grid issue. It's more a matter related to input
elements.
Here's your code, using input
elements:
div {
display: grid;
width: 200px;
background-color: #ccc;
padding: 8px;
}
div[one] {
grid: "first second" / 1fr 1fr;
}
div[two] {
grid: "first first second second" / 1fr 1fr 1fr 1fr;
}
#first {
grid-area: first;
}
#second {
grid-area: second;
}
<div one>
<input id="first" />
<input id="second" />
</div>
<hr>
<div two>
<input id="first" />
<input id="second" />
</div>
There's a difference in the two layouts.
Now here's your code using section
elements instead of inputs:
article {
display: grid;
width: 200px;
background-color: #ccc;
}
article[one] {
grid: "first second" / 1fr 1fr;
}
article[two] {
grid: "first first second second" / 1fr 1fr 1fr 1fr;
}
#first {
grid-area: first;
height: 25px;
background-color: green;
}
#second {
grid-area: second;
height: 25px;
background-color: red;
}
<article one>
<section id="first"></section>
<section id="second"></section>
</article>
<hr>
<article two>
<section id="first"></section>
<section id="second"></section>
</article>
There's no difference.
This essentially boils down to the intrinsic width of input
elements, which is a standard feature in most browsers. The issue is discussed in more detail here:
The simple way to disable this feature is by:
- overriding the default
min-width: auto
on grid items by adding min-width: 0
(explanation) or,
- by switching from
1fr
(which represents minmax(auto, 1fr)
) to minmax(0px, 1fr)
.