NV Elements Catalog Starters Repo System Themes About Getting Started Changelog Metrics Support Accessibility Contributions Requests Migration Deprecations Integrations Installation MCP CLI Lint Angular Bundles Extensions Go Hugo Import Maps Lit NextJS Nuxt Preact React SolidJS Svelte TypeScript Vue Foundations Typography Iconography Themes Design Tokens Size & Space Objects Interactions Support Status Color Animation Fonts Layers Custom Layout Horizontal Vertical Grid Popovers i18n Visualization View Transitions Elements Accordion Alert Avatar Badge Breadcrumb Button Button Group Card Chat Message Checkbox Color Combobox Copy Button Datagrid Integrations Column Action Column Alignment Column Fixed Column width Container Card Display Settings Footer Heatmap Keynav Multi Select Pagination Panel Detail Panel Grid Performance Placeholder Row Action Row Groups Row Sort Scroll Height Single Select Stripe Date Datetime Dialog Divider Dot Drawer Dropdown Dropdown Group Dropzone File Forms Validation Actions Control Icon Icon Button Input Input Group Logo Menu Month Notification Page Page Header Page Loader Pagination Panel Progressive Filter Chip Progress Bar Progress Ring Password Preferences Input Pulse Radio Range Resize Handle Search Select Skeleton Sort Button Sparkline Star Rating Steps Switch Tabs Tag Textarea Time Toast Toggletip Toolbar Tooltip Tree Week Patterns Authentication Browse Dashboard Editor Empty States Heatmap Keyboard Shortcut Logging Media Onboarding Panel Responsive Search Subheader Trend Code Codeblock Monaco Input Diff Input Editor Diff Editor Problems Markdown Markdown CSS Utility Labs Responsive Layout Viewport Container Patterns Forms API Design Properties & Attributes Slots Registration CustomEvents Stateless Composition Styles Packaging Glossary Logs Internal Guidelines Agent Harness Documentation Examples TypeScript Testing Unit Testing Accessibility Testing Lighthouse Testing SSR Testing Visual Testing Troubleshooting Component Creation Internal Examples All Examples

Grid Layout

CSS Grid provides two-dimensional control over both rows and columns. The Elements grid system implements CSS Grid through the nve-layout API to simplify complex layout creation.

Learn more about CSS Grid in the MDN Grid Layout guide, this comprehensive CSS-Tricks Grid guide, and practice with the interactive CSS Grid Garden game.

Understanding Grid Layout

Grid layout creates structured, magazine-style layouts where precise placement and alignment matter. Unlike flexbox which is primarily one-dimensional, grid provides control over both dimensions simultaneously.

The Elements grid system uses a 12-column layout, a standard approach that provides flexibility for common design patterns.

When to Use Grid

Choose grid layout when you need:

  • Multi-column layouts: Dashboards, card galleries, product listings
  • Precise alignment: Forms with labels and inputs, data tables
  • Complex page structures: Magazine layouts, image galleries with captions
  • Responsive design: Layouts that reflow based on available space
  • Overlapping elements: Layered designs with precise positioning
Grid vs Flexbox: use grid when you need to control both rows and columns. Use flexbox for one-dimensional layouts (either row or column). You can combine both systems—use grid for page structure and flexbox for component internals.

Basic Grid Setup

Set nve-layout="grid" on a container element and control child element sizing through either:

  1. Parent-controlled sizing with span-items:... (all children same width)
  2. Child-controlled sizing with individual span:... values (flexible widths)

    

Column Spanning

The 12-column system offers these common patterns:

  • Full width: span:12 (1 item per row)
  • Half width: span:6 (2 items per row)
  • Thirds: span:4 (3 items per row)
  • Quarters: span:3 (4 items per row)
  • Sixths: span:2 (6 items per row)
  • Twelfths: span:1 (12 items per row)

Grid Alignment

Like flexbox layouts, grid supports comprehensive alignment options that control how items position themselves within the grid container:

  • align:left
  • align:right
  • align:top
  • align:bottom
  • align:center
  • align:horizontal-center
  • align:vertical-center
  • align:stretch
  • align:horizontal-stretch
  • align:vertical-stretch
Grid alignment works on the entire grid container. For individual item alignment, consider wrapping items in flexbox containers or using CSS custom properties for fine-tuned control. See the child-controlled spans section for more granular layout control options.

Grid Layout Examples

Span Items 2

Span Items 6

Child-controlled Spans vs Parent-controlled Spans

The Elements grid system offers two approaches for controlling column spans, each suited to different design needs:

When to Use Parent-controlled Spans (span-items)

Use span-items on the grid container when:

  • Uniform layouts: All items should have the same width
  • Gallery grids: Photo galleries, card grids, or product listings
  • Predictable patterns: Dashboard widgets, metric cards, or feature boxes
  • Simplified markup: You want cleaner HTML without span attributes on each child
  • Dynamic content: When the system generates items dynamically and they should maintain consistent sizing

    

When to Use Child-controlled Spans (span)

Use individual span attributes on grid items when:

  • Mixed layouts: Different items need different widths
  • Editorial designs: Magazine-style layouts with varied content sizes
  • Form layouts: Labels, inputs, and buttons of different widths
  • Asymmetric designs: Sidebars, main content areas, and feature blocks
  • Precise control: Each element needs specific column spanning

    

Combining Both Approaches

You can mix both approaches for the most flexibility. Items with explicit span values override the parent's span-items setting:


    

Performance Considerations

  • Parent-controlled (span-items): More efficient for large grids with uniform items
  • Child-controlled (span): Better for complex layouts with specific requirements
  • Mixed approach: Use sparingly to maintain code clarity

Child-controlled Spans Example

Align Top

Align Vertical Center

Align Bottom

Align Horizontal Center

Align Center

Align Center and Bottom

Align Top and Right

Align Center and Right

Align Bottom and Right

Align Horizontal Stretch

Align Vertical Stretch

Align Full Stretch

Common Grid Patterns

Dashboard Layout


    

Form Layout


    

    

Best Practices

  1. Start with 12 columns: This provides flexibility for responsive design patterns
  2. Use semantic HTML: Apply grid to meaningful containers like <main>, <section>, <nav>
  3. Consistent gaps: Use standard gap spacing throughout your layout
  4. Plan for responsiveness: Consider how your grid adapts on smaller screens
  5. Combine with flexbox: Use grid for layout structure, flexbox for component details
Browser Support: all modern browsers support CSS Grid. For older browser support, ensure you have appropriate fallbacks or polyfills in place.

Next Steps