Tips for populating any leftover empty columns within a CSS grid

I've set up a grid with 5 columns. What I aim to achieve is, when columns 1, 2, and 5 are filled as shown in the picture, I want to position an item in the open spaces of columns 3 and 4.

Since the occupied columns may vary, I need the code to be adaptable and fill the first available empty columns before moving on to the next ones.

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

Answer №1

Utilizing CSS: employ the :empty pseudo-class to determine if an element is devoid of content.

Utilizing JavaScript: utilize innerHTML == '' to verify if the element has no content.

document.querySelectorAll('.grid div').forEach(e => {
  if (e.innerHTML == '') {
    console.log(e);
  }
})
.grid {
  display: grid;
  grid-gap: 1em;
  grid-template-columns: repeat(5, 1fr);
}

.grid div {
  background: green;
}

.grid div:empty {
  background: Red;
}
<div class="grid">
  <div>1</div>
  <div>2</div>
  <div></div>
  <div></div>
  <div>5</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

In CAKEPHP, emphasize a row in a Product table to indicate a comparison condition, without the need for hovering

I've been experimenting with different approaches to make this comparison work. Despite trying various methods, only the "gumboots" string check seems to do the trick. This string represents a product name in the table and proves that PHP is not neede ...

Accessing an object from a webpage using Selenium: A step-by-step guide

Today marks my debut using Selenium IDE, and I must admit that my knowledge of web development is limited. My team is currently working on a website, and as part of my task, I need to write tests for it. The website features several charts showcasing data ...

Center the search box in responsive mode

I am utilizing a bootstrap navigation bar as shown in this source. My aim is to ensure that on mobile devices, users do not have to click on the menu icon to search on the page. I want to display the search box immediately after the Brand in responsive mod ...

Using PHP to create dynamic text within email messages

I'm currently working on creating dynamic email content, and I have successfully implemented a solution for images. However, I am facing difficulty when it comes to displaying text dynamically. The PHP code snippet below retrieves location data and r ...

Preventing CakePHP from inserting comment tags in the output

Currently, I am working on creating an endpoint that will return JSON data. {"a":1,"b":1} Surprisingly, when my code runs, Cake inserts an HTML tag into the output result. {"a":1,"b":1} <!-- 0.0964s --> This unexpected formatting is causing issue ...

What is the best way to continuously loop an image from left to right and right to left indefinitely using JavaScript?

As part of the challenge, I am tasked with creating a Pacman game using HTML, CSS, and JavaScript. One key aspect is to control the movement of the ghosts throughout the game. However, I am facing an issue with the current function that controls the ghost& ...

Updating the navigation with a dynamic CSS class using jQuery

Hey there, I'm on the lookout for a simple and discreet method to dynamically add an "active" class to a navigation menu based on the current directory. Unfortunately, I am unable to modify the body tag with a custom class, ruling out a pure CSS solut ...

What changes should I make to my code in order to incorporate an additional level of submenu to my CSS-based dropdown menu?

Hello and thank you for your help, I currently have some CSS code that is working well for 3 levels of dropdown menus, but I am now in need of it to also support a 4th level. When I try to add the extra level by copying the 3rd level code and making adjus ...

The resizable function in jQuery is not functioning as expected

Within my project, the initial page includes the following code snippet: html <div id="box"> <div id="header"> <span id="title"></span> <span id="button">X</span> </div> <div id="text"> </div> ...

Infinite scrolling dropdown feature in the <Select> widget

I currently have around 20 typical HTML select widgets on my page. Each of these selects contains a large number of elements, for example: <select> <option>1</option> ... <option>3000</option> </select> As ...

Align breadcrumbs to the left in Bootstrap 4 while keeping the text aligned to the right

I am striving to achieve something similar to this: Example Currently, I have the following setup: <nav aria-label="breadcrumb"> <ol class="breadcrumb"> <li class="breadcrumb-item>Home </li> <li class="breadcrumb-item>Level ...

Troubleshooting the Compatibility Issue Between Wordpress Theme and SSL

My personal blog is functional when accessed via http, but encounters issues with loading CSS code when accessed via https. Any suggestions on how to resolve this? The SSL certification is through the Let's Encrypt program. Website: Broken Link: ...

Tips for making a 2D grid on a webpage

Is there a way to implement a 10x10 grid on a webpage where users can click anywhere on the grid and have their (x, y) position recorded with one decimal place accuracy, covering values between 0.0-10.0 or 0.1-9.9? Appreciate any guidance! ...

What could be causing this navigation item to vanish in Firefox?

Check out this link: http://jsfiddle.net/Nvntm/1/ In Firefox and some other browsers, the "Support" navigation item disappears while the other three remain. Why does this happen? Is there something incorrect in my code? ...

Creating a CSS container with a uniquely shaped rounded triangle/arrow design

I would like to create a container background with a rounded arrow design. The arrow should always stretch to the full width of the container and be able to adjust its height dynamically (I can use JavaScript if needed to adjust the height). Here's a ...

Issues encountered with the functionality of face-api and tensorflow.js within the browser

I've been trying to execute this example in the browser Check out the example here Specifically looking at this code snippet <!DOCTYPE html> <html> ... (Contents of the code snippet) ... </body> </html> Unfortunately, I&apos ...

Guide on changing JSON to HTML with a Groovy script

Looking to convert JSON to HTML using Groovy instead of Python due to running in Jenkins. Need help with Groovy code for the conversion. The JSON data : [ { "kubernetes.pod.name": "sds-endpoints-6-hn0fe2l", "container.id": "d19e001824978", "m ...

Adjust the display from none to block when the parent element is set to flex

JSFiddle Demo Issue with - .popupcard_first:hover .popupcard_first { display: block; } I am trying to link the :hover effect to the first card only, but the only way I could make it work is by changing .popupcard... to .featured_cards:hover .po ...

Fundamentals of object property in jQuery and Javascript

Currently working on creating a new property named startPosition and setting the top property to equal the button's current top value in CSS. Below is the jQuery code snippet: var morphObject = { button: $('button.morphButton'), c ...

Ways to create a shorter upper line compared to the lower line (inside a div)

I'm dealing with an unordered list that has 3 list items, each represented by a green box containing an image and 3 divs (title, location, price). My main focus is on the title div of each box. If the title is long enough to span 2 lines, I need the ...