I've read the following:
Can I make a CSS grid with dynamic number of rows or columns?
CSS Grid layout for dynamic content
and a few other questions. They cover dynamic columns at the end of the grid with grid-auto-columns
. The dynamic column I have is added at the start of the grid.
I have a layout that looks like this.
body {
background: #131418;
margin: 1em;
}
:root {
--extra-section: 70px;
}
.container {
padding: 0.5em;
border: 1px solid silver;
color: silver;
/* grid */
display: grid;
grid-template-columns: 1fr auto repeat(3, minmax(var(--extra-section), auto));
grid-auto-columns: var(--extra-section);
grid-auto-flow: column;
grid-column-gap: 0.5em;
align-items: center;
}
<div class="container">
<div class="title">
Lorem ipsum dolor sit amet, consectetur
</div>
<div class="people">
<img src="https://i.pravatar.cc/25" />
<img src="https://i.pravatar.cc/25" />
<img src="https://i.pravatar.cc/25" />
</div>
<div class="likes">9</div>
<div class="bookmarks">4</div>
<div class="shares">2</div>
</div>
Sometimes the number of small columns on the right is more than three. This is not an issue, and I have managed to fix that with
grid-auto-columns: var(--extra-section);
grid-auto-flow: column;
since they're always the same width.
My issue is that there's a checkbox that appears on the far left based on a user action. Like so
body {
background: #131418;
margin: 1em;
}
:root {
--extra-section: 70px;
}
.container {
padding: 0.5em;
border: 1px solid silver;
color: silver;
/* grid */
display: grid;
grid-template-columns: 1fr auto repeat(3, minmax(var(--extra-section), auto));
grid-auto-columns: var(--extra-section);
grid-auto-flow: column;
grid-column-gap: 0.5em;
align-items: center;
}
<div class="container">
<div class="select">
<label>
<input type="checkbox">
</label>
</div>
<div class="title">
Lorem ipsum dolor sit amet, consectetur
</div>
<div class="people">
<img src="https://i.pravatar.cc/25" />
<img src="https://i.pravatar.cc/25" />
<img src="https://i.pravatar.cc/25" />
</div>
<div class="likes">9</div>
<div class="bookmarks">4</div>
<div class="shares">2</div>
</div>
When that happens, the layout gets messed up. It's not very obvious in the preview, so please open it in full screen to see the issue.
I understand why that's happening because I've defined an explicit grid and now the checkbox is taking 1fr
.
This extra checkbox is only added in a given state. When that occurs, a class is also added to the container selecting
. So, here's what I'm doing right now to try to fix this. I redefine the grid columns when that class is added.
body {
background: #131418;
margin: 1em;
}
:root {
--extra-section: 70px;
}
.container {
padding: 0.5em;
border: 1px solid silver;
color: silver;
/* grid */
display: grid;
grid-template-columns: 1fr auto repeat(3, minmax(var(--extra-section), auto));
grid-auto-columns: var(--extra-section);
grid-auto-flow: column;
grid-column-gap: 0.5em;
align-items: center;
}
.container.selecting {
grid-template-columns: auto 1fr auto repeat(3, var(--extra-section));
}
<div class="container selecting">
<div class="select">
<label>
<input type="checkbox">
</label>
</div>
<div class="title">
Lorem ipsum dolor sit amet, consectetur
</div>
<div class="people">
<img src="https://i.pravatar.cc/25" />
<img src="https://i.pravatar.cc/25" />
<img src="https://i.pravatar.cc/25" />
</div>
<div class="likes">9</div>
<div class="bookmarks">4</div>
<div class="shares">2</div>
</div>
There's a bunch of other elements below this one just like it and they all share the same layout and CSS. When the checkbox is added to one, it's added to the rest of them as well. Kind of like a table if you would.
My goal is to give as much space to the title section as possible.
My question is:
Can I achieve that without having to redefine the template? As in, set it once, and it would adjust automatically if the checkbox is added while giving as much space to the title as possible?
I would like to solve this with grid and not flexbox because everything else in the layout uses grid and I'd love to keep it that way.