The grid-template-rows property
The grid-template-rows
property
specifies a number and width of rows
that an element will occupy in a grid. The
property is specified on the parent element
and determines the width of the rows of
the child elements. In the property value,
specify the width of the rows in any
size units.
When specifying values in pixels in properties,
the sizes of the rows will correspond exactly
to them. If we specify the word auto
,
then the rows will fill all the available
space. Using the fr
(fraction) unit
means that the entire space will be divided
into equal parts. The advantage of fr
is its adaptability to different containers
or screen resolutions, since fr
simply
divides them into the specified number of
fractions without reference to the exact
pixel size.
Syntax
selector {
grid-template-rows: row width;
}
Example
Let's set a row width for our elements in the grid:
<div id="parent">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>
#parent {
display: grid;
grid-template-rows: 50px 100px 50px 50px;
border: 2px solid #696989;
padding: 10px;
width: 400px;
}
#parent > div {
padding: 10px;
border: 1px solid #696989;
}
:
Example
Let's give the first and third rows a fixed width in pixels, and let the second row automatically fill the available space:
<div id="parent">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
#parent {
display: grid;
grid-template-rows: 100px auto 60px;
border: 2px solid #696989;
padding: 10px;
width: 400px;
height: 200px;
}
#parent > div {
padding: 10px;
border: 1px solid #696989;
}
:
Example
Now, using the grid-template-rows
property, we’ll make sure that the first
and second rows take up one part of
the container, and the third row
takes up three parts:
<div id="parent">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
#parent {
display: grid;
grid-template-rows: 1fr 1fr 3fr;
border: 2px solid #696989;
padding: 10px;
width: 400px;
height: 200px;
}
#parent > div {
padding: 10px;
border: 1px solid #696989;
}
:
Example
Values indicated in fr
units can take
a fractional form. Let's modify the previous
example by specifying fractional widths
for the second and third rows:
<div id="parent">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
#parent {
display: grid;
grid-template-rows: 1fr 0.5fr 2.5fr;
border: 2px solid #696989;
padding: 10px;
width: 400px;
height: 200px;
}
#parent > div {
padding: 10px;
border: 1px solid #696989;
}
:
Example
Let's set the grid-template-rows
property to the repeat()
function,
which will tell the container that all
three rows should have the same width:
<div id="parent">
<div>1</div>
<div>2</div>
<div>3</div>
</div>
#parent {
display: grid;
grid-template-rows: repeat(3, 1fr);
border: 2px solid #696989;
padding: 10px;
width: 400px;
height: 200px;
}
#parent > div {
padding: 10px;
border: 1px solid #696989;
}
:
Example
Now let's change the previous example so that a fourth row is added to three identical rows, which will occupy two fractions of the container:
<div id="parent">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>
#parent {
display: grid;
grid-template-rows: repeat(3, 1fr) 2fr;
border: 2px solid #696989;
padding: 10px;
width: 400px;
height: 200px;
}
#parent > div {
padding: 10px;
border: 1px solid #696989;
}
:
Example
Let's set the first two rows to be one container fraction wide, and the last two rows to be two container fractions wide:
<div id="parent">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>
#parent {
display: grid;
grid-template-rows: repeat(2, 1fr) repeat(2, 2fr);
border: 2px solid #696989;
padding: 10px;
width: 400px;
height: 200px;
}
#parent > div {
padding: 10px;
border: 1px solid #696989;
}
:
Example
Now let's set the width of the rows
by combining the values specified using
the repeat()
function and fr
units:
<div id="parent">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
</div>
#parent {
display: grid;
grid-template-rows: repeat(2, 1fr) 3fr repeat(2, 2fr);
border: 2px solid #696989;
padding: 10px;
width: 400px;
height: 300px;
}
#parent > div {
padding: 10px;
border: 1px solid #696989;
}
:
Example
Also in the repeat()
function you
can specify the auto-fill
value, which
will fill our container with identical rows
with the width we need:
<div id="parent">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>
#parent {
display: grid;
grid-template-rows: repeat(auto-fill, 50px);
border: 2px solid #696989;
padding: 10px;
width: 400px;
height: 200px;
}
#parent > div {
padding: 10px;
border: 1px solid #696989;
}
:
Example
It is very convenient, together with auto-fill
,
to specify the minmax
function, which specifies
the range of row widths from the minimum to the
maximum value. If the width of the container does
not accommodate all the rows, then some of them
will be moved to a new column, while the rows in
the column will be evenly distributed in it. Let's
modify the previous example to include the
minmax
function:
<div id="parent">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>
#parent {
display: grid;
grid-template-rows: repeat(auto-fill, minmax(50px, 1fr));
border: 2px solid #696989;
padding: 10px;
}
#parent > div {
padding: 10px;
border: 1px solid #696989;
}
:
Example
Now let's specify the auto-fit
property,
which differs from auto-fill
in that
it adjusts the number of rows to the
available width of the container,
expanding or contracting them:
<div id="parent">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>
#parent {
display: grid;
grid-template-rows: repeat(auto-fit, minmax(100px, 1fr));
border: 2px solid #696989;
padding: 10px;
}
#parent > div {
padding: 10px;
border: 1px solid #696989;
}
:
Example
Also, along with fr
, you can use
values in %
, which also determine which part
of the container the row will occupy. In
this case, the row size will first be
calculated in %
, and the remaining
free space will be divided into fractions:
<div id="parent">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</div>
#parent {
display: grid;
grid-template-rows: 50% 1fr 2fr 30%;
border: 2px solid #696989;
padding: 10px;
width: 400px;
height: 400px;
}
#parent > div {
padding: 10px;
border: 1px solid #696989;
}
:
Example
Let's use the
grid-template-columns
and grid-template-rows
properties together:
<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-rows: 1fr 1fr 1fr;
grid-template-columns: 1fr 1fr 1fr;
border: 2px solid #696989;
padding: 10px;
width: 400px;
height: 400px;
}
#parent > div {
padding: 10px;
border: 1px solid #696989;
}
:
Example
Let's use the
grid-template-columns
and grid-template-rows
properties. Let's
create a table of nine cells arranged in three
rows. Moreover, the second and third row will
have the same width, and each column will have
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-rows: 60px 1fr 60px;
grid-template-columns: 20% 1fr 15%;
border: 2px solid #696989;
padding: 10px;
width: 400px;
height: 400px;
}
#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-rows: 2fr 1fr 1fr;
grid-template-columns: 0.5fr 1fr 1fr;
border: 2px solid #696989;
padding: 10px;
width: 400px;
height: 400px;
}
#parent > div {
padding: 10px;
border: 1px solid #696989;
}
:
See also
-
the
grid-template-columns
property
that specifies a number and width of columns in a grid -
the
grid-auto-rows
property
that specifies a number and width of rows in an implicitly-created grid