Skip to main content
April 1, 2026Dan Rodney/5 min read

Grid: Vertical Centering on a Full Screen Background

Master CSS Grid for Perfect Full Screen Layouts

CSS Grid vs Flexbox for Centering

CSS Grid Approach

Uses grid-template-rows and align-items for precise control over vertical positioning. Better for complex layouts with multiple rows.

Flexbox Alternative

Previously covered method using flex properties. Simpler for basic centering but less control over complex layouts.

Topics Covered in This Web Development Tutorial:

Mastering vertical alignment with CSS Grid — a modern, robust approach to centering content that surpasses traditional methods in both flexibility and browser compatibility.

Exercise Preview

full screen bg done

Exercise Structure

This exercise builds on previous flexbox centering techniques, demonstrating how CSS Grid provides more granular control for full-screen background layouts.

Exercise Overview

In this exercise, you'll harness CSS Grid's powerful alignment capabilities to center content both vertically and horizontally within the viewport. While we previously accomplished similar results using Flexbox, Grid offers distinct advantages for complex layouts — particularly when you need precise control over both dimensions simultaneously. This technique is essential for creating professional hero sections, landing pages, and full-screen interfaces that maintain perfect centering across all device sizes and orientations.

By the end of this exercise, you'll understand not just how to center content with Grid, but why this approach has become the preferred method for many developers working on modern web applications.

Grid Centering Process Overview

1

Set Grid Container

Apply display: grid to the header element with min-height: 100vh for full viewport height

2

Define Grid Rows

Create two rows - auto height for heading content and 60px for the scroll arrow

3

Center Content

Use justify-items and align-items to center elements both horizontally and vertically

Getting Started

  1. In your code editor, close any files you may have open to maintain a clean workspace.
  2. For this exercise, navigate to the Grid Vertical Centering folder located in Desktop > Class Files > yourname-Flexbox Grid Class. If you're using Visual Studio Code or another modern editor that supports folder-based workflows, open the entire folder to take advantage of your editor's project navigation features.
  3. Open index.html from the Grid Vertical Centering folder.
  4. Preview index.html in a modern browser (Chrome, Firefox, Safari, or Edge).

    • Notice how the photo background covers the full viewport dimensions, but the content appears misaligned. This is exactly the type of layout challenge that Grid was designed to solve elegantly.
  5. Keep index.html open in your browser throughout this exercise. This live-reload workflow allows you to immediately see the visual impact of each CSS change — a crucial habit for efficient front-end development.
  6. Return to your code editor and examine the project structure.
  7. Locate the header tag in the HTML markup.
  8. Within the header element, you'll find the heading and anchor tags that comprise our content to be centered. Understanding this semantic structure is key to applying Grid properties effectively.

Setup Requirements

0/4
Development Workflow

Keep your browser open alongside your code editor to immediately see changes as you modify the CSS. This iterative approach speeds up development.

Implementing Grid-Based Vertical Centering

Now we'll transform this basic layout into a perfectly centered composition using CSS Grid's alignment properties. This approach gives us granular control over both the overall layout structure and individual item positioning.

  1. Open main.css from the css folder within your Grid Vertical Centering directory.
  2. In the existing header rule, add the following Grid properties:

    header {

    Code Omitted To Save Space

    min-height: 100vh;
       display: grid;
       grid-template-rows: auto 60px;
       justify-items: center;
       align-items: center;
    }

    Here's what each property accomplishes: display: grid establishes a Grid formatting context; grid-template-rows creates two rows — one flexible row for the heading content and a fixed 60px row for the scroll indicator; justify-items: center horizontally centers all grid items; and align-items: center vertically centers content within each row. The 60px height accounts for the 40px arrow icon plus 20px of desired spacing below.

  3. Save the file and reload your browser.

    • You should see immediate improvement in the content positioning. However, some refinements are needed for optimal presentation across different viewport sizes.
    • The scroll arrow is currently centered within its 60px row, creating uneven spacing below. We'll address this with targeted alignment adjustments.
    • Test the layout on narrow screens — you'll notice the heading text defaults to left alignment on mobile devices, breaking the centered aesthetic we're building.
  4. Return to your code editor to fine-tune the typography alignment.
  5. In the h1 rule, add explicit text centering:

    h1 {

    Code Omitted To Save Space

    margin: 0;
       text-align: center;
    }
  6. Save and reload to test the changes.

    • The heading now maintains perfect horizontal centering across all screen sizes — a critical detail for professional-grade responsive design.
  7. Switch back to your code editor to refine the scroll indicator positioning.
  8. In the .scroll-down rule, add precise alignment control:

    .scroll-down {

    Code Omitted To Save Space

    opacity:.6;
       align-self: start;
    }
  9. Save and reload to observe the positioning improvement.

    • The align-self: start property overrides the parent's centering behavior for this specific element, positioning the arrow at the top of its grid row. This creates the desired 20px spacing below the 40px icon.
    • While the layout now functions well, you might notice the heading sits slightly high due to the space allocated for the scroll indicator. For absolute vertical centering, we'll make one final adjustment.
  10. Return to your code editor for the final optimization.
  11. In the header rule, add compensatory spacing:

    header {

    Code Omitted To Save Space

    align-items: center;
       padding-top: 60px;
    }
  12. Save and perform a final browser reload.

    • Excellent! The 60px top padding perfectly balances the 60px bottom row, achieving true vertical centering of the heading content. Test your layout across multiple viewport sizes to appreciate the robust responsiveness of this Grid-based approach.

This Grid technique provides superior control compared to older centering methods and maintains excellent browser support across modern web environments. You now have a production-ready pattern for creating compelling hero sections and full-screen interfaces.

Key Takeaways

1CSS Grid provides more granular control than Flexbox for complex full-screen layouts with multiple elements
2The grid-template-rows property allows precise definition of row heights for consistent spacing
3Using min-height: 100vh ensures the grid container spans the full viewport height
4The align-self property can override grid-level alignment for individual elements
5Combining justify-items and align-items centers content both horizontally and vertically within grid cells
6Mobile responsiveness requires additional considerations like text-align: center for proper text alignment
7Strategic padding can fine-tune visual centering when mathematical centering doesn't achieve the desired aesthetic
8Real-time browser preview during development accelerates the iterative design process

RELATED ARTICLES