DevToolBoxGRATIS
Blogg

CSS Grid komplett guide: Fra grunnleggende til avanserte layouter

14 minby DevToolBox

Komplett guide til CSS Grid

CSS Grid er det kraftigste layoutsystemet som noen gang er lagt til CSS.

CSS Grid grunnleggende

CSS Grid creates a two-dimensional grid container. You define the column and row tracks, and place items into the resulting cells. The fr (fraction) unit distributes remaining space proportionally.

.container {
  display: grid;

  /* Define columns: 3 equal columns */
  grid-template-columns: 1fr 1fr 1fr;
  /* Shorthand: repeat(3, 1fr) */

  /* Define rows: auto height or fixed */
  grid-template-rows: auto 200px auto;

  /* Gap between cells */
  gap: 16px;          /* both row and column gap */
  row-gap: 16px;      /* rows only */
  column-gap: 24px;   /* columns only */
}

/* Named template areas */
.layout {
  display: grid;
  grid-template-columns: 250px 1fr;
  grid-template-rows: 60px 1fr 50px;
  grid-template-areas:
    "header  header"
    "sidebar main"
    "footer  footer";
  min-height: 100vh;
}

.header  { grid-area: header; }
.sidebar { grid-area: sidebar; }
.main    { grid-area: main; }
.footer  { grid-area: footer; }

Named template areas are one of Grid's most powerful features — they make complex layouts readable and self-documenting.

Plassere elementer på rutenettet

Grid items can be precisely placed by specifying their start and end lines. Lines are numbered from 1 (or -1 from the end). The span keyword makes items stretch across multiple tracks.

.container {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  grid-template-rows: repeat(4, 100px);
}

/* Place by line numbers */
.item-a {
  grid-column: 1 / 4;     /* span columns 1 to 4 */
  grid-row: 1 / 3;        /* span rows 1 to 3 */
}

/* Using span keyword */
.item-b {
  grid-column: span 2;    /* span 2 columns */
  grid-row: span 3;       /* span 3 rows */
}

/* Shorthand: grid-area: row-start / col-start / row-end / col-end */
.item-c {
  grid-area: 2 / 3 / 4 / 6;
}

/* Alignment */
.container {
  justify-items: center;  /* align items horizontally within cell */
  align-items: center;    /* align items vertically within cell */
  justify-content: space-between; /* align grid horizontally in container */
  align-content: center;          /* align grid vertically in container */
}

/* Per-item alignment */
.item-d {
  justify-self: end;
  align-self: start;
}

Responsive rutenettlayouter

The killer feature of CSS Grid for responsive design is repeat(auto-fill, minmax()). It creates responsive columns without a single media query.

/* Responsive without media queries using auto-fill/auto-fit */
.card-grid {
  display: grid;

  /* auto-fill: creates as many columns as fit */
  /* minmax(min, max): min width 250px, max is 1fr */
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 24px;
}

/* auto-fit vs auto-fill:
   - auto-fill: keeps empty columns (tracks exist, cells empty)
   - auto-fit: collapses empty columns (tracks collapse to 0)
   Use auto-fit when you want items to stretch to fill the row */

.stretch-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
}

/* Media query approach for more control */
.responsive-layout {
  display: grid;
  grid-template-columns: 1fr;     /* mobile: 1 column */
  gap: 16px;
}

@media (min-width: 640px) {
  .responsive-layout {
    grid-template-columns: repeat(2, 1fr); /* tablet: 2 columns */
  }
}

@media (min-width: 1024px) {
  .responsive-layout {
    grid-template-columns: repeat(3, 1fr); /* desktop: 3 columns */
  }
}

CSS Grid vs Flexbox

The question isn't "which is better" but "which is right for this task." Grid and Flexbox are complementary tools, and modern UIs use both together.

/* Flexbox: 1-dimensional (row OR column) */
.flex-nav {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: center;
  /* Perfect for: navbars, button groups, centering single items */
}

/* Grid: 2-dimensional (rows AND columns) */
.grid-dashboard {
  display: grid;
  grid-template-columns: 200px 1fr 300px;
  grid-template-rows: 60px 1fr;
  /* Perfect for: page layouts, card grids, complex UI */
}

/* When to use Flexbox:
   - Aligning items in a single row or column
   - Navigation bars
   - Centering content
   - Distributing space between items
   - Components where item count is unknown

   When to use Grid:
   - Two-dimensional page layouts
   - Card grids where alignment across rows matters
   - Complex UI with overlapping elements
   - When you need items to align to a fixed track structure
*/

/* Modern approach: use both together */
.page {
  display: grid;                          /* outer: grid layout */
  grid-template-columns: 250px 1fr;
}
.sidebar-nav {
  display: flex;                          /* inner: flex for list items */
  flex-direction: column;
  gap: 8px;
}

Avanserte Grid-teknikker

