What is the reason for using grid-lines instead of row and column numbers to position an item on the CSS grid?

During a recent tech presentation I delivered at my workplace on the new CSS grid spec, my manager posed an intriguing question that left me stumped. He wanted to know why elements positioned within a grid are identified based on the grid lines they fall between, rather than having a more straightforward method for identifying columns and rows. Although I am familiar with grid-template-areas, it still requires manual definition.

In essence, the question boils down to this: Why do we use grid line numbers instead of grid column numbers in the following example?

grid-column: 1/4;

Answer №1

Grid lines serve as boundaries for positioning grid items and defining grid areas with a clear starting and ending point.

Specifying that a grid item starts at grid line 1 and ends at grid line 4 indicates that the item should be within the space between those lines. On the other hand, stating that a grid item starts at column 1 and ends at column 4 raises questions about whether the item should occupy column 4 or not. This ambiguity could lead to overlapping issues if another item is placed in column 4 following similar semantics.

This distinction becomes crucial for auto-placement, as it may require creating new rows and columns when there isn't enough space in the explicit grid for additional items. For more information, refer to Section 8.5 of the specification.

Answer №2

The CSS Grid specification provides various methods for aligning and positioning items within a grid container, as outlined in the section titled Placing Grid Items.

Concerning your query:

Why do the numbers in the example correspond to grid line numbers rather than column numbers?

grid-column: 1 / 4;

This approach of placing grid items is known as "Line-based Placement", which focuses on the lines that define the boundaries of a grid area.

Line-based Placement

The grid-row-start, grid-column-start, grid-row-end, and grid-column-end properties specify the size and location of a grid item within the grid by defining its edges in terms of lines, spans, or automatic placement. This determines the start and end points of the grid area in relation to inline and block directions.

("inline-start" refers to the starting point of text, usually the left side in LTR writing. "block-start" corresponds to the top in horizontal writing. "-end" indicates the opposite direction. source)

If you prefer to count columns/rows instead of lines, utilize the span keyword.

For instance:

grid-column: 1 / 4

defines a grid area from column line 1 to column line 4 (in this case, spanning columns 1, 2 & 3)...

grid-column: 1 / span 3

achieves the same result but counts the columns instead.

There are additional techniques available. Refer to the details here:

Below is a demonstration showcasing these two methods:

article {
  display: grid;
  grid-template-columns: repeat(12, 75px);
  grid-auto-rows: 50px;
  grid-gap: 10px;
}

section:nth-child(1) {
  grid-column: 1 / 4;
  grid-row: 1;
}

section:nth-child(2) {
  grid-column: 1 / span 3;
  grid-row: 2;
}

/* additional styles for demo */
section {
  background-color: lightgreen;
  border: 1px solid #ccc;
  display: flex;
  justify-content: center;
  align-items: center;
  text-align: center;
}
<article>
  <section><code>grid-column: 1 / 4<br>line counting</code></section>
  <section><code>grid-column: 1 / span 3<br>column counting</code></section>
  <section>3</section>
  <section>4</section>
  <section>5</section>
</article>

Answer №3

That's a great question! Here is a simple example of a grid layout:

https://i.sstatic.net/QDg69.png

In traditional CSS grid layouts, columns can be referenced by start and end line numbers like this:

grid-column: 2 / 4;

However, it could have been equally valid to specify column numbers instead:

 grid-column: 2 / 3;

The methods for defining columns would still work in the same way:

 grid-column: 2 / -1;

 grid-column: 2 / span 2;

The choice between referencing grid lines versus tracks (rows and columns) only becomes significant when trying to reference the last row or column from a starting point, leading to potential off-by-one errors.

So why did the W3C opt for lines over tracks? The answer lies in The Story of CSS Grid, from Its Creators, an article on A List Apart.

Peter Linss, Co-Chair of the CSS Working Group, proposed including the concept of grid lines in the spec to make it more intuitive for designers.

Rossen Atanassov, Editor of the spec at Microsoft, shared insights on combining app-centric models with traditional graphic design grids based on lines for a seamless implementation.

In essence, the decision to use grid lines aimed to bridge the gap between different design perspectives for the benefit of all users. While some may find the adoption of one-based addressing and grid lines unusual, it ultimately enhances the versatility and functionality of CSS grid layouts.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Responsive design and column alignment dilemma

I'm attempting to create a layout using Bootstrap 3.2.0 with tiled divs in a single row for screens wider than 768px. The divs have heights of either 50px or 100px, controlled by the classes "home-height-single" and "home-height-double," and widths ad ...

The JQuery loading symbol does not prevent keyboard interactions

I successfully implemented a Jquery busy indicator in my application using the following link: However, I noticed that even though it prevents users from clicking on input elements while loading, I can still navigate to these elements by pressing the tab ...

Personalizing the pop-up window using window.open

