Grid - Implicit Grid Flashcards

1
Q

What is an implicit grid?

A

If there are more grid items than cells in the grid or when a grid item is placed outside of the explicit grid, the grid container automatically generates grid tracks by adding grid lines to the grid. The explicit grid together with these additional implicit tracks and lines forms the so called implicit grid.

Source css-tricks

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What do grid-auto-columns and grid-auto-rows properties do?

A

Specifies the size of any auto-generated grid tracks (aka implicit grid tracks). Implicit tracks get created when there are more grid items than cells in the grid or when a grid item is placed outside of the explicit grid.

Values:

<track-size> – can be a length, a percentage, or a fraction of the free space in the grid (using the fr unit)

.container {
  grid-auto-columns: <track-size> ...;
  grid-auto-rows: <track-size> ...;
}

Source css-tricks

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does grid-auto-flow property do?

A

If you have grid items that you don’t explicitly place on the grid, the auto-placement algorithm kicks in to automatically place the items. This property controls how the auto-placement algorithm works.

Values:

  • row– default - tells the auto-placement algorithm to fill in each row in turn, adding new rows as necessary
  • column – tells the auto-placement algorithm to fill in each column in turn, adding new columns as necessary
  • dense – tells the auto-placement algorithm to attempt to fill in holes earlier in the grid if smaller items come up later
.container {
  grid-auto-flow: row | column | row dense | column dense;
}

Note: that dense only changes the visual order of your items and might cause them to appear out of order, which is bad for accessibility.

Source css-tricks

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does grid property do?

A

A shorthand for setting all of the following properties in a single declaration: grid-template-rows, grid-template-columns, grid-template-areas, grid-auto-rows, grid-auto-columns, and grid-auto-flow.

Note: You can only specify the explicit or the implicit grid properties in a single grid declaration.

Source css-tricks

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What do the sizing functions do?

A

The minmax() function sets a minimum and maximum value for what the length is able to be. This is useful for in combination with relative units. Like you may want a column to be only able to shrink so far.

Example:

grid-template-columns: minmax(100px, 1fr) 3fr;

The min() function lets you set the smallest (most negative) value from a list of comma-separated expressions as the value of a CSS property value.

The max() function lets you set the largest (most positive) value from a list of comma-separated expressions as the value of a CSS property value.

Source css-tricks

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does the repeat() function do?

A

The repeat() function can save some typing:

grid-template-columns: 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr;

/* easier: */
grid-template-columns: repeat(8, 1fr);

/* especially when: */
grid-template-columns: repeat(8, minmax(10px, 1fr));

But repeat() can get extra fancy when combined with keywords:

  • auto-fill: Fit as many possible columns as possible on a row, even if they are empty.
  • auto-fit: Fit whatever columns there are into the space. Prefer expanding columns to fill space rather than empty columns.

Source css-tricks

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What does grid-template-columns: repeat(auto-fill, 100px); do?

A

It will create as many columns of 100px as it can possible fit in the grid without overflowing the width of the grid.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does this css rule do grid-template-columns: repeat(auto-fit, 100px);?

A

It will create as many columns (tracks) of width 100px as needed depending amount of grid items.

The difference between auto-fit and auto-fill is that with auto-fit the empty tracks will colapse while with auto-fill they will be created.

Note: if a fixed number of vertical tracks is used in the repeat notation and the number of items exceeds this value more rows are added.

Source css-tricks

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How can we size implicit tracks?

A

The grid-auto-rows and grid-auto-columns properties give us control over the size of implicit tracks.

 .grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: 100px 100px;
  grid-gap: 20px;
  grid-auto-columns: 200px;
  grid-auto-rows: 60px;
}

Implicit tracks will now always have a width of 200px and a height of 60px, no matter if the grid item fits or not.

You can make sized implicit tracks more flexible by specifying a range using the minmax() notation.

.grid {
  grid-auto-columns: minmax(200px, auto);
  grid-auto-rows: minmax(60px, auto);
}

Implicit tracks are now at least 200px wide and 60px high, but will expand if the content demands it.

Source css-tricks

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the automatic placement of grid-items?

A

Implicit tracks are also added if the number of items exceeds the number of cells. By default, the auto-placement algorithm places items by filling each row consecutively, adding new rows as necessary.

Source css-tricks

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How can you change how auto-placed items get flowed into the grid?

A

We can specify how auto-placed items get flowed into the grid by using the grid-auto-flow property.

.grid-container {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: 100px 100px;
  grid-gap: 20px;
  grid-auto-flow: column;
}

Instead of rows, columns are being filled with items and additional implicit columns are created.

Source css-tricks

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How can you create a flexible grid that expands with new columns and wraps to new rows if there is no space?

A

You could do:

.grid {
  display: grid;
  gap: 1rem;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}

Note that the minimum value used here is 200px for each column. That’s just some number that you’d pick that feels good for your content. If you need to consider an alteration that allows it to go smaller if the screen itself is smaller than that wide. You could use:

grid-template-columns: repeat(auto-fill, minmax(min(10rem, 100%), 1fr));

That’s saying that if 100% width calculates to less than 10rem (otherwise the minimum), then use that instead, making it safer for small-screen layouts.

Source css-tricks

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does this CSS declaration grid-template-columns: repeat(12, 1fr); do?

A

It creates a 12 columns grid layout.

The 1fr is what tells the browser to distribute the space between the columns so that each column equally gets one fraction of that space.

Source css-tricks

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the problem with this CSS declaration grid-template-columns: repeat( 12, minmax(250px, 1fr) );?

A

This will cause overflow in the row. The columns will not wrap into new rows if the viewport width is too narrow to fit them all with the new minimum width requirement, because we’re explicitly telling the browser to repeat the columns 12 times per row.

