CSS Grid assistance has actually been extensively offered given that March 2017 in all significant web browsers. Here we are in 2024, and I still see couple of individuals utilizing the grid design template locations include.
It's not a surprise that numerous prevent design template locations as understanding the grid is challenging enough. In this interactive post, I intend to clarify this function and, ideally, persuade you to utilize it more frequently. When you see the simpleness and power of design template locations, you might grab them a lot more regularly.
Intro
In the copying, we have a grid design with 3 columns.
page screen: grid; grid-template-columns: 1fr 1fr 1fr; space: 1rem;
If I require to place kid products within the grid, I require to define the line number for each product by utilizing grid-column.
item-1 3;. item-2 4;
Here is the outcome.
Initially look, it appears great and works as anticipated. Have you believed about how I came up with the line numbers? I do not constantly like to open the DevTools and switch on the line numbers so I can position the products where I require them.
The line numbers look something like this:
You can't see the line numbers up until you examine the aspect in the internet browser DevTools. Let's take a fast test.
Think about the following CSS grid:
page display screen: grid; grid-template-columns: 1fr 1fr 1fr 1fr 1fr; space: 1rem;
Compose the line numbers required to put the 2nd product in the last 3 columns.
⚠ Don't open the DevTools, think it yourself.
If you thought it from the very first time, it's terrific. For me, I in some cases miss out on the proper line number and have to open the DevTools to get it.
See the below example with the line numbers triggered.
Looking at the line numbers, it sounds simpler? I concur too. This can end up being more tough if we require to put both the columns and rows.
Let's take it even more.
In the following demonstration, we have a design with 5 columns and 2 rows.
page screen: grid; grid-template-columns: 1fr 1fr 1fr 1fr 1fr; grid-template-rows: 1fr 1fr; space: 1rem;
Can you think what's the appropriate line numbers to position “Item 1” in the very first 3 columns?
Cool, the next action is to place the 2nd product in the last 3 columns. Attempt to think the line numbers worth.
Great! For me, handling line numbers is possible just if the DevTools are active. Usually, my brain is managing great deals of information while developing a design and “envisioning” the line numbers isn't among them.
CSS called grid locations
In CSS grid, we can call each grid location and recommendation it throughout the CSS.