I have the following HTML. I would expect the columns to have the following widths:
First sum up the fr units: 0.370162 + 0.46852 + 0.281787 = 1.120469
- Column 1 = ~198px (600 * 0.370162 / 1.120469)
- Column 2 = ~250px (600 * 0.46852 / 1.120469)
- Column 3 = ~150px (600 * 0.281787 / 1.120469)
Instead all browsers I've tested (Chrome, Safari, Firefox) renders columns with the following widths:
- Column 1 = ~165px
- Column 2 = ~209px
- Column 3 = ~150px
So column 3 is the right width. What's even stranger is if I remove the min-width: 150px
CSS the columns are the expected widths! If I remove the min-width
CSS and change grid-template-columns
to grid-template-columns: minmax(150px, 0.370162fr) minmax(150px, 0.46852fr) minmax(150px, 0.281787fr)
the issue still reproduces.
This feels like a CSS grid bug in how it handles minimum column widths. Or is there some corner of the spec that I'm missing?
My use case: I'm building a responsive table element with resizable columns with CSS grid.
*,
::before,
::after {
box-sizing: border-box;
}
.table {
display: grid;
width: 600px;
padding: 1px;
gap: 1px;
background-color: #ccc;
grid-template-columns: 0.370162fr 0.46852fr 0.281787fr;
}
.cell {
min-width: 150px;
padding: 8px 12px;
background-color: white;
}
<div class="table">
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
</div>
I have the following HTML. I would expect the columns to have the following widths:
First sum up the fr units: 0.370162 + 0.46852 + 0.281787 = 1.120469
- Column 1 = ~198px (600 * 0.370162 / 1.120469)
- Column 2 = ~250px (600 * 0.46852 / 1.120469)
- Column 3 = ~150px (600 * 0.281787 / 1.120469)
Instead all browsers I've tested (Chrome, Safari, Firefox) renders columns with the following widths:
- Column 1 = ~165px
- Column 2 = ~209px
- Column 3 = ~150px
So column 3 is the right width. What's even stranger is if I remove the min-width: 150px
CSS the columns are the expected widths! If I remove the min-width
CSS and change grid-template-columns
to grid-template-columns: minmax(150px, 0.370162fr) minmax(150px, 0.46852fr) minmax(150px, 0.281787fr)
the issue still reproduces.
This feels like a CSS grid bug in how it handles minimum column widths. Or is there some corner of the spec that I'm missing?
My use case: I'm building a responsive table element with resizable columns with CSS grid.
*,
::before,
::after {
box-sizing: border-box;
}
.table {
display: grid;
width: 600px;
padding: 1px;
gap: 1px;
background-color: #ccc;
grid-template-columns: 0.370162fr 0.46852fr 0.281787fr;
}
.cell {
min-width: 150px;
padding: 8px 12px;
background-color: white;
}
<div class="table">
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
</div>
Share
Improve this question
edited Jan 29 at 22:32
CcmU
1,03614 silver badges31 bronze badges
asked Jan 29 at 16:34
CalebmerCalebmer
2,8808 gold badges30 silver badges44 bronze badges
3
|
1 Answer
Reset to default 1You need to use minmax(0, X.XXXXX) with your column widths, as per this answer.
.table {
display: grid;
width: 600px;
padding: 1px;
gap: 1px;
background-color: #ccc;
grid-template-columns: minmax(0, 0.370162fr) minmax(0, 0.46852fr) minmax(0, 0.281787fr);
box-sizing: border-box;
}
.cell {
min-width: 150px;
padding: 8px 12px;
background-color: white;
box-sizing: border-box;
}
<div class="table">
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
</div>
Further reading:
- Preventing a grid blowout
- Equal width columns in CSS grid are kinda weird
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745290089a4620804.html
padding: 1px
fixes the issue. So that may be part of the problem? – Calebmer Commented Jan 29 at 16:41