I believe that visual aids are more effective than text in illustrating the issue:
My Objective
https://i.sstatic.net/vBAsI.png
Current Situation
Looking at the example, you can see that the title and description expand equally to accommodate the gallery. I aim to achieve a layout similar to the first image, where the gallery extends past the container and generates a scrollbar, allowing for minimal content in the left column.
https://i.sstatic.net/BCI1M.png
* {
margin: 0;
}
.container {
background-color: aqua;
display: grid;
padding: 1rem;
grid-template-columns: 1fr min-content; /* Avoid setting a fixed width for min-content */
grid-template-rows: repeat(2, min-content);
grid-template-areas:
'title gallery'
'description gallery';
grid-gap: 1rem;
}
.gallery {
grid-area: gallery;
display: grid;
grid-template-columns: 8rem 8rem;
grid-auto-rows: 8rem;
grid-gap: 1rem;
overflow-y: auto; /* How do I have this overflow, depending on the cumulative min-content of the left column */
}
.gallery > * {
background-color: magenta;
}
h2 {
grid-area: title;
background-color: white;
}
p {
grid-area: description;
background-color: lightgrey;
}
<div class="container">
<div class="gallery">
<div></div>
<div></div>
<div></div>
<div></div>
</div>
<h2>Some Title</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
A Temporary Fix
To temporarily address the issue, I applied absolute positioning which necessitates knowing the width of the gallery in advance.
* {
margin: 0;
}
.container {
background-color: aqua;
display: grid;
padding: 1rem;
grid-template-columns: 1fr 18rem; /* Reserve space for the gallery */
grid-template-rows: repeat(2, min-content);
grid-template-areas:
'title gallery'
'description gallery';
grid-gap: 1rem;
position: relative /* Create a context for absolute positioning */
}
.gallery {
grid-area: gallery;
display: grid;
grid-template-columns: 8rem 8rem;
grid-auto-rows: 8rem;
grid-gap: 1rem;
overflow-y: auto;
position: absolute; /* Remove gallery from the flow */
top: 0; /* Align with the container's boundaries */
right: 0;
}
.gallery > * {
background-color: magenta;
}
h2 {
grid-area: title;
background-color: white;
}
p {
grid-area: description;
background-color: lightgrey;
}
<div class="container">
<div class="gallery">
<div></div>
<div></div>
<div></div>
<div></div>
</div>
<h2>Some Title</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
</div>
Is there a way to achieve the desired layout without specifying a fixed width for the gallery? (And without using JavaScript). I haven't explored the use of flexbox yet, as my primary objective is to minimize the number of wrappers for optimal responsiveness. If achieving this with CSS grid alone is not feasible, I am open to reverting to flexbox.