If the value of grid-column-end exceeds the maximum number of columns in a CSS grid, what will occur?

I have been delving into the CSS grid system with insight from MDN. In the segment on line based placement, after experimenting a bit, I came across an unusual behavior. When the layout consists of two columns, for example defined as grid-template-columns: 1fr 3fr;, and the grid-column-end for one of the child elements is set greater than 2, it results in empty columns with their own grid-gap. An illustrative example can be seen at this link https://jsfiddle.net/u1paezkw/. By inspecting the header, you'll notice dotted lines representing these surplus empty columns.

                body {
                    width: 90%;
                    max-width: 900px;
                    margin: 2em auto;
                    font: .9em/1.2 Arial, Helvetica, sans-serif;
                }
                
                .container {
                    display: grid;
                    grid-template-columns: 1fr 3fr;
                    grid-gap: 20px;
                }
header {
    grid-column: 1 / 13;
    grid-row: 1;
}

article {
    grid-column: 2;
    grid-row: 2;
}

aside {
    grid-column: 1;
    grid-row: 2;
}

footer {
    grid-column: 1 / 3;
    grid-row: 3;
}
        
header,
footer {
  border-radius: 5px;
  padding: 10px;
  background-color: rgb(207,232,220);
  border: 2px solid rgb(79,185,227);
}
        
aside {
  border-right: 1px solid #999;
}  
<!-- Learn about this code on MDN: https://developer.mozilla.org/en-US/docs/Learn/CSS/CSS_layout/Grids -->

<div class="container">
  <header>This is my lovely blog</header>
  <article>
    <h1>My article</h1>
    <p>Duis felis orci, pulvinar id metus ut, rutrum luctus orci. Cras porttitor imperdiet nunc, at ultricies tellus laoreet sit amet. Sed auctor cursus massa at porta. Integer ligula ipsum, tristique sit amet orci vel, viverra egestas ligula. Curabitur vehicula tellus neque, ac ornare ex malesuada et. In vitae convallis lacus. Aliquam erat volutpat. Suspendisse ac imperdiet turpis. Aenean finibus sollicitudin eros pharetra congue. Duis ornare egestas augue ut luctus. Proin blandit quam nec lacus varius commodo et a urna. Ut id ornare felis, eget fermentum sapien.</p>

    <p>Nam vulputate diam nec tempor bibendum. Donec luctus augue eget malesuada ultrices. Phasellus turpis est, posuere sit amet dapibus ut, facilisis sed est. Nam id risus quis ante semper consectetur eget aliquam lorem. Vivamus tristique elit dolor, sed pretium metus suscipit vel. Mauris ultricies lectus sed lobortis finibus. Vivamus eu urna eget velit cursus viverra quis vestibulum sem. Aliquam tincidunt eget purus in interdum. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.</p>
  </article>
  <aside>
    <h2>Other things</h2>
    <p>Nam vulputate diam nec tempor bibendum. Donec luctus augue eget malesuada ultrices. Phasellus turpis est, posuere sit amet dapibus ut, facilisis sed est.</p>
  </aside>
  <footer>Contact <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0d60684d60747e647968236e6260">[email protected]</a></footer>
</div>               

  1. Could someone enlighten me about this phenomenon and also provide an official reference? How can there exist more columns than those specified in grid-template-columns?

  2. Regarding grid-column: 1 / 3, where is the third column located? Why does the line end at the conclusion of the second column?

Answer №1

When I set the grid-column-end to be greater than 2 for one of the children, it creates empty columns with their own grid-gap.

To clarify, it should actually be greater than 3 and not 2. With 2 columns, you have 3 lines (1, 2, 3) so you can position an element across both columns by setting grid-column: 1 / 3.

https://i.sstatic.net/5pBOr.png

https://www.w3.org/TR/css-grid-1/#placement

As mentioned in the comment, understanding the concept of explicit and implicit grids is essential. You define the explicit grid, while the implicit grid is generated automatically.

The properties grid-template-rows, grid-template-columns, and grid-template-areas collectively define the explicit grid of a grid container. Any grid items placed outside the explicit grid result in the creation of implicit tracks, which are sized by the grid-auto-rows and grid-auto-columns properties.ref

In addition,

If a grid item is positioned in a row or column that is not explicitly sized by grid-template-rows or grid-template-columns, implicit grid tracks are created to accommodate it. This occurs when a grid item is positioned in a row or column that is out of range or through the auto-placement algorithm generating additional rows or columns. The grid-auto-columns and grid-auto-rows properties specify the size of these implicitly created tracks.ref

If your grid definition does not contain all elements, extra rows/columns are added to create the implicit grid. This ensures that all grid items belong within the grid structure.

In this scenario, using grid-column:1/13 will result in a total of 12 columns, where 2 are explicitly defined and the rest are set to

auto</code, creating 11 gaps as well.</p>
<hr />
<p>You can construct an entire grid without any explicit definitions and solely rely on the implicit grid.</p>
<p>For example:</p>
<div>
<div>
<pre class="snippet-code-css lang-css"><code>.box {
  display: grid;
  grid-gap: 10px;
}

.box > * {
  height: 50px;
  background: red;
}

/* This rule will make the grid have 3 columns */
.box > *:nth-child(3) {
  grid-column: 3;
  background: green;
}
<div class="box">
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
  <div></div>
</div>

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

You are unable to use multiple background colors on a material UI snackbar

