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:

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

What is the reason for the lack of letter-spacing between a nested inline element and the next character that follows?

Upon reviewing the specifications for letter-spacing, it is evident that runs of atomic inlines (e.g. inline-block) are considered as a single character (http://www.w3.org/TR/css3-text/#letter-spacing): For letter-spacing purposes, each consecutive run ...

Combining the p class with flexbox for optimal layout design

I have four flexed container boxes and I am looking to add text below them in a row. Does anyone have suggestions on how I can achieve this alignment, as well as tips for optimizing the code? .container { display: flex; flex-direction: row; just ...

What is the best way to center my dropdown menu on the page?

Discover my exclusive menu by visiting this link. For those who are curious about the source code, here's the HTML snippet: <div id='menu-container'> <ul id='menu' class="menu"> <li class='active'>& ...

Fix the positioning of a div in BootStrap and CSS code

Hey there, I'm relatively new to CSS/Bootstrap and currently in the process of learning. I might need to incorporate some CSS into the "chat_funcs" div, but unsure about what code to input to achieve my desired outcome. The issue at hand: What I am ...

challenges with website design on Internet Explorer 7

Welcome! This is my HTML code: <div style="width:220px;float:left;" id="radio_text"> <ul style="width:20px;float:left;display:block;"> <li style="list-style:none;line-height:13px;height:13px;float:left;"><input type="radio ...

Implementing image max-width and maintaining aspect ratios for responsiveness

Within the code snippet below and utilizing Bootstrap, a grid layout is defined with several .block components, each comprising an image and a title. The images are designed to be larger than the column size so that they can expand to full width for respon ...

Deleting various classes (jQuery)

Is there a more effective alternative to rewriting the following code snippet? $('element').removeClass('class1').removeClass('class2'); I am unable to utilize removeClass(); because it would eliminate ALL classes, and that ...

Sass Alert: The mixin called roboto-family is missing from the stylesheet. Trace: Problem detected in src/pages/forms/forms.scss at

Greetings, I am diving into the world of Ionic for the first time. Recently, I embarked on a new project in Ionic and decided to integrate a theme. To do so, I copied an .html file, an .scss file, and also created a .ts file. Forms.html <!DOCTYPE html ...

Looking to add a dropdown feature to my current main navigation bar

I've been struggling to add a drop-down menu to my website's main menu. Every time I try, something goes wrong - sometimes the menu appears inline, other times it completely messes up the layout. Here is the HTML code snippet: <ul class="m ...

What are some effective ways to stretch specific muscles?

I am facing an issue with my select element. The default value is "please choose," but there are options like "Accommodation & Hotels" that are longer and not clearly visible on IE browsers, though they work fine in Firefox. How can I resolve this issue? S ...

Placing a CSS image with absolute positioning on top of a responsive image

Is there a way to keep the balloon image position fixed in relation to the grid image when resizing it? The balloons Balloon1 and B2 are technically within grid 5 and 7, but if you resize the grid left or right, the balloons will go off-center. Do I requ ...

Eliminating Unnecessary Stylesheets in Vite and Vue Website

When working on the Vite-Vue application, I noticed that the styles I implemented for both index.html and Vue components/views are showing up as crossed out in the browser. Additionally, many of the styles I added to the components/views are also being cro ...

Execute script when on a specific webpage AND navigating away from another specific webpage

I created a CSS code that adds a fade-in effect to the title of my website, and it works perfectly. However, I now want to customize the fade-in and fade-out effect based on the page I am transitioning from. My desired outcome: If I am leaving the "Biolo ...

Ensure that the <aside> elements span the entire width of their containing parent element

I am currently designing a website and I am aiming for a specific layout. Any advice on how to achieve this would be greatly appreciated. The header of the page consists of: a logo aligned to the left within an <a> tag 2 widgets placed in <asid ...

Extracting CSS data and storing it in a SQL database

Hello there, I've created a div using CSS like this: echo '#'. $pess2['naam'] .' { width: 190px; height: 90px; padding: 0.5em; float: left; left:'. $pess2['left'] .'px; top:'. $pess2['top'] ...

Creating flexible items with a 1:1 ratio and ensuring that the padding on the left and right of the flex container remains consistent

Whenever I adjust the size of the window, the flex-container ends up with uneven padding on the left and right, which is less than ideal. So, I am looking for a way to allow the flex-item to resize when the window size changes, ensuring that the conta ...

Tips for aligning text to the left instead of the right when it exceeds container width in IE 11

Within the div, there is text with a 5px margin on the right. When the container div narrows, the text overflows from the container and loses the right margin. Is it feasible to maintain this margin on the right side while allowing the text to overflow fro ...

What could be the reason behind my code functioning properly on CodePen but not in my editor?

I'm currently using Sublime 3 for coding in HTML and CSS. After saving the files with the proper extensions, I included the Google Fonts to use the "Lobster" font. However, upon opening the .html file on Google Chrome, the Lobster font doesn't d ...

Margin ambiguity in a vue.js application

I am facing an issue with my Vue.JS project setup. I want the App.vue to occupy the entire page and have different routes displayed within App.vue using router-view. But, when I try to add a margin to the content of my Game component, the margin seems to ...

Is it possible for us to customize the angular material chip design to be in a rectangular shape

I recently implemented angular material chips as tags, but I was wondering if it's possible to customize the default circular design to a rectangle using CSS? ...