ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

CSS Grid: Responsive Column Sizing

CSS

Goal -- WPM

Ready
Exercise Algorithm Area
1/* Main grid container styling */
2.grid-container {
3display: grid;
4grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
5gap: 1rem;
6padding: 1rem;
7}
8
9/* Basic item styling */
10.grid-item {
11background-color: #f0f0f0;
12padding: 1rem;
13border: 1px solid #ccc;
14text-align: center;
15}
16
17/* Responsive adjustments for very small screens */
18@media (max-width: 480px) {
19.grid-container {
20grid-template-columns: 1fr;
21}
22}
23
24/* Function to calculate column widths (conceptual, not actual CSS) */
25function calculateColumnWidth(minWidth, maxWidth) {
26// This function conceptually represents the browser's calculation
27// for grid-template-columns: repeat(auto-fit, minmax(minWidth, maxWidth))
28// It ensures columns are at least minWidth and can grow up to maxWidth.
29// Edge case: If container width is less than minWidth, it will wrap.
30// Edge case: If container width is very large, columns will expand.
31return `minmax(${minWidth}px, ${maxWidth})`;
32}
Algorithm description viewbox

CSS Grid: Responsive Column Sizing

Algorithm description:

This CSS code defines a responsive grid layout using `display: grid` and `grid-template-columns`. It leverages `repeat(auto-fit, minmax(250px, 1fr))` to automatically adjust the number of columns based on the container width, ensuring each column is at least 250px wide but can grow to fill available space. A media query handles very small screens by collapsing the grid to a single column. This is commonly used for product listings, image galleries, or article layouts that need to adapt across devices.

Algorithm explanation:

The `grid-template-columns: repeat(auto-fit, minmax(250px, 1fr))` property is the core of this responsive design. `repeat(auto-fit, ...)` tells the grid to create as many columns as can fit within the container, while `minmax(250px, 1fr)` defines the size of each column. The `minmax()` function ensures each column has a minimum width of `250px` and a maximum width of `1fr` (one fraction of the available space). If the container is too narrow to fit multiple 250px columns, `auto-fit` will cause them to wrap. The `gap` property adds spacing between grid items. The media query `@media (max-width: 480px)` overrides the `grid-template-columns` to `1fr` on screens smaller than 480px, ensuring a single-column layout for better readability on mobile devices. This approach is efficient as it relies on the browser's intrinsic grid layout capabilities.

Pseudocode:

SET grid display to grid.
DEFINE columns using repeat(auto-fit, minmax(minimum_width, maximum_width)).
ADD gap between grid items.
ON small screens (e.g., max-width 480px):
  SET columns to a single fraction (1fr).