Is there a way to apply multiple background colors to a material UI snackbar? I attempted using linear-gradient as shown below, but it didn't work. import Snackbar from 'material-ui/Snackbar'; const bodyStyle = { border: `2px solid ${co ...

When the width of a single table is large, Bootstrap tables can appear misaligned and not properly

I am facing an issue with displaying two tables side by side. Whenever the width of the first table is too large, the second table is pushed below it. https://i.sstatic.net/w8zwp.png https://i.sstatic.net/SZdZU.png Is there a way to make them stay side b ...

What is the solution for incorporating multiple elements in knockout's applyBindingsToNode function?

I am currently using knockout applyBindingsToNode to dynamically add and remove elements in order to update my html. I need to cut the binding, which is why I am utilizing applyBindingsToNode. In an example I have provided, if you click on the button "Reb ...

What is causing the elements on the screen to not occupy the entire screen?

My web-app on react is not displaying full-screen, even though there are very few elements on the page. https://i.sstatic.net/RYc6D.jpg https://i.sstatic.net/yhqey.jpg https://i.sstatic.net/uKuLD.jpg Css: html { height: 100%; width: 100%; } * { margin: ...

Two dropdown menus opening simultaneously

Recently, I encountered an issue while using a dropdown menu within a div. Even though it was functioning properly, I noticed that when I included two dropdown menus on the same page, clicking on the first one would cause both the first and second dropdown ...

Steps to insert a logo into a Bootstrap navbar

Hey there, I'm having trouble fitting my logo into a Bootstrap navbar. The issue is that the logo appears larger than the navbar itself, and I've tried various solutions without success. Can someone please assist me with this problem? Below is th ...

Injecting CSS into a dynamically created element within a Polymer component using jQuery

Having an issue when trying to add a CSS class to a jQuery created element within a Polymer component. The code for the Polymer component is as follows: <dom-module id="image-add"> <style> .image { height: 100%; } </sty ...

Issue with fluid layout in CSS - Unable to specify height in pixels and minimum height property not functioning as expected

While working on a liquid layout, I have set all my width and height values in percentages. However, during the design process, I needed to check how my CSS was rendering so I temporarily set the height in pixels. Eventually, the height in percentage wil ...

Capture value from HTML datalist and insert into textarea

Is it possible to extract the value from a datalist and have it displayed in a textarea? I am currently using a script called "selectProgram" for this purpose. However, I noticed that an extra input textfield appears when I include select tags. Stran ...

Issue with Bootstrap modal not displaying in the center as expected

screenshot of the page Hey there, I'm struggling to understand why this modal is displaying incorrectly. I copied the code from the bootstrap website. Here it is: <button type="button" class="btn btn-info btn-lg" (click)="showPerformedActivityMod ...

Issues with CSS not loading properly on EJS files within subdirectories

This is the structure of my folders (with views located in the root directory): views/contractor/auth/login.ejs When I access that file, the CSS styles are not being applied. The connection to the CSS file, which is located in the public directory in th ...

Unable to adjust image opacity using jQuery

I am attempting to change the opacity of an image based on a boolean flag. The image should have reduced opacity when the var pauseDisabled = true, and return to full opacity when pauseDisabled = false. To demonstrate this, I have created a fiddle below. ...

Issue where the link in the first column of a horizontal scrolling table is unclick

In my attempt to create a fixed first column horizontal scrolling table, I encountered an issue where hyperlinks in the first column become unclickable once scrolled to the right. This seems to be because the fixed column is not clickable by default, and o ...

To activate two separate click events, one for an absolute element and another for the element positioned behind it, simply click on the desired absolute element to trigger both actions

Is it possible to trigger click events for both of these elements? position: relative container ELEMENT 1: standard div ELEMENT 2: position: absolute div In real life, element 2 is a transparent backdrop designed to capture clicks on top of the header ...

Struggles encountered when choosing the initial visible item

I have a set of 3 tabs, each with its own heading and content. However, I only want to display the tabs that the user selects by checking the corresponding checkboxes. There are 3 checkboxes, one for each tab. Below is the code snippet: //Function to ...

What could be the reason that the results of my quick sort function are not appearing on the screen

I'm having trouble getting any output from this code. Can someone help me figure out what's wrong? function Ascending() { var array = new Array(); array[0]=parseInt(document.getElementById("1").value); array[1]=parseInt(document.getElementById(" ...

The functionality of JQuery's .hover() is disabled once an HTML onClick() event is activated

I am currently working on a webpage and attempting to incorporate JQuery into it for the first time. However, I seem to be encountering some issues that I believe might have simple solutions. Background Information My JQuery code only contains one event l ...

What is the best way to use a CSS selector to target multiple divs such as block-11, block-12, and so

Here are several block IDs I have: <div id="block-11">content Here</div> <div id="block-12">content Here</div> <div id="block-13">content Here</div> <div id="block-14">conten ...

Looking to run a JavaScript command without the need for any triggering events?

Is there a way to execute the following JavaScript code without using any event and have it run at the start, onLoad of the page? function calculateDiscount() { var totalPrice = document.getElementsByClassName("Total")[0].innerHTML; var discountedPr ...

Transferring data from JavaScript to an HTML page

I'm currently developing a rock, paper, scissors game using HTML and JS. Here is the link to the complete code (CSS, JS, HTML): http://jsfiddle.net/fJw4R/. You can also view the game with images here: The functionality of the .math-module is working ...