Why does explicitly setting two CSS Grid columns to 50% each create a horizontal scroll beyond 100% of the viewport width?
This codepen shows the behavior I'm asking about.
Basically, when the grid is set to the following css, it produces a two-column layout with no forced horizontal scroll beyond the viewport width:
.grid {
display: grid;
grid-gap: 1rem;
grid-template-columns: 50%;
grid-template-areas: 'item item';
}
When grid-template-columns
is changed to explicitly set both column widths, the result is a two-column layout with a horizontal scroll:
.grid {
grid-template-columns: 50% 50%;
}
Why is the horizontal scroll present in the second context, but not the first?
edit: <aside>
I seem to remember flexbox behaves in a similar fashion</aside>
* {
box-sizing: border-box;
}
body {
margin: 0;
}
.grid {
padding-top: 1rem;
display: grid;
grid-gap: 1rem;
grid-template-columns: 50%;
grid-template-areas: 'item item';
}
/* WHY THE HORIZONTAL SCROLL
WHEN LINES 21-23 ARE
UNCOMMENTED?! */
/* .grid {
grid-template-columns: 50% 50%;
} */
.item {
padding: 2rem;
border: 2px solid black;
border-radius: 0.25rem;
}
.p1 {
padding: .5rem;
}
code {
padding: .5rem;
background: gray;
color: white;
font-weight: regular;
}
<header class="p1">
<h1>Why does the horizontal scroll happen when <code>.grid { grid-template-columns: 50% 50%; }</code></h1>
<p>Uncomment lines 21-23 in the CSS of this codepen to set the above <code>.grid</code> css rule. See this <a href="https://stackoverflow.com/q/52374483/2145103">Stack Overflow post for discussion.</a></p>
</header>
<div class="grid">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>