CSS Grid Properties Shorthand
The grid property is a shortened form of writing all the properties of the columns and rows of the grid container. All values are written in one line separated by a slash.
With grid, you can only describe one type of properties in one line - explicit (grid-template-rows, grid-template-columns, grid-template-areas) or implicit (grid-auto-rows, grid-auto-columns, grid-auto-flow). Those properties that are left unspecified take default values.
Example
Let's make a table using the grid 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: 1fr 1fr 1fr / 1fr 1fr 1fr;
border: 2px solid #696989;
padding: 10px;
width: 600px;
height: 200px;
}
#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: 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: 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 example:
Implement the following example:
Implement the following example: