How do I make it so a cell in a grid is only as wide as its max-content
, but never overflows the parent grid container?
Check this example:
.grid {
border: 1px dotted blue;
display: grid;
gap: 5px;
grid-template-columns: min-content max-content;
width: 120px;
}
label {
background-color: yellow;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
<div class="grid">
<input type="checkbox" id="foo" /><label for="foo">foo label, must not overflow parent grid!</label>
</div>
<div class="grid">
<input type="checkbox" id="bar" /><label for="bar">bar label</label>
</div>
The code sample also shows my real-world use case. I need it so the label is restricted to the parent element, while also not being any wider than necessary, because I don't want white-space to the right side of the label be part of the clickable area.
If I set it to 1fr
instead of max-content
, the label covers the whole rest of the container, which is unwanted because now you can toggle the checkbox clicking the whitespace right of the label.
.grid {
border: 1px dotted blue;
display: grid;
gap: 5px;
grid-template-columns: min-content 1fr;
width: 120px;
}
label {
background-color: yellow;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
<div class="grid">
<input type="checkbox" id="foo" /><label for="foo">foo label, must not overflow parent grid!</label>
</div>
<div class="grid">
<input type="checkbox" id="bar" /><label for="bar">bar label</label>
</div>
CSS would offer a solution here
min(max-content, 1fr)
but unfortunately max-content
cannot be used in the min()
-function.
Fr is a fractional unit and 1fr is for 1 part of the available space. The following are a few examples of the fr unit at work. The grid items in these examples are placed onto the grid with grid areas.
Since the browser found a grid item of 1fr, it will allow such item to take all available space (that is 100% of the container), unless something else appears on that track. When the auto thing appears, it is only assigned, so to say it, enough space to exist (with its content, currently just a number).
So each 1FR=1/5th of the available space or 20%. So, 2FR is 2/5th or 40% wide. Now you have 4 columns but a total of 5FRs so automatically each fraction takes up 20% of the available space. So, the first column takes up 2 fractions of the space, that's 40%.
You can use fit-content(100%)
ref
.grid {
border: 1px dotted blue;
display: grid;
gap: 5px;
grid-template-columns: min-content fit-content(100%);
width: 120px;
}
label {
background-color: yellow;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
<div class="grid">
<input type="checkbox" id="foo" /><label for="foo">foo label, must not overflow parent grid!</label>
</div>
<div class="grid">
<input type="checkbox" id="bar" /><label for="bar">bar label</label>
</div>
With flexbox it's a trivial task because all you need is to make the element a flexbox container:
.grid {
border: 1px dotted blue;
display: flex;
gap: 5px;
width: 120px;
}
label {
background-color: yellow;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
<div class="grid">
<input type="checkbox" id="foo" /><label for="foo">foo label, must not overflow parent grid!</label>
</div>
<div class="grid">
<input type="checkbox" id="bar" /><label for="bar">bar label</label>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With