To achieve wrapping, we can use the auto-fit or auto-fill keywords.

grid-template-columns: repeat( auto-fit, minmax(250px, 1fr));

Source css-tricks

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What do auto-fit or auto-fill keywords tell the browser when passed to a repeat function?

For example:

grid-template-columns: repeat( auto-fit, minmax(250px, 1fr));

A

These keywords tell the browser to handle the column sizing and element wrapping for us so that the elements will wrap into rows when the width is not large enough to fit them in without any overflow.

In the example, the fraction unit used, ensures that in case the width allows for a fraction of a column to fit but not a full column, that space will instead be distributed over the column or columns that already fit, making sure we aren’t left with any empty space at the end of the row.

Source css-tricks

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How can you change the default auto size of implicit grid tracks?

A

You can size implicit tracks with the grid-auto-columns and grid-auto-rows properties.

Example:

grid-auto-columns: 100px;
17
Q

What values does CSS grid property have?

A

Syntax

/* <'grid-template'> values */
grid: none;
grid: "a" 100px "b" 1fr;
grid: [linename1] "a" 100px [linename2];
grid: "a" 200px "b" min-content;
grid: "a" minmax(100px, max-content) "b" 20%;
grid: 100px / 200px;
grid: minmax(400px, min-content) / repeat(auto-fill, 50px);

/* <'grid-template-rows'> /
   [ auto-flow && dense? ] <'grid-auto-columns'>? values */
grid: 200px / auto-flow;
grid: 30% / auto-flow dense;
grid: repeat(3, [line1 line2 line3] 200px) / auto-flow 300px;
grid: [line1] minmax(20em, max-content) / auto-flow dense 40%;

/* [ auto-flow && dense? ] <'grid-auto-rows'>? /
   <'grid-template-columns'> values */
grid: auto-flow / 200px;
grid: auto-flow dense / 30%;
grid: auto-flow 300px / repeat(3, [line1 line2 line3] 200px);
grid: auto-flow dense 40% / [line1] minmax(20em, max-content);

/* Global values */
grid: inherit;
grid: initial;
grid: revert;
grid: revert-layer;
grid: unset;

Values

  • <'grid-template'> - Defines the grid-template including grid-template-columns, grid-template-rows and grid-template-areas.
  • <'grid-template-rows'> / [ auto-flow && dense? ] <'grid-auto-columns'>? - Sets up an auto-flow by setting the row tracks explicitly via the grid-template-rows property (and the grid-template-columns property to none) and specifying how to auto-repeat the column tracks via grid-auto-columns (and setting grid-auto-rows to auto). grid-auto-flow is also set to column accordingly, with dense if it’s specified.

All other grid sub-properties are reset to their initial values.

  • [ auto-flow && dense? ] <'grid-auto-rows'>? / <'grid-template-columns'> - Sets up an auto-flow by setting the column tracks explicitly via the grid-template-columns property (and the grid-template-rows property to none) and specifying how to auto-repeat the row tracks via grid-auto-rows (and setting grid-auto-columns to auto). grid-auto-flow is also set to row accordingly, with dense if it’s specified.

All other grid sub-properties are reset to their initial values.

Initial values

  • grid-template-rows: none
  • grid-template-columns: none
  • grid-template-areas: none
  • grid-auto-rows: auto
  • grid-auto-columns: auto
  • grid-auto-flow: row
  • grid-column-gap: 0
  • grid-row-gap: 0
  • column-gap: normal
  • row-gap: normal

Note: You can only specify the explicit or the implicit grid properties in a single grid declaration

Source css-tricks

18
Q

Arguments of repeat() CSS function

A

The repeat() function takes two arguments:

  • repeat count: the first argument specifies the number of times that the track list should be repeated. It is specified with an integer value of 1 or more, or with the keyword values auto-fill or auto-fit. These keyword values repeat the set of tracks as many times as is needed to fill the grid container.
  • tracks: the second argument specifies the set of tracks that will be repeated. Fundamentally this consists of one or more values, where each value represents the size of that track. Each size is specified using either a <track-size> value or a <fixed-size>_ value. You can also specify one or more line names before or after each track, by providing <line-names> values before and/or after the track size.

Source MDN

19
Q

What does the repeat() CSS function does?

A

The repeat() CSS function represents a repeated fragment of the track list, allowing a large number of columns or rows that exhibit a recurring pattern to be written in a more compact form.

Source MDN

20
Q

Possible tracks values for repeat(4, track_value) CSS function call

A

This type of repeat calls are called < track-repeat >, which uses an integer to set the repeat count and one of the following values as track value.

  • A <length-percentage> value.
  • One of the following size keywords: min-content, max-content, or auto.
  • A <flex> value (fr unit value).
  • A minmax() function with:
    • min given as a <length-percentage> value, or one of the following keywords: min-content, max-content, or auto.
    • max given as one of a <length-percentage> value, a <flex> value (fr unit), or one of the following keywords: min-content, max-content, or auto
  • a fit-content() function, passed a <length-percentage> value.

Source MDN

21
Q

Possible tracks values for repeat(auto-fill | auto-fit, track_value) CSS function call

A

This type of repeat calls are called<auto-repeat>, which uses
auto-fill or auto-fit to set the repeat count and one of the following values as track value.

  • A <length-percentage> value.
  • a minmax() function with:
    • min given as a <length-percentage> value
    • max given as one of a <length-percentage> value, a <flex> value (fr unit), or one of the following keywords: min-content, max-content, or auto
  • a minmax() function with:
    • min given as a <length-percentage> value or one of the following keywords: min-content, max-content, or auto
    • max given as a <length-percentage> value.

Source MDN