Home >Web Front-end >CSS Tutorial >CSS Grid: Percentages vs. `fr` Units: Which Should I Use?
Function: Fr units allocate free space within a container.
Calculation: Free space (after subtracting grid gaps) is divided equally among the fr columns.
Function: Defines column widths as a percentage of the container's width.
Calculation: Column width is determined by dividing the total width by the number of columns, including the widths of any grid gaps.
Percentage: Columns can overflow outside the container width due to fixed unit lengths and grid gaps.
Fr: Columns only take up free space within the container, not extending beyond it. Grid gaps do not impact free space allocation.
To avoid overflow with percentage widths, subtract the total width of the grid gaps from the full container width before dividing by the number of columns:
grid-template-columns: repeat(12, calc(8.3333% - 9.1667px))
where:
By using appropriate values for percentage widths or implementing fr units, you can manage column sizing and prevent overflow in CSS Grid Layout.
The above is the detailed content of CSS Grid: Percentages vs. `fr` Units: Which Should I Use?. For more information, please follow other related articles on the PHP Chinese website!