Tillitsdone
down Scroll to discover

CSS Grid A Powerful Layout Tool for Web Design

CSS Grid is a powerful layout system for web developers.

Create complex web pages easily with rows, columns, and named areas.

Explore compatibility, syntax, and examples.
thumbnail

Introduction

CSS Grid is a powerful layout system that helps web developers create flexible and responsive designs. It’s widely supported across browsers and devices, making it a reliable choice for modern web design. CSS Grid lets you define layouts using rows and columns, simplifying the process of designing complex web pages.

CSS Grid Property Overview

The grid property in CSS is a shorthand that combines several grid-related properties into one declaration. This makes it easier to manage the layout of rows, columns, and areas within your grid container. The grid property includes explicit properties like grid-template-rows and grid-template-columns, as well as implicit properties like grid-auto-rows, grid-auto-columns, and grid-auto-flow.

Using the Grid Property

  1. Define the Grid Container: Start by setting the display property of your container element to grid.
.container {
display: grid;
}
  1. Specify Grid Template: Use the grid property to define the rows and columns of your grid.
.container {
display: grid;
grid: repeat(2, 100px) / auto-flow 200px;
}
  1. Auto-Repeat and Auto-Flow: The grid property also allows you to specify how content should auto-repeat and auto-flow within the grid.
.container {
display: grid;
grid: 200px / auto-flow dense;
}
  1. Named Grid Areas: You can use the grid property to define named grid areas.
.container {
display: grid;
grid: 'header header' 100px
'main sidebar' auto / 1fr 1fr;
}

Shorthand Properties and Syntax

The grid property combines several properties into one:

  1. Grid Template: Defines the explicit grid template.
.container {
grid: 'header header' 100px
'main sidebar' auto / 1fr 1fr;
}
  1. Grid Template Rows and Columns: Defines the size and behavior of rows and columns.
.container {
grid: repeat(2, 100px) / auto-flow 200px;
}
  1. Grid Auto Rows and Columns: Defines the size and behavior of implicit rows and columns.
.container {
grid: 200px / auto-flow dense;
}
  1. Grid Auto Flow: Defines how content should auto-flow within the grid.
.container {
grid: auto-flow dense / 200px;
}

Example Uses of the grid Property

Example 1: Basic Grid Layout

HTML:

<div id="container">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>

CSS:

#container {
display: grid;
grid: repeat(2, 60px) / auto-flow 80px;
}
#container > div {
background-color: #8ca0ff;
width: 50px;
height: 50px;
}

Example 2: Named Grid Areas

HTML:

<div id="container">
<div style="grid-area: header">Header</div>
<div style="grid-area: main">Main</div>
<div style="grid-area: sidebar">Sidebar</div>
</div>

CSS:

#container {
display: grid;
grid: 'header header' 100px
'main sidebar' auto / 1fr 1fr;
}
#container > div {
background-color: aliceblue;
text-align: center;
padding: 25px 5px;
font-size: 18px;
}

Example 3: Complex Grid Layout

HTML:

<div class="grid-container">
<div class="header">Header</div>
<div class="main">Main</div>
<div class="sidebar">Sidebar</div>
<div class="footer">Footer</div>
</div>

CSS:

.grid-container {
display: grid;
grid: 'header header' 100px
'main sidebar' auto
'footer footer' 50px / 1fr 2fr;
}
.header, .main, .sidebar, .footer {
background-color: lightblue;
text-align: center;
padding: 10px;
font-size: 18px;
}

Example 4: Responsive Grid Layout

HTML:

<div class="responsive-grid">
<div>Item 1</div>
<div>Item 2</div>
<div>Item 3</div>
<div>Item 4</div>
<div>Item 5</div>
</div>

CSS:

.responsive-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 10px;
padding: 10px;
background-color: lightcoral;
}
.responsive-grid > div {
background-color: lightgrey;
text-align: center;
padding: 20px;
font-size: 18px;
}

Formal Definition and Values of the grid Property

The grid property in CSS is a powerful shorthand that combines several grid-related properties into a single declaration, making it easier to define and manage complex grid layouts. Here’s a formal definition of the grid property and its values:

Initial Value

The initial value of the grid property sets the following defaults:

  • grid-template-rows: none
  • grid-template-columns: none
  • grid-template-areas: none
  • grid-auto-rows: auto
  • grid-auto-columns: auto
  • grid-auto-flow: row
  • grid-column-gap: 0
  • grid-row-gap: 0
  • column-gap: normal
  • row-gap: normal

Applies To

The grid property applies to grid containers.

Inherited

The grid property is not inherited.

Percentages

Percentages refer to the corresponding dimension of the content area for each property.

Computed Value

The computed value of the grid property is as specified, with relative lengths converted into absolute lengths.

Animation Type

The animation type of the grid property is as follows:

  • grid-template-rows: simple list of length, percentage, or calc
  • grid-template-columns: simple list of length, percentage, or calc
  • grid-template-areas: discrete
  • grid-auto-rows: by computed value type
  • grid-auto-columns: by computed value type
  • grid-auto-flow: discrete
  • grid-column-gap: a length
  • grid-row-gap: a length
  • column-gap: a length, percentage, or calc
  • row-gap: a length, percentage, or calc

Values

The grid property accepts the following values:

<'grid-template'>

Defines the explicit grid template, including grid-template-rows, grid-template-columns, and grid-template-areas.

.container {
grid: 'header header' 100px
'main sidebar' auto / 1fr 1fr;
}

<'grid-template-rows'> / [auto-flow && dense?] <'grid-auto-columns'>?

Sets up an auto-flow by setting the row tracks explicitly via the grid-template-rows property and specifying how to auto-repeat the column tracks via grid-auto-columns.

.container {
grid: 200px / auto-flow 80px;
}

[auto-flow && dense?] <'grid-auto-rows'>? / <'grid-template-columns'>

Sets up an auto-flow by setting the column tracks explicitly via the grid-template-columns property and specifying how to auto-repeat the row tracks via grid-auto-rows.

.container {
grid: auto-flow dense / 200px;
}

Understanding the grid-auto-flow Property

The grid-auto-flow property in CSS Grid can use the dense keyword to fill any available space more efficiently. Here’s a simple example:

.container {
grid: auto-flow dense / 200px;
}

In this example, the grid container will have rows that auto-flow densely, filling any available space, with a column width of 200 pixels.

Formal Syntax of the grid Property

The formal syntax for the grid property is:

grid = [<'grid-template'>]
[<'grid-template-rows'> / [auto-flow && dense?] <'grid-auto-columns'>?]
[auto-flow && dense?] <'grid-auto-rows'>? / <'grid-template-columns'>
initial | inherit;

Values of the grid Property

The grid property accepts the following values:

  • <'grid-template'>: Defines the explicit grid template, including rows, columns, and areas.
  • <'grid-template-rows'> / [auto-flow && dense?] <'grid-auto-columns'>?: Sets up rows explicitly and auto-repeats columns.
  • [auto-flow && dense?] <'grid-auto-rows'>? / <'grid-template-columns'>: Sets up columns explicitly and auto-repeats rows.

Example of Using the grid Property

Here’s an example that combines several shorthand properties:

HTML:

<div class="grid-container">
<div class="header">Header</div>
<div class="main">Main</div>
<div class="sidebar">Sidebar</div>
<div class="footer">Footer</div>
</div>

CSS:

.grid-container {
display: grid;
grid: 'header header' 100px
'main sidebar' auto
'footer footer' 50px / 1fr 2fr;
}
.header, .main, .sidebar, .footer {
background-color: lightblue;
padding: 10px;
text-align: center;
}

Result:

This creates a grid layout with named areas for the header, main content, sidebar, and footer, with specific row heights and column widths.

Browser Support for CSS Grid

  • Google Chrome: Version 57 and above (Released in March 2017)
  • Mozilla Firefox: Version 52 and above (Released in March 2017)
  • Microsoft Edge: Version 16 and above (Released in September 2017)
  • Opera: Version 44 and above (Released in March 2017)
  • Safari: Version 10.1 and above (Released in September 2016)

Key Points on Browser Support

  1. Wide Availability: CSS Grid is supported by all major web browsers, making your web designs accessible to a wide audience.
  2. Consistent Performance: CSS Grid behaves consistently across different browsers, providing a stable foundation for your web design projects.
  3. Future-Proof: With strong baseline support, CSS Grid is a future-proof solution for creating modern and responsive web layouts.

Compatibility Table

BrowserVersionRelease Date
Google Chrome57+March 2017
Mozilla Firefox52+March 2017
Microsoft Edge16+September 2017
Opera44+March 2017
Safari10.1+September 2016

Ensuring Cross-Browser Compatibility

To ensure that your CSS Grid layouts work consistently across all browsers, consider these best practices:

  1. Test Across Browsers: Regularly test your web designs on different browsers and versions to identify and fix any compatibility issues.
  2. Use Feature Queries: Implement feature queries (@supports) to provide fallback layouts for browsers that do not fully support CSS Grid.
  3. Stay Updated: Keep your browser versions up to date to take advantage of the latest features and improvements.

Example of Feature Query

Here’s how to use a feature query to provide a fallback layout for browsers that do not support CSS Grid:

