Using CSS GRID for creating layouts with customized grid-template-areas that include empty cells and automatic placement

Struggling with implementing the css grid layout module to showcase a 12-column, 3-row grid.

The initial row should only contain two elements, positioned on each side of the grid with empty cells in between using ten periods.

Subsequent rows should automatically display the remaining elements.

Considering the use of grid-template-areas for better control over placement but facing issues as it requires specifying individual grid-area names for all elements.

Encountering complications while attempting to set the grid-area property to 'auto' for the remaining cells to fill the third row, observing that they occupy the empty cells from the first row instead.

Would appreciate advice on the most efficient solution to resolve this problem.

Provided below is the code snippet:

.grid {
  display: grid;
  grid-template-areas: 
  "elem1 . . . . . . . . . . elem2" 
  "elem3 elem4 elem5 elem6 elem7 elem8 elem9 elem10 elem11 elem12 elem13 elem14";
}
.elem{
text-align: center;
color: white;
}
...
<div class="grid">
  <div class="elem elem1">
    elem1
  </div>
  ...
</div>

Your help is greatly appreciated.

Answer №1

When utilizing the grid-template-areas property within an implicit grid, the grid auto placement algorithm seeks out unoccupied cells to fill before creating a new row (refer to bottom for further details).

Simply specify your desired layout:

.elem:nth-last-child(-n + 6) {
  grid-row-start: 3;
}

This directive alters the default auto-placement process by positioning the last six items on row three.

.grid {
  display: grid;
  grid-template-areas: 
    "elem1 . . . . . . . . . . elem2" 
    "elem3 elem4 elem5 elem6 elem7 elem8 elem9 elem10 elem11 elem12 elem13 elem14";
}

/* NEW */
.elem:nth-last-child(-n + 6) {
  grid-row-start: 3;
}

.elem{
  text-align: center;
  color: white;
}
.elem1 {
  background-color: blue;
  grid-area: elem1;
}
.elem2 {
  background-color: red;
  grid-area: elem2;
}
(elem3-elem20 styling continues...)
<div class="grid">
  <!-- Grid item elements -->
</div>

The Grid Item Placement Algorithm consists of five main steps (0-4).

One key initial step involves positioning grid items that are not automatically placed, giving priority to items with specified positions.

Finally, in the process comes Position the remaining grid items, which states the following:

The auto-placement cursor determines the current "insertion point" in the grid, which is defined as a pair of row and column grid lines. Initially, the auto-placement cursor is set at the beginning row and column lines in the implicit grid.

This section also clarifies why auto-placed items begin on row 1, column 2:

If the item has automatic grid positioning in both axes:

Increase the column position of the auto-placement cursor until either this item’s grid area does not overlap any occupied grid cells, or the cursor’s column position, along with the item’s column span, exceeds the number of columns in the implicit grid, identified earlier in this algorithm.

Answer №2

To simplify the layout, utilize the default grid-auto-flow:row property and specify the start/end points for the "1st row - right" div using the grid-column attribute.

This eliminates the need for grid-areas altogether.

Check out the result below:

.grid {
  display: grid;
  grid-template-columns: repeat(12, 1fr);
  grid-auto-flow: row; /*typo corrected*/
}

.elem {
  text-align: center;
  color: white;
}

.elem1 {
  background-color: blue;
}

.elem2 {
  background-color: red;
  grid-column: 12/13;
}
...
... (CSS code continues)
...
</div>

Answer №3

To create a grid layout, you can insert an empty div in the HTML and use grid-template-columns to structure it accordingly. Here is an example:

.grid {
  display: grid;
  grid-template-columns: repeat(12, 1fr);
}

.spacer {
  grid-column: 2 / span 10;
}

.elem {
  text-align: center;
  color: white;
}
... (CSS classes continue)
<div class="grid">
  <div class="elem elem1">
    elem1
  </div>
  <div class="spacer"></div>
  <div class="elem elem2">
    elem2
  </div>
  ... (HTML content continues)
</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

What is preventing the buttons from filling the entire space of the parent element in this case?

https://i.stack.imgur.com/kbiWi.png I'm trying to figure out how to make the Repos and Stars buttons fill the entire height of their parent container, similar to the Github icon. Unfortunately, the code below is not achieving this effect. I attempted ...

What is the method for formatting within the <textarea> element?

While working on developing a comment system, I recently made the discovery that text areas cannot be formatted to retain paragraphs, line breaks, etc. This was particularly noticeable when comparing sites like StackOverflow, which uses a text editor inste ...

