{"id":372551,"date":"2022-08-25T12:28:00","date_gmt":"2022-08-25T19:28:00","guid":{"rendered":"https:\/\/css-tricks.com\/?page_id=372551"},"modified":"2022-10-04T11:24:29","modified_gmt":"2022-10-04T18:24:29","slug":"grid-column-start","status":"publish","type":"page","link":"https:\/\/css-tricks.com\/almanac\/properties\/g\/grid-column-start\/","title":{"rendered":"grid-column-start"},"content":{"rendered":"\n
The Based on CSS Grid\u2019s default auto-placement behavior, the second child element of the grid in this example should be placed in the second column. But we declared a This is the default<\/strong> value. It indicates the default span ( This syntax allows you to either use an integer<\/strong> to refer to a numbered grid line or a string<\/strong> to refer to a named grid line<\/a> or a named grid area<\/a>. In other words, you can specify a grid line by its numerical index<\/strong> or name<\/strong> to the starting edge of a grid item.<\/p>\n\n\n There are two grid lines before and after each grid track with a numerical index assigned to them automatically, starting from number one.<\/p>\n\n\n\n In the first example of this article, we used this syntax to refer to the third grid line by its index ( Note that you can also use a negative number to refer to a grid line, remember that it counts starting from the ending edge of the grid. The following code points to the same grid line in the previous example, but counts in reverse:<\/p>\n\n\n\n Notice the negative integers have been assigned to our grid as well as positive ones:<\/p>\n\n\n\n You can assign a custom name to a grid line using Let\u2019s go back to our example and name all its column track lines like the following declaration:<\/p>\n\n\n\n We can refer to the third line by our custom name instead of its index:<\/p>\n\n\n\n Note that the When defining grid areas using the Alternately, you can refer to the area\u2019s name to position an item at the starting line of the Since you are using the Here is a full example:<\/p>\n\n\n\n This sets the position of the This flavor of syntax allows you to position grid items by grid lines when there are repeated names. If there are grid lines with the same name, this syntax helps specify which of those lines you are referring to.<\/p>\n\n\n\n Let\u2019s assume you want to choose the third line, but that line has the same name as the first and the last grid line \u2014 all of them are called Note that the order doesn\u2019t matter, so the previous code can also be written like this as well:<\/p>\n\n\n\n Like the previous syntax, you can use a negative integer to count the grid lines starting from the end edge of the grid too. In our example, if we want to refer to the first Note that the integer value cannot be zero.<\/p>\n\n\n This syntax allows a grid item to span across the grid tracks. It can be specified in three different ways.<\/p>\n\n\n\n Note that if the integer is not specified anywhere in this syntax, the default value is Using the When we use You can combine Since the end line of the grid item is known ( If the specified grid line name is assigned to more than one grid line \u2014 in other words, if we have repeated named grid lines \u2014 we need to say which ones we want to target. To do that, we can add an integer to our value specifying which grid line we are referring to.<\/p>\n\n\n\n Take the following grid as an example:<\/p>\n\n\n\n We set the end line of the grid item to the fifth line. Then we want it to span towards the starting line until it hits a grid line named As a result, our grid item starts spanning toward the starting line from the fifth line, as illustrated below. The first line that it hits is named This syntax is helpful when you want to span a grid item towards a grid line using its name, but you are aware that there is more than one grid line with that name, so we add an integer to say we want the Setting the position of a grid item to a grid line\u2019s index or a grid line\u2019s name that does not exist in an explicit grid will force the grid to create implicit tracks to add that position to the grid.<\/p>\n\n\n\n Let\u2019s create a grid with two columns and set the position of its item to non-existent lines. In the following example, we have only two columns and we didn\u2019t name any of our gird lines. So, there is no line with an index of Since we know there are implicit tracks in our grid and we want them to have a size so we can see them, we add the Now that we have everything set up, let\u2019s see what happens to our grid:<\/p>\n\n\n\n We set As you may have seen in the previous examples, line-based positioning can cause empty spaces between grid items.<\/p>\n\n\n\n In case those holes aren\u2019t welcome in your design, they can be handled easily by applying the When positioning items across the grid, we can stack or overlap them on top of each other. This gives us the ability to sometimes use CSS Grid as an alternative to absolute positioning. For instance, we can put a caption layer on top of an image without using the Here\u2019s what we get:<\/p>\n\n\n\n By default, grid items stack in the source order, but you can control their level using the There are cases where you want a full-width item in your grid, meaning it starts from the first grid line and ends with the last one. For example, in a four-column grid, the end line index is It works like a charm, but what if the number of columns changes to six? In that case, we need to update the index value for the placement property.<\/p>\n\n\n\n The flexible solution is to use the negative index!<\/p>\n\n\n\n Now, no matter how many columns we have, the grid item with the The whole module of grid layout is based on the writing mode of the document; hence, grid lines depend on the direction and writing mode as well. For example, in a right-to-left language like Persian and Arabic, the first grid line with So, take the writing direction of your grid into account when using a grid line index to position grid items using a property like If a named grid area<\/a> has the same name as a line name<\/a>, the placement algorithm will prefer to use named grid area\u2019s lines.<\/p>\n\n\n\n Consider the following example:<\/p>\n\n\n\n The second grid line is named grid-column-start<\/code> CSS property is part of the CSS Grid Layout specification<\/a>, used to indicate the column grid line where a grid item starts in a grid layout. This property \u2014 among other line-based placement grid properties \u2014 controls the size of a grid item and where it sits on the grid.<\/p>\n\n\n\n
.grid-container {\n display: grid;\n grid-template-columns: 1fr 1fr 1fr;\n}\n\n.grid-item:nth-child(2) {\n grid-column-start: 3; \/* Item starts on the third column line *\/\n}<\/code><\/pre>\n\n\n\n
grid-column-start<\/code> position on the third grid line, moving the grid item into the third column and aligning its starting edge with the third grid line.<\/p>\n\n\n\n\n\n\n\n
grid-column-start<\/code> CSS Grid property.<\/figcaption><\/figure>\n\n\n\n
\n
Table of contents<\/h3>\n <\/summary>\n \n
\n
Syntax<\/h3>\n\n\n
grid-column-start: <grid-line><\/code><\/pre>\n\n\n\n
\n Full definition <\/summary>\n \n\n
where\n<grid-line> =\n auto |\n <custom-ident> |\n [ [ <integer [\u2212\u221e,\u22121]> | <integer [1,\u221e]> ] && <custom-ident>? ] |\n [ span && [ <integer [1,\u221e]> || <custom-ident> ] ]<\/code><\/pre>\n\n\n<\/details>\n\n\n
auto<\/code><\/li>
Values<\/h3>\n\n\n
\/* Keyword value *\/\ngrid-column-start: auto;\n\n\/* <custom-ident> value *\/\ngrid-column-start: myLineName;\ngrid-column-start: myGridArea;\n\n\/* <integer> + <custom-ident> values *\/\ngrid-column-start: 3;\ngrid-column-start: -2;\ngrid-column-start: main-area 2;\ngrid-column-start: 3 sidebar-start;\n\n\/* span + <integer> + <custom-ident> values *\/\ngrid-column-start: span 3;\ngrid-column-start: span main;\ngrid-column-start: span myarea 5;\n\n\/* Global values *\/\ngrid-column-start: inherit;\ngrid-column-start: initial; \/* same as `auto` *\/\ngrid-column-start: revert;\ngrid-column-start: revert-layer;\ngrid-column-start: unset;<\/code><\/pre>\n\n\n
auto<\/code><\/h4>\n\n\n
1<\/code>) and auto-placement behavior, which means the grid item is automatically placed in the next available empty grid cell.<\/p>\n\n\n
<custom-ident><\/code><\/h4>\n\n\n
Positioning Items by line numbers<\/h5>\n\n\n
<\/figure>\n\n\n\n
3<\/code>) to align the starting edge of the grid item with the starting edge of the third column using
<custom-ident><\/code> syntax:<\/p>\n\n\n\n
.grid-item:nth-child(2) {\n grid-column-start: 3;\n}<\/code><\/pre>\n\n\n\n
.grid-item:nth-child(2) {\n grid-column-start: -2; \/* same as 3 *\/\n}<\/code><\/pre>\n\n\n\n
grid-column-start<\/code> CSS Grid property.<\/figcaption><\/figure>\n\n\n
Positioning Items by line names<\/h5>\n\n\n
grid-template-columns<\/code><\/a> and
grid-template-rows<\/code><\/a> and use line-based placement grid properties to refer to that line by its name.<\/p>\n\n\n\n
.grid {\n display: grid;\n grid-template-columns: [first] 1fr [second] 1fr [third] 1fr [last]; \n}<\/code><\/pre>\n\n\n\n
.grid-item:nth-child(2) {\n grid-column-start: third; \/* same as index number 3 *\/\n}<\/code><\/pre>\n\n\n\n
<custom-ident><\/code> cannot take the
span<\/code> value since
span<\/code> is a reserved keyword for grid placement properties (e.g.
grid-column: 1 \/ span 2<\/code>).<\/p>\n\n\n
Positioning Items by grid areas<\/h5>\n\n\n
grid-template-areas<\/code><\/a> property, you get implicit line names<\/a> for free based on the name of the areas. For instance, a grid area with the name
sidebar<\/code> generates a line name
sidebar-start<\/code> before it and a
sidebar-end<\/code> after it. You can refer to these lines to set the position of a grid item.<\/p>\n\n\n\n
.grid-item:nth-child(2) {\n grid-column-start: sidebar-start;\n}<\/code><\/pre>\n\n\n\n
sidebar<\/code> named area:<\/p>\n\n\n\n
.grid-item:nth-child(2) {\n grid-column-start: sidebar;\n}<\/code><\/pre>\n\n\n\n
grid-column-start<\/code> property in this example, the browser understands that you want the
sidebar-start<\/code> line of the
sidebar<\/code> area; therefore, it will align the starting edge of the grid item with the starting edge of the grid area.<\/p>\n\n\n\n
<body>\n <main><\/main>\n <aside><\/aside>\n<\/body><\/code><\/pre>\n\n\n\n
body {\n display: grid;\n grid-template-columns: 1fr 1fr 1fr; \n grid-template-areas: \"main main sidebar\";\n}\n\naside {\n grid-column-start: sidebar-start;\n}<\/code><\/pre>\n\n\n\n
aside<\/code> element to the
sidebar<\/code> area in our grid.<\/p>\n\n\n
<integer> && <custom-ident>?<\/code><\/h4>\n\n\n
.grid {\n display: grid;\n grid-template-columns: [bar] 1fr [foo] 1fr [bar] 300px [bar];\n\n \/*\n Using repeat() function also gives you repeated named grid line, for example:\n grid-template-columns: repeat(3, [bar] 1fr);\n *\/\n}<\/code><\/pre>\n\n\n\n
bar<\/code>. Since the second<\/em> line named
bar<\/code> is the third grid line, you can use
2<\/code> to select it as the starting point:<\/p>\n\n\n\n
.grid-item:nth-child(2) {\n grid-column-start: 2 bar; \/* The second `bar` named line which is the third line *\/\n}<\/code><\/pre>\n\n\n\n
<\/figure>\n\n\n\n
.grid-item:nth-child(2) {\n grid-column-start: bar 2;\n}<\/code><\/pre>\n\n\n\n
bar<\/code>, we can count starting from the ending edge of our grid and write it like this:<\/p>\n\n\n\n
.grid-item:nth-child(2) {\n grid-column-start: -3 bar;\n}<\/code><\/pre>\n\n\n\n
span && [ <integer> || <custom-ident> ]<\/code><\/h4>\n\n\n
1<\/code>.<\/p>\n\n\n
span <integer><\/code><\/h5>\n\n\n
span<\/code> keyword followed by an integer indicates the number of tracks a grid item spans from a specific grid line. For example, if we want a grid item to span three column tracks towards its starting edge, we can apply the following value:<\/p>\n\n\n\n
.grid-item:nth-child(2) {\n grid-column-start: span 3;\n}<\/code><\/pre>\n\n\n\n
grid-column-start<\/code> property.<\/figcaption><\/figure>\n\n\n\n
span<\/code> for placing the starting line of an item, technically, it spans backward instead of forward to the ending edge, which is usually what happens in real-world scenarios.<\/p>\n\n\n
span <custom-ident><\/code><\/h5>\n\n\n
span<\/code> with a name of a grid line to make the grid item expand until it reaches that specified grid line.<\/p>\n\n\n\n
.grid-item:nth-child(3) {\n grid-column-start: span thirdline;\n grid-column-end: 6;\n}<\/code><\/pre>\n\n\n\n
6<\/code>) we can span the item towards the starting line until it hits a grid line named
thirdline<\/code>.<\/p>\n\n\n\n
thirdline<\/code> using the
grid-column-start<\/code> CSS property.<\/figcaption><\/figure>\n\n\n
span <custom-ident> <integer><\/code><\/h5>\n\n\n
.grid-container {\n display: grid;\n grid-template-columns: [y] 1fr [x] 1fr [x] 1fr [y] 1fr [x] 1fr [x];\n}\n\n.grid-item:nth-child(3) {\n grid-column-start: span x 2;\n grid-column-end: 5; \/* equivalent to grid-column-end: x 3; *\/\n}<\/code><\/pre>\n\n\n\n
x<\/code>. And since we want it to be the second
x<\/code> grid line, we wind up with
grid-column-start: span x 2<\/code>.<\/p>\n\n\n\n
y<\/code>, next is the first
x<\/code>, and finally, it hits the desired second line named
x<\/code>.<\/p>\n\n\n\n
grid-column-start<\/code> property<\/figcaption><\/figure>\n\n\n\n
N<\/code> of that grid line.<\/p>\n\n\n
Positioning grid items may generate implicit tracks<\/h3>\n\n\n
5<\/code> and no line with the name of
test<\/code>, but we set these values to our properties:<\/p>\n\n\n\n
.grid {\n display: grid;\n grid-template-columns: 1fr 1fr;\n grid-auto-columns: 100px;\n}\n\n.grid-item {\n gird-column-start: span test;\n grid-column-end: 5;\n}<\/code><\/pre>\n\n\n\n
grid-auto-columns<\/code><\/a> property to our code.<\/p>\n\n\n\n
grid-column-end<\/code> to
5<\/code>, but the last index of our explicit grid is
3<\/code> and because of that, two implicit tracks are created towards the end of the grid to give us a line with an index of
5<\/code>. On the other hand, we set the
grid-column-start<\/code> to a grid line with the name of
test<\/code>. Again, since we don\u2019t have such a name in our grid, and because we want that grid line to align with the start edge of our grid item, an implicit column track gets created on the starting side of the explicit grid in the inline direction, and its line is
test<\/code>.<\/p>\n\n\n
Positioning grid items may cause holes<\/h3>\n\n\n
grid-auto-flow<\/code><\/a> property with the
dense<\/code> value to the grid container.<\/p>\n\n\n\n
grid-auto-flow<\/a><\/code> property can fill empty spaces caused by line-based positioning.<\/figcaption><\/figure>\n\n\n
Stacking grid items<\/h3>\n\n\n
position<\/code> property as demonstrated below:<\/p>\n\n\n\n
<figure>\n <img src=\"image.png\" alt=\"how dare you leave alt empty?\">\n <figcaption>The caption of our image<\/figcaption>\n<\/figure><\/code><\/pre>\n\n\n\n
figure {\n display: grid;\n}\n\nimg,\nfigcaption {\n grid-column-start: 1;\n grid-column-end: -1;\n grid-row-start: 1;\n grid-row-end: -1; \n}<\/code><\/pre>\n\n\n\n
<\/figure>\n\n\n\n
z-index<\/code><\/a> property. In the following example, we overlap some items, and we use the
z-index<\/code> property to bring the second item to the highest level:<\/p>\n\n\n\n
.item:nth-child(2) {\n grid-column-start: 2;\n grid-column-end: 4;\n grid-row-start: 2;\n grid-row-end: 4;\n z-index: 1;\n}<\/code><\/pre>\n\n\n\n
z-index<\/a><\/code> property.<\/figcaption><\/figure>\n\n\n
Using a negative index for more flexibility<\/h3>\n\n\n
5<\/code>, so you can write the code below to have your full-width item.<\/p>\n\n\n\n
.grid-container {\n display: grid;\n grid-template-columns: repeat(4, 1fr);\n}\n\n.grid-item.full-width {\n grid-column-start: 1;\n grid-column-end: 5;\n}<\/code><\/pre>\n\n\n\n
.grid-item.full-width {\n grid-column-start: 1;\n grid-column-end: -1;\n}<\/code><\/pre>\n\n\n\n
full-width<\/code> class always stretches across the grid.<\/p>\n\n\n
Grid line indexes depend on the writing mode<\/h3>\n\n\n
1<\/code> index is the right-most line in a grid.<\/p>\n\n\n\n
grid-column-start<\/code>.<\/p>\n\n\n
Named area wins over named line<\/h3>\n\n\n
.container {\n display: grid;\n grid-template-columns: 1fr [y] 1fr 1fr 1fr 1fr;\n grid-template-areas: \"x x x y y\";\n}<\/code><\/pre>\n\n\n\n