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:

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

.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

"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" ...

Show results from selecting multiple dropdown options

I am looking to create a search function that displays results based on multiple dropdown selections. I have 4 dropdown menus and a submit button, but I am unsure how to show the search results upon clicking the submit button. The desired outcome is to dis ...

simultaneous image hover effect for all images

I am experiencing an issue with my CSS and Bootstrap hover effect. The overlay tags are enclosed within a div class, but they all hover simultaneously. Each overlay belongs to a different Bootstrap grid, yet the effect extends even to the empty spaces betw ...

Pass the chosen option to PHP using Ajax within the same webpage, utilizing HTML

My Goal: I am working on a HTML/PHP page that displays the home page of my website. One section on this page is dedicated to highscores, with data retrieved from a database. Users can use a Select box to choose how the highscores are sorted. View the high ...

How can we seamlessly transition from adding a class to setting scrollTop on a particular div?

I have successfully implemented a jQuery script to change the background color of my navbar on scroll. Here's the code I used: jQuery(document).ready(function($) { $(window).scroll(function() { var scrollPos = $(window).scrollTop(), nav ...

Locate the initial div with a distinct class from the bottom upwards

If I have a HTML page and want to search for the last occurrence of a div with a specific class, how can I do that using Jquery? Assuming the following HTML structure: <div class="container"> <div id="1" class="something special"></div& ...

Issue with JQuery: Logo only becomes visible after scrolling

Recently, I added a jQuery script to modify the header as we scroll on our website. Everything is functioning smoothly except for one issue - the large logo on the left side doesn't appear when visitors first land on the page; it only shows up after t ...

Sending multiple forms at once with a single submission

I've been racking my brain trying to determine the feasibility of a particular task. I am currently utilizing zen-cart as my shopping cart software, but what I really want to achieve is creating a hardcoded page featuring a list of 7-9 products. Each ...

Perform a toggle action on the first row when clicking within the current row using Jquery

I've been grappling with the idea of creating a function that can display and hide a comment field when a button is clicked. The challenge here is that there are multiple line items with their own comment boxes. I want to find a way to achieve this wi ...

Utilizing HTML to hide rows within a table by comparing each row with the one that comes before it

I am in need of assistance to solve a project I am working on that involves a table structure similar to this: Below is the code snippet I am currently using: <table class = "table table-striped table-hover"> <thead> <tr> ...

Modifying the appearance of a CSS element with jQuery: Step-by-step guide

The code I have is as follows: $('.signup-form-wrapper').css("style", "display: block"); $('.login-form-wrapper').css("style", "display: none"); I'm not sure why it's not working. The current appearance of ...

Having trouble getting XSLT to work properly with the XML file

My XSL file doesn't appear to be functioning properly with my XML data. Despite the simplicity of my code, I am unable to identify the issue. Upon linking to the XSL spreadsheet and opening the document, my XML seems to transform into plain raw Data. ...

Guide to positioning an item at the bottom of the screen using Material UI

Can anyone help me align a button to the bottom of the screen so that it remains in place even when scrolling through a list? I've tried adjusting the code but can't seem to get it right. This is how my current screen appears, with the button al ...

Centered Navigation Bar Aligned with Header

I'm having trouble aligning my header vertically with the text in my nav-bar using Bootstrap 4.6. The nav-bar items are right-aligned, so the text isn't centered in the middle of the page like shown in picture 3. .jumbotron { background: #3 ...

Collect the text shown in an alert message created with JavaScript

I'm currently unsure if this is achievable or not. In one of my scenarios, I aim to extract text that appears in alert boxes on external websites by including my JavaScript file. Is there a method to obtain the text content shown in an alert message ...

Changing the image source when clicking on it and removing it can be done by using JavaScript

How can I add a class 'selected' to ".swiper-slide" when it is clicked? I also want to change the image src when svg-img1, 2, or 3 is clicked. However, I need the image to revert back to its default src when another swiper-slide is clicked. ...

What is the best way to deactivate certain JavaScript functions on my webpage when accessed through a mobile device?

Is there a way to disable specific JavaScript functions when accessing my website from a mobile device? While I can achieve this using CSS only, certain buttons require JavaScript functionality. Below is the internal JavaScript code: var menuBtn = docume ...

Ways to slightly boost the default font-size values when using wicked-pdf

In the wicked-pdf report, I have body content with varying font sizes. When I apply a font-size of 16px to the paragraph like so: p { font-size: 16px !important; } The entire paragraph's font size becomes 16px, which may include words with diff ...

Tips for retrieving HTML file content as a string using JavaScript

I'm looking to retrieve the contents of an HTML file using JavaScript and store it as a string. I tried writing some code for this purpose, but unfortunately, I encountered an error: Error: Cannot find module 'xmlhttprequest' Could someone ...

Add a script tag to every image in my HTML at once using C#

Managing a site with a thousand images can be overwhelming, especially when trying to add a script tag like <%=Settings.MyDomain%> to each one. I'm looking for a more efficient way to accomplish this task without spending hours on manual labor. ...