Shorthand for Rows and Columns in CSS Grids
There are situations when it is convenient to specify the sizes of rows and columns in a short form. For this we use the grid-template
property, which is specified on the parent element and is a shortened form of two properties grid-template-rows
and grid-template-columns
.
Rows and columns for a grid container are specified using a slash, and their sizes are defined in units of measurement.
Example
Let's make a table using the grid-template
property:
<div id="parent">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
</div>
#parent {
display: grid;
grid-template: 1fr 1fr 1fr / 1fr 1fr 1fr;
border: 2px solid #696989;
padding: 10px;
width: 600px;
height: 400px;
}
#parent > div {
padding: 10px;
border: 1px solid #696989;
}
:
Example
Now let's give the second and third rows the same width, and each column a different width:
<div id="parent">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
</div>
#parent {
display: grid;
grid-template: 60px 1fr 60px / 20% 1fr 15%;
border: 2px solid #696989;
padding: 10px;
width: 600px;
height: 300px;
}
#parent > div {
padding: 10px;
border: 1px solid #696989;
}
:
Example
Now in the table from the previous example, let's make the top row two fractions wide, and the first column half a fraction wide:
<div id="parent">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
</div>
#parent {
display: grid;
grid-template: 2fr 1fr 1fr / 0.5fr 1fr 1fr;
border: 2px solid #696989;
padding: 10px;
width: 600px;
height: 400px;
}
#parent > div {
padding: 10px;
border: 1px solid #696989;
}
:
Practical tasks
Implement the following tile:
Implement the following tile:
Implement the following tile:
Implement the following tile:
Implement the following tile: