Dynamically resizing grid cells
Categories:
Dynamically Resizing Grid Cells for Responsive Layouts

Learn how to create flexible and responsive grid layouts where cells dynamically adjust their size based on content and viewport, using modern CSS Grid and JavaScript techniques.
In modern web development, creating responsive layouts that adapt seamlessly to different screen sizes and content variations is crucial. CSS Grid offers powerful capabilities for defining two-dimensional layouts, but making grid cells dynamically resize based on their content or available space often requires a deeper understanding of its properties and sometimes a touch of JavaScript. This article explores various techniques to achieve dynamic grid cell resizing, ensuring your layouts are both flexible and robust.
Understanding CSS Grid's Intrinsic Sizing
CSS Grid provides several units and functions that allow for intrinsic sizing, meaning the size of grid tracks (rows and columns) can be determined by their content or the available space. Key among these are fr
units, min-content
, max-content
, and auto
, often combined with minmax()
.
The fr
unit (fractional unit) distributes available space proportionally among grid tracks. For example, grid-template-columns: 1fr 2fr 1fr;
would make the middle column twice as wide as the outer columns. When combined with minmax()
, it becomes incredibly powerful for dynamic resizing.
.grid-container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 1em;
}
Using repeat(auto-fit, minmax(min, max))
for responsive columns.
auto-fit
keyword with minmax()
is a game-changer for responsive grids. It automatically fits as many columns as possible into the container, each being at least min
size and growing up to max
size, distributing remaining space with fr
units.Dynamic Row Sizing with grid-auto-rows
While columns often adapt horizontally, rows also need to adjust vertically, especially when content within a cell varies significantly. The grid-auto-rows
property is essential for controlling the size of implicitly created grid rows (rows that are not explicitly defined by grid-template-rows
).
By default, grid-auto-rows
is auto
, which means rows will size themselves to fit their content. However, you can provide a minimum height to prevent content from becoming too small, or use minmax()
for more control.
.grid-container {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
grid-auto-rows: minmax(100px, auto);
gap: 1em;
}
Setting dynamic row heights with grid-auto-rows
.
flowchart TD A[Grid Container] --> B{`display: grid`} B --> C{`grid-template-columns`} C --> C1[`repeat(auto-fit, minmax(200px, 1fr))`] B --> D{`grid-auto-rows`} D --> D1[`minmax(100px, auto)`] C1 & D1 --> E[Dynamic Cell Sizing] E --> F[Content Adapts] E --> G[Layout Responds to Viewport]
Flowchart illustrating how CSS Grid properties contribute to dynamic cell sizing.
JavaScript for Advanced Dynamic Resizing
While CSS Grid handles most dynamic sizing scenarios, there are cases where JavaScript might be necessary. This often involves measuring content, reacting to specific user interactions, or integrating with external data that influences layout. For instance, if you need to ensure all cells in a particular row have the exact same height, even if their content differs, JavaScript can measure the tallest cell and apply that height to its siblings.
function equalizeRowHeights(gridSelector) {
const grid = document.querySelector(gridSelector);
if (!grid) return;
const rows = Array.from(grid.children);
let currentRowMaxHeight = 0;
let currentGridRow = -1;
rows.forEach(cell => {
const gridRow = parseInt(getComputedStyle(cell).gridRowStart);
if (gridRow !== currentGridRow) {
// New row, apply max height to previous row cells
if (currentGridRow !== -1) {
rows.filter(c => parseInt(getComputedStyle(c).gridRowStart) === currentGridRow)
.forEach(c => c.style.height = `${currentRowMaxHeight}px`);
}
currentGridRow = gridRow;
currentRowMaxHeight = 0;
}
currentRowMaxHeight = Math.max(currentRowMaxHeight, cell.offsetHeight);
});
// Apply to the last row
if (currentGridRow !== -1) {
rows.filter(c => parseInt(getComputedStyle(c).gridRowStart) === currentGridRow)
.forEach(c => c.style.height = `${currentRowMaxHeight}px`);
}
}
// Call on load and resize
window.addEventListener('load', () => equalizeRowHeights('.my-grid'));
window.addEventListener('resize', () => equalizeRowHeights('.my-grid'));
JavaScript function to equalize heights of cells within each grid row.
Best Practices for Dynamic Grid Cells
To ensure your dynamic grid cells perform well and remain maintainable, consider these best practices:
1. Prioritize CSS Grid
Always start with CSS Grid properties like minmax()
, auto-fit
/auto-fill
, fr
units, and grid-auto-rows
. These are optimized for layout and provide excellent responsiveness out-of-the-box.
2. Use Logical Properties
Instead of width
and height
, consider inline-size
and block-size
for better internationalization and writing mode support, though for basic grid cells, width
and height
are often sufficient.
3. Optimize Content
Ensure the content within your grid cells is also responsive. Use max-width: 100%
for images, and consider flexible typography units like rem
or em
.
4. Test Across Devices
Thoroughly test your dynamic grids on various screen sizes, devices, and with different amounts of content to catch any unexpected layout issues.