{"id":366810,"date":"2022-07-15T06:16:52","date_gmt":"2022-07-15T13:16:52","guid":{"rendered":"https:\/\/css-tricks.com\/?page_id=366810"},"modified":"2022-08-26T08:25:55","modified_gmt":"2022-08-26T15:25:55","slug":"grid-auto-rows","status":"publish","type":"page","link":"https:\/\/css-tricks.com\/almanac\/properties\/g\/grid-auto-rows\/","title":{"rendered":"grid-auto-rows"},"content":{"rendered":"\n
The That example creates a grid container with two rows defined by As said above, Here are some situations where that might come up.<\/p>\n\n\n The first case is the one that we saw at the top of this article. A grid can have an explicit row defined by The first row is set to If we add the Now as you can see in the following image, the last row\u2019s size is set to Note that the tracks that don\u2019t have an explicit size are A grid that is defined by the We have implicit grid tracks for those columns and rows because there\u2019s no sizing information. Only the number of columns and the number of rows.<\/p>\n\n\n\n One situation where you\u2019ll find implicit rows is when there are more grid items than there are defined rows. Imagine in our last example that we now have more than 6 HTML elements. We previously set up three columns and two rows, so there are exactly 6 cells in the grid \u2014 the perfect amount for our 6 elements. But with more than 6 elements, we suddenly have an overflow problem, right?<\/p>\n\n\n\n Not at all! CSS Grid\u2019s auto-placement algorithm<\/a> creates additional rows or columns to hold the extra items. Those extra columns and rows are called implicit tracks<\/strong>.<\/p>\n\n\n\n By default, the auto-placement algorithm creates row tracks to hold extra items, so our grid is capable of holding everything, no matter how many items we toss at it.<\/p>\n\n\n\n However, the implicit rows are sized automatically based on their content. That\u2019s why Say we size those implicit grid rows to Now, no matter how many implicit rows get created, they will all have their size set to Another scenario where the So what we did there was set the last We can specify multiple row sizes for grid tracks using the Here, we move the last child of our grid out of the explicit rows and our grid creates implicit rows to hold that item as a result. This grid has two explicit rows ( The If we had one more implicit rows, what would be the size of that row?<\/p>\n\n\n\n Correct, it would be 40 pixels tall!<\/p>\n\n\n<\/details>\n\n The This value can be any valid and non-negative CSS length, such as This is a non-negative value relative to the inner inline size of the grid container. If the size of the grid container depends on the size of its tracks, then the percentage must be treated the same as declaring One disadvantage of using a percentage value is that, if you add gaps between the tracks<\/a>, the size of the gaps will be added to the size of the tracks which may cause an overflow<\/a>. Use the This is a non-negative value expressed in For example, consider a grid container that\u2019s The second two rows are fractional, so they\u2019re going to take any remaining available space. And, in this case, the available space is whatever is leftover after the first row\u2019s fixed In this example, the remaining and available space is Aha! One fraction is The Take the following example:<\/p>\n\n\n\n There are two rows here: one that\u2019s In clearer terms: As long as there\u2019s more than The So, this is invalid since the But these are totally valid:<\/p>\n\n\n\n The Since Take a look at the result of these two track examples:<\/p>\n\n\n\n When all three rows are set to The Let\u2019s say the content is \u201cCSS is awesome.\u201d written in vertical mode, the In code, that looks like this:<\/p>\n\n\n\n \u2026which results in something like this:<\/p>\n\n\n\n The If we revisit our grid-auto-rows<\/code> CSS property is part of the CSS Grid Layout specification<\/a>, specifying the size of the grid
rows<\/code> that were created without having an explicit size. In other words, this property sets the size of implicit rows<\/a> and any other rows that have not been explicitly sized in the
grid-template-rows<\/code><\/a> property.<\/p>\n\n\n\n\n\n\n\n
.grid-container {\n display: grid;\n grid-template-areas: \"media detail detail\"\n \"media detail detail\";\n grid-template-rows: 200px;\n grid-auto-rows: 150px;\n}<\/code><\/pre>\n\n\n\n
grid-template-areas<\/code>. The first row has an explicit size set by
grid-template-rows<\/code>, but not the second row. This is where
grid-auto-rows<\/code> kicks in and sets that row\u2019s size to
150px<\/code>.<\/p>\n\n\n
grid-auto-rows<\/code> use cases<\/h3>\n\n\n
grid-auto-rows<\/code> sets the size of any rows that don\u2019t have an explicit size. Now the question is, what kind of rows don\u2019t have an explicit size? Or, how does a grid create rows without an explicit size?<\/p>\n\n\n\n
Rows that are defined by
grid-template-areas<\/code> that have no explicit size set by
grid-template-rows<\/code><\/h4>\n\n\n
grid-template-areas<\/code> without having its size set explicitly by the
grid-template-rows<\/code> property.<\/p>\n\n\n\n
.grid-container {\n display: grid;\n grid-template-areas: \"media detail detail\"\n \"media detail detail\"; \/* 2 rows *\/\n grid-template-rows: 200px; \/* 1 size *\/\n}<\/code><\/pre>\n\n\n\n
200px<\/code>, and since the last row is not set to any explicit size, it will be set to
auto<\/code>. In this example the second row does not have any content therefore
auto<\/code> is equal to zero size.<\/p>\n\n\n\n
grid-auto-rows<\/code> to the above example we can control the size of the last row too:<\/p>\n\n\n\n
.grid-container {\n display: grid;\n grid-template-areas: \"media detail detail\"\n \"media detail detail\";\n grid-template-rows: 200px;\n grid-auto-rows: 150px;\n}\n\n\/* Make sure grid items expand to the second row *\/\n.grid-items {\n grid-row: 1\/3;\n}<\/code><\/pre>\n\n\n\n
150px<\/code>:<\/p>\n\n\n\n
<\/figure>\n\n\n\n
auto<\/code>-sized.<\/p>\n\n\n
Implicit rows are created when there are more grid items than cells<\/h4>\n\n\n
grid-template-columns<\/code>,
grid-template-rows<\/code> or\/and
grid-template-areas<\/code> is called an explicit grid<\/strong>, meaning that all the grid tracks are a set size. Let\u2019s say, for example, we have six HTML child elements in a parent grid container, and we explicitly define three columns and two rows in the grid.<\/p>\n\n\n\n
.grid {\n display: grid;\n grid-template-columns: repeat(3, 1fr);\n grid-template-rows: repeat(2, 100px);\n}<\/code><\/pre>\n\n\n\n
grid-auto-rows<\/code> is so useful \u2014we can give all of those rows a size and CSS Grid will stop filling rows when it reaches the last element.<\/p>\n\n\n\n
150px<\/code>:<\/p>\n\n\n\n
.grid-container {\n display: grid;\n grid-template-columns: repeat(3, 1fr);\n grid-template-rows: repeat(2, 100px);\n grid-auto-rows: 150px;\n}<\/code><\/pre>\n\n\n\n
150px<\/code>.<\/p>\n\n\n\n
Implicit rows are created when we position an item outside of the explicit grid<\/h4>\n\n\n
grid-auto-rows<\/code> property comes in handy is when implicit tracks are created when we position an item outside of the explicit grid. The implicit tracks are automatically created to make a place for that item.<\/p>\n\n\n\n
.grid-container {\n display: grid;\n grid-template-columns: 150px 150px 150px; \/* 3 explicit columns *\/\n grid-template-rows: 150px; \/* 1 explicit row *\/\n grid-auto-rows: 150px; \/* Size of the implicit rows *\/\n}\n\n.grid-item:last-child {\n grid-column: 5;\n grid-row: 3; \/* Placed in 3rd row in a 1-row grid *\/\n}<\/code><\/pre>\n\n\n\n
.grid-item<\/code> child in the third row\u2026 but there is no third row in our one-row grid! That puts the last grid item outside the explicit grid, which creates two implicit row tracks. Even though those rows were created for us, we can set them to
150px<\/code> using the
grid-auto-rows<\/code> property.<\/p>\n\n\n\n
150px<\/code> using the
grid-auto-rows<\/code> property.<\/figcaption><\/figure>\n\n\n
Syntax<\/h3>\n\n\n
grid-auto-rows: <track-size>+<\/code><\/pre>\n\n\n\n
\n Full definition <\/summary>\n \n\n
where\n<track-size> = <track-breadth> | minmax( <inflexible-breadth> , <track-breadth> ) | fit-content( [ <length> | <percentage> ] )\n\nwhere\n<track-breadth> = <length-percentage> | <flex> | min-content | max-content | auto <inflexible-breadth> = <length> | <percentage> | min-content | max-content | auto\n\nwhere\n<length-percentage> = <length> | <percentage><\/code><\/pre>\n\n\n<\/details>\n\n\n
auto<\/code><\/li>
Multiple track sizes<\/h4>\n\n\n
grid-auto-rows<\/code> property. In this case, the multiple track size works as a pattern and it gets repeated if necessary.<\/p>\n\n\n\n
.grid-container {\n display: grid;\n grid-template-rows: 50px 50px;\n grid-auto-rows: 40px 60px 80px;\n}\n\n.grid-item:last-child {\n grid-row: 8;\n}<\/code><\/pre>\n\n\n\n
50px 50px<\/code>) and since the last item wants to be in the eighth row (
grid-row: 8<\/code>), six implicit rows are automatically generated in the grid.<\/p>\n\n\n\n
grid-auto-rows<\/code> sets the first implicit row size to
40px<\/code>, the second to
60px<\/code> and third row to
80px<\/code>. Since we have more than three implicit rows, our grid repeats this pattern to size them.<\/p>\n\n\n\n
grid-auto-rows<\/code> property. The multiple values get repeated as the size of the implicit rows.<\/figcaption><\/figure>\n\n\n\n
\n Show me the answer! <\/summary>\n \n\n
Values<\/h3>\n\n\n
grid-auto-rows<\/code> values are almost the same as the values of the grid-template-rows<\/a> property, except in
grid-auto-rows<\/code> you can\u2019t use the following values:<\/p>\n\n\n\n
none<\/code><\/li>
[linename]<\/code><\/li>
repeat()<\/code><\/li>
subgrid<\/code><\/li>
masonry<\/code><\/li><\/ul>\n\n\n\n
\/* Keyword values *\/\ngrid-auto-rows: auto;\ngrid-auto-rows: min-content;\ngrid-auto-rows: max-content;\n\n\/* single track-size values *\/\ngrid-auto-rows: 200px;\ngrid-auto-rows: 30vmin;\ngrid-auto-rows: 50%;\ngrid-auto-rows: 1.5fr;\ngrid-auto-rows: minmax(100px, 2fr);\ngrid-auto-rows: minmax(min-content, 1fr);\ngrid-auto-rows: fit-content(15rem);\n\n\/* multiple track-size values *\/\ngrid-auto-rows: 100px 1fr;\ngrid-auto-rows: 200px min-content 1fr;\ngrid-auto-rows: 100px 200px 50px 10%;\ngrid-auto-rows: minmax(100px, auto) auto max-content fit-content(200px);\n\n\/* Global values *\/\ngrid-auto-rows: inherit;\ngrid-auto-rows: initial; \/* same as `auto` *\/\ngrid-auto-rows: revert;\ngrid-auto-rows: revert-layer;\ngrid-auto-rows: unset;<\/code><\/pre>\n\n\n
<length><\/code><\/h4>\n\n\n
px<\/code>,
em<\/code> and
rem<\/code>, among others to specify the track size (i.e. height) of the row.<\/p>\n\n\n
<percentage><\/code><\/h4>\n\n\n
auto<\/code>.<\/p>\n\n\n\n
fr<\/code> unit<\/a> or
auto<\/code> keyword to size the tracks and prevent that from happening.<\/p>\n\n\n
<flex><\/code><\/h4>\n\n\n
fr<\/code> units and it allows you to create flexible grid tracks by specifying the size of a track as a fraction of the available space<\/strong> in the grid container. That way, as the grid changes heights, so do the rows, making for a more responsive grid.<\/p>\n\n\n\n
900px<\/code> tall and it has three implicit rows. Let\u2019s say the height of the first (top) row is fixed (i.e. it doesn\u2019t change) at
300px<\/code> while the second row takes up one fractional unit (
1fr<\/code>) and the third row weighs in at two fractional units (
2fr<\/code>):<\/p>\n\n\n\n
.grid {\n display: grid;\n height: 900px;\n grid-auto-rows: 300px 1fr 2fr;\n}<\/code><\/pre>\n\n\n\n
300px<\/code> height is taken into account. After that, the second two rows divvy up what\u2019s left according to how many fractions they get.<\/p>\n\n\n\n
600px<\/code> (i.e.
900px<\/code> minus
300px<\/code>). The second rows gets one fraction (
1fr<\/code>) of that space and the third row gets two fractions (
2fr<\/code>). Let\u2019s get math-y for a moment to figure out the height of one fraction of
600px<\/code>:<\/p>\n\n\n\n
1fr = ([Grid Container Height] - [Fixed Row Height]) \/ [Sum of the flex factors]\n1fr = (900px - 300px) \/ 3\n1fr = 600px \/ 3\n1fr = 200px<\/code><\/pre>\n\n\n\n
200px<\/code>. And since the second row is
1fr<\/code>, it must be
200px<\/code> as well. That leaves us with
400px<\/code> of available space (
900px<\/code> –
300px<\/code> –
200px<\/code>) which just so happens to be twice<\/em> the size of the second row.<\/p>\n\n\n
minmax(min, max)<\/code><\/h4>\n\n\n
minmax()<\/code> function accepts two arguments: a minimum length value and a maximum length value. And what that tells a grid container is that the
grid-auto-rows<\/code> can be no taller<\/em> than the minimum value, but no shorter<\/em> than the maximum value. Our Guide to CSS Functions has a more thorough, detailed explanation<\/a> of how the function works.<\/p>\n\n\n\n
grid-auto-rows: 200px minmax(100px, 400px);<\/code><\/pre>\n\n\n\n
200px<\/code> tall, and another that\u2019s expressed as
minmax(100px, 400px)<\/code>. Looks weird, right? But all that\u2019s saying is that the second row must<\/em> take up at least
100px<\/code> of space, but no more than
400px<\/code>.<\/p>\n\n\n\n
100px<\/code> of available space, then the second row can flex up to
400px<\/code> but has to stop there. Otherwise, the row is
100px<\/code> tall.<\/p>\n\n\n\n
min<\/code> and
max<\/code> arguments accept all the following values, except that the
min<\/code> value can\u2019t be a flex value (e.g.
1fr<\/code>):<\/p>\n\n\n\n
<length-percentage> | <flex> | min-content | max-content | auto<\/code><\/pre>\n\n\n\n
min<\/code> value is a flex value:<\/p>\n\n\n\n
\/* 👎 *\/\ngrid-auto-rows: minmax(1fr, 500px) 3fr;<\/code><\/pre>\n\n\n\n
\/* 👍 *\/\ngrid-auto-rows: minmax(auto, 1fr) 3fr;\ngrid-auto-rows: minmax(200%, 1fr) 1fr;<\/code><\/pre>\n\n\n
auto<\/code><\/h4>\n\n\n
auto<\/code> keyword behaves similarly to
minmax(min-content,<\/code>
max-content)<\/code> in most cases.<\/p>\n\n\n\n
auto<\/code> track sizes can be stretched by the
align-content<\/code><\/a> and
justify-content<\/code><\/a> properties, they will take up any remaining space in the grid container by default
auto<\/code>-matically. That said,
auto<\/code> track sizes can\u2019t win a fight against
fr<\/code> units when allocating the remaining space \u2014 they adjust to the maximum size of their content.<\/p>\n\n\n\n
grid-auto-rows: auto auto auto;\ngrid-auto-rows: auto 1fr auto;<\/code><\/pre>\n\n\n\n
auto<\/code>, all they do is share equal space. But when we drop a fractional unit in the middle as the second row, the
auto<\/code> rows are only as tall as the content inside them and the
1fr<\/code> row takes up the remaining space.<\/p>\n\n\n\n
<\/figure>\n\n\n
min-content<\/code><\/h4>\n\n\n
min-content<\/code> keyword represents the smallest<\/em> amount of space possible that an element can take in a grid container without overflowing its content. The content can be texts, images or videos.<\/p>\n\n\n\n
min-content<\/code> is the width of the world \u201cawesome\u201d because that is the tallest part of the content (taller than \u201cCSS\u201d and \u201cis\u201d). If the row goes any shorter, then the text starts to overflow the container.<\/p>\n\n\n\n
.grid {\n display: grid;\n grid-auto-rows: min-content 1fr;\n gap: 1rem;\n}\n\n.grid-item {\n writing-mode: vertical-rl;\n}<\/code><\/pre>\n\n\n\n
<\/figure>\n\n\n
max-content<\/code><\/h4>\n\n\n
max-content<\/code> keyword represents the smallest size required<\/em> for an element to fit all of its content without being wrapped or overflowed. A name that includes \u201cmax\u201dmakes it sound like we\u2019re dealing with a maximum size, so this value can be a bit of a brain teaser.<\/p>\n\n\n\n
min-content<\/code> example above, a grid track with a size value of
max-content<\/code> will grow until its content can fit into a single line. So, in the case of \u201cCSS is awesome\u201d the
max-content<\/code> is however much space those three words take up together on one line.<\/p>\n\n\n\n
.grid {\n display: grid;\n grid-auto-rows: max-content 1fr;\n gap: 1rem;\n}<\/code><\/pre>\n\n\n\n
<\/figure>\n\n\n
fit-content(<length-percentage>)<\/code><\/h4>\n\n\n