Difference between revisions of "CSS Grid"

From Publication Station
Line 41: Line 41:


You can set the amount of columns by adding or removing values:
You can set the amount of columns by adding or removing values:
* {{code|inline=y|lang=css|grid-template-columns: 200px 200px;}} will create two columns of each 200 pixels width. *
* {{code|inline=y|lang=css|grid-template-columns: 200px 200px;}} will create two columns of each 200 pixels width.  
* {{code|inline=y|lang=css|grid-template-columns: 200px 200px 200px;}} will create 3 columns of each 200 pixels width
* {{code|inline=y|lang=css|grid-template-columns: 200px 200px 200px;}} will create 3 columns of each 200 pixels width
* {{code|inline=y|lang=css|grid-template-columns: 300px 500px 500px;}} will create 3 columns, the first and last 200 pixels wide. The middle column 500 pixels wide.
* {{code|inline=y|lang=css|grid-template-columns: 300px 500px 500px;}} will create 3 columns, the first and last 200 pixels wide. The middle column 500 pixels wide.
Line 47: Line 47:
The following snippet combines columns and rows to create a grid.
The following snippet combines columns and rows to create a grid.
{{Columns}}
{{Columns}}
{{Column}}
{{Column}}<source lang="html5">
<source lang="html5">
<div class="grid-container">
<div class="grid-container">
   <div>Grid item 1</div>
   <div>Grid item 1</div>
Line 65: Line 64:
}
}
</style>
</style>
</source>
{{ColumnEnd}}
{{Column}}[[File:CSS-grid-display-grid-with-rows.png]]{{ColumnEnd}}
{{ColumnsEnd}}


Instead of pixels you can also use {{code|inline=y|lang=css|fr}} which stands for fraction (for example 1/2 or 1/4). This is super handy to create fluid grids.


In the following snippet we set {{code|inline=y|lang=css|grid-template-columns: 1fr 1fr 1fr 1fr;}}. Which means each column will be around 25% (1/4).
We also add {{code|inline=y|lang=css|grid-column-gap: 10px;}} which sets the gutter to 10 pixels.
{{Columns}}
{{Column}}<source lang="html5">
<div class="grid-container">
  <div>Grid item 1</div>
  <div>Grid item 2</div>
  <div>Grid item 3</div>
  <div>Grid item 4</div>
  <div>Grid item 5</div>
  <div>Grid item 6</div>
</div>


<style>
.grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  grid-template-rows: 300px 300px;
  grid-column-gap: 10px;
}
</style>
</source>
</source>
{{ColumnEnd}}
{{ColumnEnd}}
{{Column}}
{{Column}}[[File:CSS-grid-display-grid-with-rows.png]]{{ColumnEnd}}
[[File:CSS-grid-display-grid-with-rows.png]]
{{ColumnEnd}}
{{ColumnsEnd}}
{{ColumnsEnd}}



Revision as of 08:53, 5 September 2022

In CSS land there are multiple ways of positioning elements. The most common methods are absolute positioning position: absolute;, Flexbox display: flex; and CSS grid display: grid;.

This article will describe how to create a layout with CSS Grid.

CSS-grid-display-grid-with-rows.png

CSS grid places its direct children onto a "grid". In the following HTML snippet the parent element has the class 'grid-container' and 6 children.

<div class="grid-container">
  <div>Grid item 1</div>
  <div>Grid item 2</div>
  <div>Grid item 3</div>
  <div>Grid item 4</div>
  <div>Grid item 5</div>
  <div>Grid item 6</div>
</div>

We first have to set the parent element to be displayed as a grid via the CSS property display.

<style>
.grid-container {
  display: grid;
}
</style>

We also have to create columns and or rows. We can do this with the properties grid-template-columns and grid-template-rows.

<style>
.grid-container {
  display: grid;
  grid-template-columns: 200px 200px 200px;
  grid-template-rows: 300px 300px;
}
</style>

You can set the amount of columns by adding or removing values:

  • grid-template-columns: 200px 200px; will create two columns of each 200 pixels width.
  • grid-template-columns: 200px 200px 200px; will create 3 columns of each 200 pixels width
  • grid-template-columns: 300px 500px 500px; will create 3 columns, the first and last 200 pixels wide. The middle column 500 pixels wide.

The following snippet combines columns and rows to create a grid.

<div class="grid-container">
  <div>Grid item 1</div>
  <div>Grid item 2</div>
  <div>Grid item 3</div>
  <div>Grid item 4</div>
  <div>Grid item 5</div>
  <div>Grid item 6</div>
</div>

<style>
.grid-container {
  display: grid;
  grid-template-columns: 200px 200px 200px;
  grid-template-rows: 300px 300px;
}
</style>
CSS-grid-display-grid-with-rows.png

Instead of pixels you can also use fr which stands for fraction (for example 1/2 or 1/4). This is super handy to create fluid grids.

In the following snippet we set grid-template-columns: 1fr 1fr 1fr 1fr;. Which means each column will be around 25% (1/4).

We also add grid-column-gap: 10px; which sets the gutter to 10 pixels.


<div class="grid-container">
  <div>Grid item 1</div>
  <div>Grid item 2</div>
  <div>Grid item 3</div>
  <div>Grid item 4</div>
  <div>Grid item 5</div>
  <div>Grid item 6</div>
</div>

<style>
.grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  grid-template-rows: 300px 300px;
  grid-column-gap: 10px;
}
</style>
CSS-grid-display-grid-with-rows.png

Resources