Subgrid, dense auto-placement, and masonry layouts represent the cutting edge of CSS Grid capabilities available in 2026.

/* Subgrid - align nested grids to parent grid (CSS 2023+) */
.parent {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
}
.child {
  display: grid;
  grid-column: span 2;
  grid-template-columns: subgrid; /* inherits parent's column tracks */
}

/* Masonry layout (experimental, Chrome 2024+) */
.masonry {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: masonry;
}

/* Dense auto-placement to fill gaps */
.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  grid-auto-flow: dense; /* fills gaps with smaller items */
}
.large-item {
  grid-column: span 2;
  grid-row: span 2;
}

/* CSS Grid animation */
@keyframes expandCol {
  from { grid-template-columns: 0fr 1fr; }
  to   { grid-template-columns: 1fr 1fr; }
}

Grid-mønstre i virkelige prosjekter

/* Holy Grail Layout */
.holy-grail {
  display: grid;
  grid-template:
    "header" 60px
    "nav main aside" 1fr
    "footer" 50px
    / 200px 1fr 200px;
  min-height: 100vh;
}

/* Magazine/Editorial Layout */
.magazine {
  display: grid;
  grid-template-columns: repeat(12, 1fr);
  gap: 24px;
}
.featured { grid-column: 1 / 9; }
.sidebar  { grid-column: 9 / 13; }
.wide     { grid-column: 1 / -1; } /* full width */

/* Card grid with consistent row heights */
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  grid-auto-rows: 320px; /* fixed row height for alignment */
  gap: 20px;
}

/* Centered content with max-width */
.content-grid {
  display: grid;
  grid-template-columns:
    [full-start] minmax(16px, 1fr)
    [content-start] min(1200px, calc(100% - 32px))
    [content-end] minmax(16px, 1fr)
    [full-end];
}
.content-grid > * { grid-column: content; }
.full-bleed       { grid-column: full; }

Ofte stilte spørsmål

What is the difference between grid-template-columns and grid-auto-columns?

grid-template-columns defines explicit column tracks that you set in advance. grid-auto-columns defines the size of implicitly created columns — columns that are created when items overflow the explicit grid. For example, if you have 3 template columns but 5 items, the 4th and 5th items create implicit tracks sized by grid-auto-columns.

What does the fr unit mean in CSS Grid?

fr stands for "fraction" and represents a fraction of the available space in the grid container. After fixed-size tracks (px, %, min-content, etc.) are measured, the remaining space is divided among fr units. 1fr 2fr gives the first column one third and the second column two thirds of available space.

When should I use auto-fill vs auto-fit in repeat()?

Both create as many columns as fit. The difference appears when items don't fill a complete row: auto-fill keeps the empty track space (items don't stretch beyond their defined max), while auto-fit collapses empty tracks to 0 (causing existing items to stretch and fill the full row width). Use auto-fit when you want items to fill the row; auto-fill when you want consistent column widths.

Can CSS Grid elements overlap?

Yes! This is one of Grid's superpowers. Place two items at the same grid coordinates and they will overlap. Control the stacking order with z-index. This enables pure-CSS image-text overlays, decorative layouts, and complex UI without absolute positioning.

Is CSS Grid supported in all modern browsers?

Yes. CSS Grid has full support across all modern browsers (Chrome, Firefox, Safari, Edge) with 97%+ global coverage. Subgrid (the ability for children to participate in the parent grid) landed in all browsers by 2023. The experimental masonry layout is still behind flags in most browsers as of 2026.

How do I center a single item both vertically and horizontally with Grid?

The simplest approach: set display: grid on the parent, then place: center on the child. The longhand is justify-self: center; align-self: center; on the child. Or use place-items: center on the parent to center all children. This is often simpler than the flexbox approach.

Konklusjon

CSS Grid has matured into an essential layout tool. The combination of explicit placement, auto-placement, named areas, and the fr unit makes it the best solution for complex two-dimensional layouts. Start with the named template areas pattern for your page structure, then use repeat(auto-fill, minmax()) for your card grids.

Use the CSS Formatter tool below to clean up and inspect your grid CSS as you experiment with these patterns.

Relaterte verktøy

𝕏 Twitterin LinkedIn
Var dette nyttig?

Hold deg oppdatert

Få ukentlige dev-tips og nye verktøy.

Ingen spam. Avslutt når som helst.

Try These Related Tools

🌈CSS Gradient Generator📐Flexbox Generator

Related Articles

CSS Flexbox Komplett Guide: Fra Grunnleggende til Avanserte Monstre

Mestre CSS Flexbox med denne komplette guiden om containeregenskaper, justering og vanlige monstre.

CSS Container Queries: Komplett Guide 2026

Mestre CSS container queries: @container-syntaks, størrelsesbegrensning og container-enheter.

CSS :has() Selector: The Parent Selector You Always Wanted (2026)

Master the CSS :has() pseudo-class. Parent selection, form validation without JS, and advanced patterns.