Is there a way to prevent elements from resizing when zooming in or out?

What is the best way to prevent elements from resizing on a standard website when users zoom in (CTRL +)? ...

How to save HTML content in a variable for future use in Next.js

When working with Next JS, my code resembles something like this: function Test() { return ( <section> <div>Test</div> </section> ); } Now, imagine that I want to have multiple entries here, gen ...

CSS form not aligning properly within wrapper div and appears to be floating outside of it

I am in the process of creating a website that includes a wrapper div containing a: -Header -Content -Footer The issue I am encountering is when I insert regular text into the content section, everything displays properly and the background stretches s ...

In Angular, encountering difficulty accessing object members within an array when using custom pipes

Here is a custom pipe that I have created, but I am facing an issue accessing the members of the customfilter array, which is of type Item. import { Pipe, PipeTransform } from '@angular/core'; import {Bus} from '/home/pavan/Desktop/Pavan ...

What is the best way to ensure that multiple bootstrap buttons in a row have the same width?

Could you help me figure out how to make the <div id="full"> element take up the full width of its parent container, while also ensuring that the 3 buttons inside share this width and have equal sizes with gaps between them? https://i.stac ...

"Utilizing HTML aids for creating text input fields and adding line breaks

public string BannerText {get;set;} public void SetBanner() { BannerText = "This is line 1. \nThis is line 2." } on my webpage, I am displaying it like this: <div> <h1><%: Model.BannerText %></h1> </div> The issue ...

Is it possible to uncheck a checkbox from a list, even if some of them are already unchecked?

I am attempting to implement a feature where checking a checkbox selects all others in the list, and unchecking one deselects all. Currently, only selecting one checkbox allows me to check all the rest, but unchecking one doesn't trigger the reverse a ...

What could be causing the image not to appear on the React app?

I'm currently working on a react website, and I'm facing an issue with the image display on the single product page. Despite trying to tweak both the react and CSS code, I haven't been able to resolve the problem. Below is my react file: im ...

What is the best way to apply CSS to my HTML code?

I have the files stored within my Django project. My directory structure for templates looks like this: |base.html | |rules | |style_base.css In my base.html file, I link to the stylesheet like this: <link type="text/css" href="/rules/style_b ...

Display MQTT information on a Django template

I've been utilizing paho-MQTT successfully to receive messages. However, I'm facing a challenge in displaying the received data in a template. Below is an excerpt of my code: import paho.mqtt.client as mqtt import json def on_connect(client, use ...

Invalid web address entered

While attempting to incorporate a functionality that opens a new window to a specific link when clicking an icon, I encountered an issue where the click action redirects to the wrong URL. Instead of directing to the URL specified in its href attribute, it ...

Creating dynamic web content using KaTeX and Node.js

When I attempt to display a complex formula using HTML and CSS, I encounter difficulties. Instead of the desired output, my screen is filled with confusing unicode characters. To resolve this issue, I decided to use KaTeX. I downloaded KaTeX into the dire ...

Auto-scrolling text box cursor movement

My query is quite similar to the topic discussed in this thread on automatic newline in textarea. However, my situation involves multiple textareas with a 1-row attribute, making it seem like writing on air due to the absence of visible borders (I've ...

"Looking for a solution to the ESLint error related to 'no-unused-var' and Google Maps integration. How can I effectively resolve

I'm struggling with what seems to be a simple problem I tried adding /* export myMap */ or /* global myMap */ at the beginning of the script but I keep getting errors Code HTML <h1>My First Google Map</h1> <div id="googleMap" ...

The React Component in Next.js does not come equipped with CSS Module functionality

I have been attempting to incorporate and apply CSS styles from a module to a React component, but the styles are not being applied, and the CSS is not being included at all. Here is the current code snippet: ./components/Toolbar.tsx import styles from & ...

Choose CSS Option

I need assistance with customizing my dropdown select option - see the example below <select> <option value="volvo" title="NEED THIS BIGGER AND FORMATED" >Volvo</option> <option value="saab">Saab</option> <option val ...

When attempting to check and uncheck checkboxes with a specific class, the process fails after the first uncheck

I have a set of checkboxes and one is designated as "all." When this box is clicked, I want to automatically select all the other checkboxes in the same group. If the "all" box is clicked again, I would like to deselect all the other checkboxes. Currently ...

What is the process for updating tabs and their content in React?

Here is where the error occurs in my JavaScript code. I have successfully implemented it in HTML, CSS, and simple JavaScript, but encountered issues when trying to do so in React. My goal is to switch tabs and their corresponding data/content: ...