29
loading...
This website collects cookies to deliver better user experience
<div class="container">
<p class="red"></p>
<p class="green"></p>
<p class="blue"></p>
</div>
container
a grid where we can control the elements inside as explained earlier.display
property of the class container
:.container {
display: grid;
}
container
a grid element. If you test this out, you'll see that you can see nothing. The reason behind that is the p
elements in the .container
grid don't have any elements in them or width or height..container p {
width: 100px;
height: 100px;
}
.red {
background-color: red;
}
.green {
background-color: green;
}
.blue {
background-color: blue;
}
p
elements to 100px
. It will also give a different background color for each element.grid-auto-rows
and grid-auto-columns
on the grid element to specify the default height and width of elements:.container {
...
grid-auto-rows: 100px;
grid-auto-columns: 100px;
}
grid-template-columns
property.n
number of values, where n
is the number of columns you want in the grid and each value is the width of each column.100px
you can use:grid-template-columns: 100px 100px 100px;
100px
.grid-template-columns: 100px 200px 100px;
auto
:grid-template-columns: auto auto auto;
grid-template-columns: repeat(3, auto);
repeat
CSS function takes the first parameter the number of times an expression should be repeated, and the second parameter the statement to repeat..container
to the following:.container {
display: grid;
grid-template-columns: repeat(3, auto);
}
auto
.p
elements from earlier. It should only have height now:.container p {
height: 100px;
}
grid-column-gap
which takes the length of the gap between the columns as a value.5px
between our columns. Add the following property to .container
:grid-column-gap: 5px;
5px
space between the grid columns.grid-column-start
and grid-column-end
properties. These properties allow you to specify at which column an item in a grid should start, and/or at which column an item in a grid should end.grid-column-start
and grid-column-end
can be a number. In this case, it will specify the column to start at and the column to end before..red
element should start in the beginning and end before the 3rd column you use the following properties:.red {
background-color: red;
grid-column-start: 1;
grid-column-end: 3;
}
.red
element will span 2 columns and the .blue
element will be pushed to the row below.grid-column-start
and grid-column-end
in the format span n
, where n
is the number of columns to span. This does not specify where exactly the column should start or end. Instead, it specifies that in its natural place it should take up n
columns..red
instead:.red {
background-color: red;
grid-column-start: span 2;
}
.blue {
background-color: blue;
grid-column-start: 3;
}
grid-template-rows
. Similar to columns, the grid-template-rows
takes n
values, where n
is the number of rows the grid will have and the value is the height of each row.100px
height you can use the following:grid-template-rows: 100px 100px;
repeat
function:grid-template-rows: repeat(2, 100px);
.container
to the following:.container {
display: grid;
grid-template-columns: repeat(2, auto);
grid-template-rows: repeat(2, 100px);
grid-column-gap: 5px;
}
auto
width and 2 rows of 100px
height..red {
background-color: red;
}
.green {
background-color: green;
}
.blue {
background-color: blue;
}
grid-template-columns
and grid-template-rows
.grid-row-gap
.5px
gap between rows. Add the following property to the .container
class:grid-row-gap: 5px;
5px
space between the rows in our grid.grid-row-start
and grid-row-end
.grid-row-start
and grid-row-end
can be a number. The value of grid-row-start
will be the row the element should start on, and the value of grid-row-end
will be the row the element should end before.red
element spans 2 rows we can use the following properties:.red {
background-color: red;
grid-row-start: 1;
grid-row-end: 3;
}
span n
where n
is the number of rows the element should span..red {
background-color: red;
grid-row-start: span 2;
}
.blue {
background-color: blue;
grid-row-start: 1;
}
grid-column-start
property:.blue {
background-color: blue;
grid-row-start: 1;
grid-column-start: 2;
}
grid-area
property:.red {
background-color: red;
grid-area: Content;
}
.green {
background-color: green;
grid-area: Header;
}
.blue {
background-color: blue;
grid-area: Sidebar;
}
.red
element the grid-area name Content
, the .green
element the grid-area name Header
, and the .blue
element the grid-area Sidebar
.grid-template-areas
property on the grid container element to determine how these different areas should be distributed. The grid-template-areas
takes as a value n
string values, where n
is the number of rows in the grid layout you're creating. The value will be the name of the areas separated by space. Each name specifies a column. So, if you want to have 2 columns in your grid and you want the Header
element to take 2 columns in a row, you can use the value "Header Header"
for the row.Header
which will span the 3 columns, and the second row will be the Content
which will span 2 columns and the Sidebar
which will be just one column:.container {
display: grid;
grid-template-areas: "Header Header Header"
"Content Content Sidebar";
grid-row-gap: 5px;
grid-column-gap: 5px;
grid-auto-rows: 100px;
}
grid-template-areas
should have 3 area names. The first string value is for the first row in the grid. As we just want the Header
to occupy it the value will be the Header
repeated 3 times.Content
area to span 2 columns. So, in the string value we repeat the Content
area twice then the Sidebar
."
and string values are separated by a space.grid-row-gap
and grid-column-gap
. We also set the default row height to 100px
using grid-auto-rows
..container {
display: grid;
grid-template-columns: repeat(3, 1fr);
grid-template-rows: 100px;
}