
- CSS - Home
- CSS - Roadmap
- CSS - Introduction
- CSS - Syntax
- CSS - Inclusion
- CSS - Types
- CSS - Measurement Units
- CSS - Selectors
- CSS - Colors
- CSS - Backgrounds
- CSS - Fonts
- CSS - Text
- CSS - Images
- CSS - Links
- CSS - Tables
- CSS - Borders
- CSS - Border Block
- CSS - Border Inline
- CSS - Margins
- CSS - Lists
- CSS - Padding
- CSS - Cursor
- CSS - Outlines
- CSS - Dimension
- CSS - Scrollbars
- CSS - Inline Block
- CSS - Dropdowns
- CSS - Visibility
- CSS - Overflow
- CSS - Clearfix
- CSS - Float
- CSS - Arrows
- CSS - Resize
- CSS - Quotes
- CSS - Order
- CSS - Position
- CSS - Hyphens
- CSS - Hover
- CSS - Display
- CSS - Focus
- CSS - Zoom
- CSS - Translate
- CSS - Height
- CSS - Hyphenate Character
- CSS - Width
- CSS - Opacity
- CSS - Z-Index
- CSS - Bottom
- CSS - Navbar
- CSS - Overlay
- CSS - Forms
- CSS - Align
- CSS - Icons
- CSS - Image Gallery
- CSS - Comments
- CSS - Loaders
- CSS - Attr Selectors
- CSS - Combinators
- CSS - Root
- CSS - Box Model
- CSS - Counters
- CSS - Clip
- CSS - Writing Mode
- CSS - Unicode-bidi
- CSS - min-content
- CSS - All
- CSS - Inset
- CSS - Isolation
- CSS - Overscroll
- CSS - Justify Items
- CSS - Justify Self
- CSS - Tab Size
- CSS - Pointer Events
- CSS - Place Content
- CSS - Place Items
- CSS - Place Self
- CSS - Max Block Size
- CSS - Min Block Size
- CSS - Mix Blend Mode
- CSS - Max Inline Size
- CSS - Min Inline Size
- CSS - Offset
- CSS - Accent Color
- CSS - User Select
- CSS - Cascading
- CSS - Universal Selectors
- CSS - ID Selectors
- CSS - Group Selectors
- CSS - Class Selectors
- CSS - Child Selectors
- CSS - Element Selectors
- CSS - Descendant Selectors
- CSS - General Sibling Selectors
- CSS - Adjacent Sibling Selectors
- CSS Advanced
- CSS - Grid
- CSS - Grid Layout
- CSS - Flexbox
- CSS - Visibility
- CSS - Positioning
- CSS - Layers
- CSS - Pseudo Classes
- CSS - Pseudo Elements
- CSS - @ Rules
- CSS - Text Effects
- CSS - Paged Media
- CSS - Printing
- CSS - Layouts
- CSS - Validations
- CSS - Image Sprites
- CSS - Important
- CSS - Data Types
- CSS3 Advanced Features
- CSS - Rounded Corner
- CSS - Border Images
- CSS - Multi Background
- CSS - Color
- CSS - Gradients
- CSS - Box Shadow
- CSS - Box Decoration Break
- CSS - Caret Color
- CSS - Text Shadow
- CSS - Text
- CSS - 2d transform
- CSS - 3d transform
- CSS - Transition
- CSS - Animation
- CSS - Multi columns
- CSS - Box Sizing
- CSS - Tooltips
- CSS - Buttons
- CSS - Pagination
- CSS - Variables
- CSS - Media Queries
- CSS - Functions
- CSS - Math Functions
- CSS - Masking
- CSS - Shapes
- CSS - Style Images
- CSS - Specificity
- CSS - Custom Properties
- CSS Responsive
- CSS RWD - Introduction
- CSS RWD - Viewport
- CSS RWD - Grid View
- CSS RWD - Media Queries
- CSS RWD - Images
- CSS RWD - Videos
- CSS RWD - Frameworks
- CSS References
- CSS Interview Questions
- CSS Online Quiz
- CSS Online Test
- CSS Mock Test
- CSS - Quick Guide
- CSS - Cheatsheet
- CSS - Properties References
- CSS - Functions References
- CSS - Color References
- CSS - Web Browser References
- CSS - Web Safe Fonts
- CSS - Units
- CSS - Animation
- CSS Resources
- CSS - Useful Resources
- CSS - Discussion
CSS - grid-template Property
CSS grid-template property specifies the grid columns, grid rows and grid areas for the grid layout. The property is a shorthand for the individual grid-related properties: grid-template-areas, grid-template-columns and grid-template-rows
Syntax
grid-template: none | grid-template-rows / grid-template-columns | grid-template-areas | initial | inherit;
Property Values
Value | Description |
---|---|
none | No specific size for the columns and rows is set. Default. |
grid-template-rows / grid-template-columns | It specifies the size(s) of the rows and columns. |
grid-template-area | It defines the grid layout using named grid items. |
initial | This sets the property to its default value. |
inherit | This inherits the property from the parent element. |
Examples of CSS Grid Template Property
The following examples explain the grid-template property with different values.
Grid Template Property with None Value
To not have any predefined rows, columns, or areas, we use the none value which indicates that no explicit grid template is defined. The grid will automatically adjust based on the content and layout of the grid items. This is shown in the following example.
Example
<!DOCTYPE html> <html> <head> <style> .container { display: grid; background-color: lightgrey; grid-gap: 10px; padding: 20px; grid-template: none; } .container>div { padding: 10px; text-align: center; color: white; border: 3px solid blue; background-color: #4775d1; } </style> </head> <body> <h2> CSS grid-template property </h2> <h4> grid-template: none </h4> <div class=" container container1"> <div> Item-1 </div> <div> Item-2 </div> <div> Item-3 </div> <div> Item-4 </div> </div> </body> </html>
Grid Template Property for Rows and Columns
To define the sizes of the rows and columns in the grid layout, we specify the height of the rows and the width of the columns separated by a '/' to the grid-template property. The number of sizes specified before the '/' indicate the number of rows and the number of sizes specified after the '/' indicate the number of columns. These are shown in the following example.
Example
<!DOCTYPE html> <html> <head> <style> .container { display: grid; height: 200px; background-color: lightgrey; grid-gap: 20px; padding: 10px; } .container1 { grid-template: 120px 60px / auto auto auto; } .container2 { grid-template: 100px 60px / 160px 130px 160px; } .container>div { padding: 10px; text-align: center; color: white; border: 3px solid blue; background-color: #4775d1; } </style> </head> <body> <h2> CSS grid-template property </h2> <p> <strong> grid-template: 120px 60px / auto auto auto (2 rows and three columns) </strong> ; the first row is 120px high and second row is 60px high, the width of the columns has been set to auto and thus depends on the content. </p> <div class=" container container1"> <div> Item-1 </div> <div> Item-2 </div> <div> Item-3 </div> <div> Item-4 </div> <div> Item-5 </div> <div> Item-6 </div> </div> <p> <strong> grid-template: 100px 60px / 160px 130px 160px (2 rows and three columns) </strong> ; the first row is 100px high and second row is 60px high, the first column is 160px wide, second column is 130px wide, third column is 160px wide. </p> <div class="container container2"> <div> Item-1 </div> <div> Item-2 </div> <div> Item-3 </div> <div> Item-4 </div> <div> Item-5 </div> <div> Item-6 </div> </div> </body> </html>
Grid Template Property with Named Grid Items
To define named grid areas within the grid container, we specify the rows using apostrophes '' and the columns with the number of names inside them to the grid-template property. An element needs to use the name inside the apostrophes to refer to the portion of the layout. This is shown in the following example.
Example
<!DOCTYPE html> <html> <head> <style> .container { display: grid; background-color: lightgrey; grid-gap: 10px; padding: 20px; } .container>div { padding: 20px; text-align: center; color: white; border: 3px solid blue; background-color: #4775d1; } .container1 { grid-template: 'header header header header' 'sidebar content content content' 'footer footer footer footer'; } .container2 { grid-template: 'item item ..' 'item item..'; } .cont1-item1 { grid-area: header; } .cont1-item2 { grid-area: sidebar; } .cont1-item3 { grid-area: content; } .cont1-item4 { grid-area: footer; } .cont2-item1 { grid-area: item; } </style> </head> <body> <h2> CSS grid-template property </h2> <p> <strong> grid-template: 'header header header header' 'sidebar content content content' 'footer footer footer footer' </strong> ; The layout has three rows indicated by three different apostrophes' ' and four columns indicated by the number of items inside apostrophes ' ' </p> <div class=" container container1"> <div class="cont1-item1"> This is header </div> <div class="cont1-item2"> This is sidebar </div> <div class="cont1-item3"> This is content </div> <div class="cont1-item4"> This is footer </div> </div> <p> <strong> grid-template: 'item item . .''item item . .' </strong> ; item1 takes two rows indicated by two separate apostrophes ' ' and 4 columns indicated by the number of items inside apostrophes ' '</p> <div class=" container container2"> <div class="cont2-item1"> Item-1 </div> <div> Item-2 </div> <div> Item-3 </div> <div> Item-4 </div> <div> Item-5 </div> <div> Item-6 </div> </div> </body> </html>
Supported Browsers
Property | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
grid-template | 57 | 16 | 52 | 10 | 44 |