OSCSS.005: CSS Grid — Tracks, Fractional Units and Responsive Auto-Placement

Open-Source CSS Programming • Intermediate • Estimated study time: 35 minutes

CSS Grid organizes HTML elements along rows and columns. Unlike the primarily one-dimensional Flexbox model, Grid coordinates both dimensions in a single layout. A technician can use Grid to arrange equipment inventory, maintenance records or monitoring summaries without relying on fixed-width positioning. Review the Flexbox lesson before comparing the two layout systems.

Prerequisites and learning objectives

Know basic HTML, CSS selectors, properties and responsive units. By the end, learners should be able to distinguish explicit and implicit tracks, size columns using fractional units, build a responsive grid using repeat and minmax, explain auto-fit versus auto-fill, and preserve logical reading order.

Grid containers, tracks and placement

Applying display: grid to a parent creates a grid formatting context for its direct children. The space between neighboring grid lines forms a track; a cell is the intersection of a row and column. A declared grid-template-columns or grid-template-rows establishes explicit tracks. Additional tracks created as items exceed the declared grid are implicit. The gap property separates adjacent tracks without introducing outer margins. See MDN’s basic concepts of Grid and revisit CSS selectors.

The fr unit allocates a share of available flexible space after applicable sizing constraints and gaps. Two columns declared as 1fr and 2fr divide the available flexible space in a one-to-two ratio, subject to minimum track sizes and content constraints.

Kevin Powell: building a responsive CSS Grid layout.

Responsive track sizing

The repeat function can generate a specified number of tracks or determine how many fit. The minmax function sets a minimum and maximum track sizing function. Combining auto-fit with a fractional maximum allows occupied columns to expand when unused repeated tracks collapse. Auto-fill instead retains empty repeated tracks. The inner min function below caps the preferred minimum at the available container width to help avoid horizontal overflow on small screens. Review responsive CSS units.

Create a local HTML file using the following complete training example:

<!doctype html>
<html lang=”en”>
<meta charset=”utf-8″>
<meta name=”viewport” content=”width=device-width, initial-scale=1″>
<title>Training inventory</title>
<style>
  body { margin: 1rem; }
  .inventory {
    display: grid;
    grid-template-columns:
      repeat(auto-fit, minmax(min(100%, 14rem), 1fr));
    gap: 1rem;
  }
  .inventory article {
    min-width: 0;
    overflow-wrap: anywhere;
  }
</style>
<main class=”inventory”>
  <article><h2>Switch</h2><p>Ready</p></article>
  <article><h2>Router</h2><p>Ready</p></article>
  <article><h2>Server</h2><p>Maintenance</p></article>
</main>
</html>

Open the saved file in a browser and resize the viewport. The occupied columns adapt to available width. A collapsed empty track can still appear as zero pixels in computed browser styles. Inspect grid lines using browser developer tools. Read MDN’s Grid learning guide for additional examples.

Kevin Powell: breakpoint-free Grid layouts, minmax, auto-fit and auto-fill.

Accessibility and safe testing

Visual placement does not ordinarily alter document reading order or keyboard navigation. Keep source order meaningful and avoid relying on dense packing or explicit placement to communicate a sequence. Test locally with fictional inventory rather than uploading production equipment data. Read MDN’s Grid accessibility guidance and revisit the cascade and specificity lesson when styles conflict.

Exercises

1. Identify the property that creates the grid container. 2. Explain the difference between explicit and implicit tracks. 3. Replace auto-fit with auto-fill and inspect empty columns at a wide viewport. 4. Explain why the minimum uses min(100%, 14rem). 5. Check whether visual changes alter the document source order.

Answer key

1. display: grid. 2. Explicit tracks are declared; implicit tracks are generated as needed. 3. Auto-fill retains unused repeated tracks, whereas auto-fit collapses them. 4. The minimum can shrink to the container width, reducing narrow-screen overflow. 5. Grid placement alone does not normally change source order.

We volunteer daily to ensure the credibility of the information on this platform is Verifiably True. If you would like to support to help further secure the integrity of our research initiative, please scan the QR code below

Optional 5,000-satoshi contribution QR
Optional 5,000-satoshi contribution. Independently confirm the wallet address 3C9o19EH5HSiwEPyCTmEKzxhNCbo2X6TTb and payment amount before sending.

BitcoinVersus.tech is not a financial advisor. This media platform reports on financial subjects purely for informational purposes.

Leave a Reply