When clicking a hyperlink on a page, I need to open multiple pop-up windows. To achieve this, I must use the Window.open function instead of showModalDialog. However, I have noticed that the appearance is not satisfactory when using Window.open. (Essentia ...

Explore and target elements using browser inspection

Is it possible to create a new CSS class using browser inspection that contains new hover/focus styles? For example: .CanIDo { /*some css rules (ex.)*/ width: 100; } /*my question (USING BROWSER INSPECTOR, not directly typing in the CSS file)*/ .CanI ...

The media query was running smoothly until it unexpectedly stopped functioning

I've encountered an issue where something that used to work perfectly fine suddenly stopped working :( @media only screen and (max-width: 768px) { form, button, input { width: 80vw !important; } } <meta name="viewport" content="width=de ...

seize the cursor on chrome for windows

I'm experiencing an issue with two cursors appearing in Windows Chrome version 31.0.1650.57. The two cursors are grab and a default arrow. I have applied the following CSS: CSS div { width:200px; height:200px; background-color:maroon; ...

Transitioning from clip to clip-path in order to utilize percentage values

My current approach involves using a scss-based animation to create border animations with movement. The animation's core functionality relies on the use of clip. $box-width: 80px; $box-height: 50px; @keyframes clipMe { 0%, 100% { ...

Instructions for including a scroll bar in an HTML side menu

Can anyone help me figure out how to add a scroll bar specifically for the left side navigation menu? I've tried adding overflow-y: scroll; in different places, but it hasn't worked so far. I'm still learning web design and have been piecing ...

The behavior of a fixed position in Flexbox varies when comparing Chrome to Safari and Firefox

In Chrome, the following code works as expected, with the list element positioning itself at the very left of the .PageContainer when scrolling down. However, in Safari and Firefox, the list element positions itself just after the logo. Are there any CSS r ...

JavaScript error leading to ERR_ABORTED message showing up in the console

When I try to load my HTML page, my JavaScript code keeps throwing an error in the console. I am attempting to import some JavaScript code into VSCode using an app module for future reuse. The code is being run through a local server. Error Message: GET ...

It is important for the button size to remain consistent, regardless of any increase in text content

My goal was to keep the button size fixed, even as the text on it grows. This is the current code I am using: .button { border: none; color: white; padding: 10px 50px; text-align: center; text-decoration: none; display: inline-block; font ...

I've created a logo, but why are there two logos on my website?

There seems to be an issue with two divs stacking on top of each other, which is not the desired layout. This problem occurs when five div boxes are added. The goal is to have all divs in a single row, but it's unclear why this layout is not working ...

The performance of ASP.net text-boxes is inconsistent

After days of searching for a solution, I am still unable to resolve my issue. My website features a simple web-form with text-boxes that are supposed to appear when the user selects the "custom search" option. While testing locally, everything works fine. ...

The positioning of the Bootstrap dropdown menu is not aligned correctly

Issue with positioning Bootstrap dropdown I recently encountered a problem where I placed a bootstrap dropdown inside a div that centers the dropdown using CSS "text-align: center", but the dropdown menu still appeared in its original position. It seems l ...

Getting the CSS for an element's "Active" state using jQuery

Is there a way to retrieve the CSS for the :active state using jQuery? I am looking to create dynamic code so that I don't need to manually adjust the jQuery every time I want to style an element. Edit To clarify, I am trying to avoid using .addClas ...

Faux CSS, every other moment appears to be unsuccessful

My HTML code looks like this: <div class="container"> <div class="foo">Foo!</div> <!-- if this is red... --> <div class="bar">Bar!</div> </div> <div class="container"> <div class="foo">Foo! ...

What are the advantages of importing CSS files from JS source code with webpack, rather than building CSS in isolation as traditionally done?

If you're looking for information on how to load CSS with webpack, check out the documentation here: https://webpack.js.org/guides/asset-management/#loading-css1 The webpack documentation suggests importing CSS files from JavaScript code and using ex ...

Tips for positioning a Wordpress navigation bar to the right of a logo

My goal is to position the navigation bar next to the logo in my Wordpress theme using Underscores. I have successfully aligned the primary navigation and the logo as desired with the following CSS: .main-navigation { position: relative; float: ri ...

I'm looking to adjust the padding on the left and right sides of the v-select menu checkbox in Vuetify 3. How

While trying to reduce the left padding of the v-select menu checkbox using the F12 debugger, I encountered an issue where the menu suddenly disappears when clicked. This makes it difficult to see the active menu. Attached is a screenshot of the select me ...

Issue with FullPageJS: scrollOverflow feature not functioning upon click event

I am currently working on a Fullpage.js instance that needs to be initialized with a click event and then destroyed when switching to another page, and vice versa. The scrollOverflow feature works perfectly as long as the #fullpage element is not hidden up ...