top of page
Writer's pictureThe Tech Platform

Grid-Template Property

The grid-template property is a shorthand property for the following properties:

  • grid-template-rows

  • grid-template-columns

  • grid-template-areas


Syntax:

grid-template: none|grid-template-rows / grid-template-columns|grid-template-areas|initial|inherit;

Where,

  • none: Default value. No specific sizing of the columns or rows

  • grid-template rows / grid-template-columns: Specifies the size(s) of the columns and rows

  • grid-template-areas: Specifies the grid layout using named items

  • initial: Sets this property to its default value.

  • inherit: Inherits this property from its parent element.


Example:

<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
  display: grid;
  grid-template: 200px / auto auto auto;
  grid-gap: 10px;
  background-color: orange;
  padding: 10px;
}

.grid-container > div {
  background-color: rgba(255, 255, 255, 0.8);
  text-align: center;
  padding: 20px 0;
  font-size: 30px;
}
</style>
</head>
<body>

<h2>Grid-Template Property</h2>
<p>This grid layout has three columns, and the first row is 200px high:</p>

<div class="grid-container">
  <div class="item1">1</div>
  <div class="item2">2</div>
  <div class="item3">3</div>  
  <div class="item4">4</div>
  <div class="item5">5</div>
  <div class="item6">6</div>
</div>

</body>
</html>

Output:


Grid-Row-Template

The grid-template-rows property specifies the number (and the heights) of the rows in a grid layout. The values are a space-separated list, where each value specifies the height of the respective row.


Syntax:

grid-template-rows: none|auto|max-content|min-content|length|initial|inherit;

Where,

  • max-content: Sets the size of each row to depend on the largest item in the row

  • min-content: Sets the size of each row to depend on the smallest item in the row

  • length: Sets the size of the rows, by using a legal length value.


Example:

<!DOCTYPE html>
<html>
<head>
<style>
.grid-container {
  display: grid;
  grid-template-columns: auto auto auto auto;
  grid-template-rows: 100px 200px;
  grid-gap: 10px;
  background-color: Orange;
  padding: 10px;
}

.grid-container > div {
  background-color: rgba(255, 255, 255, 0.8);
  text-align: center;
  padding: 20px 0;
  font-size: 30px;
}
</style>
</head>
<body>

<h2>Grid-Template-Rows Property</h2>
<p>The first row is 100px high, and the second row is 300px high:</p>

<div class="grid-container">
  <div class="item1">1</div>
  <div class="item2">2</div>
  <div class="item3">3</div>  
  <div class="item4">4</div>
  <div class="item5">5</div>
  <div class="item6">6</div>
  <div class="item7">7</div>
  <div class="item8">8</div>
</div>

</body>
</html>

Output:



Grid-Column-Template

The grid-template-columns property specifies the number (and the widths) of columns in a grid layout. The values are a space separated list, where each value specifies the size of the respective column.


Syntax:

grid-template-columns: none|auto|max-content|min-content|length|initial|inherit;

Where,

  • auto: The size of the columns is determined by the size of the container and on the size of the content of the items in the column

  • max-content: Sets the size of each column to depend on the largest item in the column

  • min-content: Sets the size of each column to depend on the smallest item in the column

  • length: Sets the size of the columns, by using a legal length value.


Example:

<!DOCTYPE html>
<html>
<head>
<style>

.grid-container {
  display: grid;
  grid-template-columns: 200px 400px;
  grid-gap: 10px;
  background-color: orange;
  padding: 10px;
}

.grid-container > div {
  background-color: rgba(255, 255, 255, 0.8);
  text-align: center;
  padding: 20px 0;
  font-size: 30px;
}
</style>
</head>
<body>

<h2>Grid-Template-Columns Property</h2>
<p>In this grid First Column is 200px wide and Second column is 400px wide.</p>

<div class="grid-container">
  <div class="item1">1</div>
  <div class="item2">2</div>
  <div class="item3">3</div>  
  <div class="item4">4</div>
  <div class="item5">5</div>
  <div class="item6">6</div>
  <div class="item7">7</div>
  <div class="item8">8</div>
</div>

</body>
</html>

Output:



Grid-Area-Template

The grid-template-areas property specifies areas within the grid layout. You can name grid items by using the grid-area property, and then reference to the name in the grid-template-areas property. Each area is defined by apostrophes. Use a period sign to refer to a grid item with no name.


Syntax:

grid-template-areas: none|itemnames;

Where,

  • itemnames: A sequence that specifies how each columns and row should display


Example:

<!DOCTYPE html>
<html>
<head>
<style>
.item1 {
  grid-area: myArea;
}

.grid-container {
  display: grid;
  grid-template-areas: 'myArea myArea . . .';
  grid-gap: 10px;
  background-color: orange;
  padding: 10px;
}

.grid-container > div {
  background-color: rgba(255, 255, 255, 0.8);
  text-align: center;
  padding: 20px 0;
  font-size: 30px;
}
</style>
</head>
<body>

<h2>Grid-Template-Areas Property</h2>
<p>Item1, is called "myArea" and will take up the place of two columns (out of five):</p>

<div class="grid-container">
  <div class="item1">1</div>
  <div class="item2">2</div>
  <div class="item3">3</div>  
  <div class="item4">4</div>
  <div class="item5">5</div>
  <div class="item6">6</div>
  <div class="item7">7</div>
  <div class="item8">8</div>
  <div class="item9">9</div>
</div>

</body>
</html>

Output:



The Tech Platform


0 comments

Comments


bottom of page