/* Fallback layout for non-supporting browsers */
.container {
display: flex;
flex-wrap: wrap;
}
/* Grid layout for supporting browsers */
@supports (display: grid) {
.container {
display: grid;
grid: repeat(2, 60px) / auto-flow 80px;
}
}

Conclusion

CSS Grid is a powerful and versatile tool for creating modern and responsive web layouts. By using the grid property, you can define complex layouts with minimal code, making your web development process more efficient. Whether you’re creating a basic grid layout, using named grid areas, or designing a responsive layout, CSS Grid provides the flexibility and control you need to achieve your design goals.

By leveraging the related properties and resources, you can enhance your web design skills and create more sophisticated and responsive layouts. CSS Grid is a future-proof solution that ensures your web designs are accessible and functional across various browsers and devices.

Related Properties and Resources

To get the most out of CSS Grid, here are some key properties and resources to explore:

Key Grid Properties

  1. grid-template: Defines the explicit grid template, including rows, columns, and areas.
    .container {
    grid-template: 'header header' 100px
    'main sidebar' auto / 1fr 1fr;
    }
  2. grid-template-rows: Defines the size and behavior of rows.
    .container {
    grid-template-rows: 100px auto;
    }
  3. grid-template-columns: Defines the size and behavior of columns.
    .container {
    grid-template-columns: 1fr 2fr;
    }
  4. grid-template-areas: Defines named grid areas.
    .container {
    grid-template-areas: 'header header'
    'main sidebar';
    }
  5. grid-auto-rows: Defines the size and behavior of implicit rows.
    .container {
    grid-auto-rows: auto;
    }
  6. grid-auto-columns: Defines the size and behavior of implicit columns.
    .container {
    grid-auto-columns: auto;
    }
  7. grid-auto-flow: Defines how content should auto-flow within the grid.
    .container {
    grid-auto-flow: row dense;
    }
  8. grid-gap, grid-column-gap, grid-row-gap: Defines the gap between grid items.
    .container {
    grid-gap: 10px;
    }

Additional Resources

  1. Line-based placement with CSS grid: Learn more about line-based placement.
    • ( WebsiteUrl )
  2. Grid template areas: grid definition shorthands: Explore grid definition shorthands for named grid areas.
    • ( WebsiteUrl )
  3. CSS Grid Layout Module Level 2: Official documentation and specifications for CSS Grid.
    • ( WebsiteUrl )
  4. Browser Compatibility Tables: Check the compatibility of CSS Grid across different browsers.
    • ( WebsiteUrl )

By understanding these related properties and resources, you can create complex and responsive layouts with ease. CSS Grid offers a powerful and flexible way to design web pages, making it an essential tool for modern web development.

Conclusion

The CSS Grid property is a powerful and versatile tool for creating modern and responsive web layouts. By using the grid property, you can define complex layouts with minimal code, making your web development process more efficient. Whether you’re creating a basic grid layout, using named grid areas, or designing a responsive layout, CSS Grid provides the flexibility and control you need to achieve your design goals.

By leveraging the related properties and resources, you can enhance your web design skills and create more sophisticated and responsive layouts. CSS Grid is a future-proof solution that ensures your web designs are accessible and functional across various browsers and devices.

icons/css-4.svg CSS Blogs
CSS3 is the latest version of Cascading Style Sheets, offering advanced styling features like animations, transitions, shadows, gradients, and responsive design.
icons/logo-tid.svg

Talk with CEO

Ready to bring your web/app to life or boost your team with expert Thai developers?
Contact us today to discuss your needs, and let’s create tailored solutions to achieve your goals. We’re here to help at every step!
🖐️ Contact us
Let's keep in Touch
Thank you for your interest in Tillitsdone! Whether you have a question about our services, want to discuss a potential project, or simply want to say hello, we're here and ready to assist you.
We'll be right here with you every step of the way.
Contact Information
rick@tillitsdone.com+66824564755
Find All the Ways to Get in Touch with Tillitsdone - We're Just a Click, Call, or Message Away. We'll Be Right Here, Ready to Respond and Start a Conversation About Your Needs.
Address
9 Phahonyothin Rd, Khlong Nueng, Khlong Luang District, Pathum Thani, Bangkok Thailand
Visit Tillitsdone at Our Physical Location - We'd Love to Welcome You to Our Creative Space. We'll Be Right Here, Ready to Show You Around and Discuss Your Ideas in Person.
Social media
Connect with Tillitsdone on Various Social Platforms - Stay Updated and Engage with Our Latest Projects and Insights. We'll Be Right Here, Sharing Our Journey and Ready to Interact with You.
We anticipate your communication and look forward to discussing how we can contribute to your business's success.
We'll be here, prepared to commence this promising collaboration.
Frequently Asked Questions
Explore frequently asked questions about our products and services.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.