Reorganizing CSS Grid: Moving a column-aligned item to a new row

I am attempting to create a grid that displays the position, name, evolution, and score of users. The challenge is, this list is constantly changing, so I cannot predict how many rows it will have in the end. In addition, I am not allowed to alter the order of elements in the HTML.

Upon inspecting the snippet below, I noticed that the .name element moves to the next row instead of staying in the current one when placed by column number. This behavior has left me puzzled...

body {
  background-color: darkslategrey;
  color: white;
}

.container {
  display: grid;
  grid-template-columns: repeat(3, auto);
  gap: 10px;
  text-align: center;
}

.name,
.evolution,
.score {
  border-top: 1px solid white;
  border-bottom: 1px solid white;
}

.position {
  grid-column: 1 / span 3;
}

.name {
  grid-column: 1;
}

.evolution {
  grid-column: 2;
}

.score {
  grid-column: 3;
}
<div class="container">
  <div class="position">1st</div>
  <div class="evolution">+2</div>
  <div class="score">85pts</div>
  <div class="name">Jack</div>
  <div class="position">2nd</div>
  <div class="evolution">+3</div>
  <div class="score">82pts</div>
  <div class="name">Kate</div>
  <div class="position">3rd</div>
  <div class="evolution">-2</div>
  <div class="score">80pts</div>
  <div class="name">Sawyer</div>
  <!-- and many more, this list is dynamic -->
</div>

Any insights on what might be causing this issue?

Answer №1

The grid is populating based on the order of HTML elements provided.

If you want it to automatically fill any available space, you can use the grid-auto-flow property in CSS. This will help in filling up empty cells like placing Jack in a vacant cell in row two.

body {
  background-color: darkslategrey;
  color: white;
}

.container {
  display: grid;
  grid-template-columns: repeat(3, auto);
  grid-auto-flow: dense;
  gap: 10px;
  text-align: center;
}

.name,
.evolution,
.score {
  border-top: 1px solid white;
  border-bottom: 1px solid white;
}

.position {
  grid-column: 1 / span 3;
}

.name {
  grid-column: 1;
}

.evolution {
  grid-column: 2;
}

.score {
  grid-column: 3;
}
<div class="container">
  <div class="position">1st</div>
  <div class="evolution">+2</div>
  <div class="score">85pts</div>
  <div class="name">Jack</div>
  <div class="position">2nd</div>
  <div class="evolution">+3</div>
  <div class="score">82pts</div>
  <div class="name">Kate</div>
  <div class="position">3rd</div>
  <div class="evolution">-2</div>
  <div class="score">80pts</div>
  <div class="name">Sawyer</div>
  <!-- and many more, this list is dynamic -->
</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

Top Bootstrap dropdown menu overlaid with Leaflet zoom buttons

I'm encountering an issue depicted in the screenshot below. Is this related to bootstrap or leaflet? How can I adjust the position/style of the dropdown menu so that it appears above all other elements? https://i.sstatic.net/cQhN5.png ...

Google Chrome Failing to Display Fonts Correctly

There's a strange issue I'm facing with Chrome. I've integrated Bootstrap with glyphicons and another font (BYekan) using @font-face. Sometimes the glyphicons and BYekan fonts display correctly, but other times a square appears in place of ...

Design cards in a particular sequence using either bootstrap or CSS

I am currently developing a blog website and I need assistance with arranging the cards in this specific order: https://i.sstatic.net/Ffpcb.png Despite my efforts using Bootstrap, I am unable to achieve the desired layout. Here is the code I have so far: ...

Activate dropdown menu link when tapped twice on tablet devices

I am trying to implement a dropdown navigation link feature on tablets where the child menu div drops down only on the second click. This functionality already works on iPads, but on Android and Windows tablets, the link is triggered at the same time as th ...

Can CSS actually generate accurate inches?

Can you accurately set the width of an element, like a div, in inches and expect it to maintain that width across different devices? Or will it simply scale at a ratio like 1in = 96px? Edit: Try using CSS to set an element's width in inches and then ...

The asp.net bundle.config file isn't updating to include the latest css files

I'm experiencing an issue with my bundle.config file. I've added some CSS files with the correct path and even rebuilt the solution, but none of the new files are showing up on the page. <?xml version="1.0" encoding="utf-8" ?> <bundles ...

Styling an input text using CSS and Jquery without relying on a parent

Styling CSS input text without a parent div Hello everyone, I am looking to remove the <div id="input_container"> from the code below. Is it possible to achieve the same result without that surrounding div? Check out the jsfiddle: http://jsfiddle.n ...

In Safari, my layout is disrupted by the mere mention of the word 'City'

There's something strange happening in Safari that I can't quite figure out. Here's how my layout looks in Chrome: https://i.sstatic.net/lPhnP.png But when viewed in Safari, it looks like this: https://i.sstatic.net/IMnQb.png For some r ...

Error: The value being evaluated in document.getElementById(x).style is not an object and is not supported

Desired Outcome for my Javascript: I am working with a div that includes an "onmouseover='getPosition(x)'" attribute which can be dynamically added and removed through my javascript by clicking a specific button. The function 'getPosition() ...

Vuetify's scrolling list feature allows for smooth navigation through a list

Check out my Vuetify code snippet that utilizes a list: <v-list> <v-list-tile v-for="user in users" :key="user.id" avatar @click="" > <v-list-tile-content> < ...

What is the best way to retrieve values from similar class/properties using Selenium in C#?

Currently, I am in the process of automating tests to compare expected and actual endorsements on a summary page. I am facing a challenge in reading all the endorsements values displayed on the summary page. The number of endorsements can vary from 2 to 5 ...

Creating a responsive layout with Bootstrap4 to show two rows of three columns on smaller screens

I am looking to design a pagination feature that has the following appearance: On a wide screen: | | | [<<] Page # 1 of 6 [>>] | | ...

Difficulty with setting a border for text spanning multiple lines

I am trying to style a header text with a border around it. The issue arises when the larger text spans over two lines, causing the border to break in between. In an attempt to fix this, I applied the CSS property box-decoration-break: clone;. However, thi ...

How to collapse or expand a DIV element just like the feature showcased on this site

The way the DIVs fold and unfold from top to bottom then left to right when users click on the menu items (about, services, my tools, etc) on this website is really impressive. It seems like it was created by a skilled developer. Do you have any insights ...

powerful enhancer separates the heading into a layout

I have an HTML div element that just isn't behaving right when I resize the screen. The title pretty much sums it up: <div class='serveMessage'><strong>Fun fact:</strong>&nbsp;over 1,259,468 American students fail ev ...

Having trouble with CSS text-align right not functioning properly? Here's how you can fix it

<div style="text-align: left;width: 21cm;"> <h4 style="text-align: center;font-weight: bold;font-size: 20px;margin: 0">Tax Invoice(Center) <span style="text-align: right;"> For Customer(Right)</span></h4> </div> I n ...

I'm puzzled as to why the banner text for my three images in the slider is only displaying on one of the images, all crammed together

Currently, I am working on an ecommerce project with Next.js. One of the challenges I faced was while setting up my banner page that includes a react-slick slider for images. Initially, when I added just one image, I noticed multiple renderings of it, but ...

Using the CSS overflow scroll property with Bootstrap

I am attempting to resize Section A in Column 1 to match the height of the row. 1 row, 2 columns: +---------------------+---------------------+ | Column 1 | Column 2 | | | | | +--------------- ...

Is it possible to transform the Bootstrap5 sidebar, currently positioned above the block content, into a sticky element on

After creating a Django blog using bootstrap5 and following guidance from the python crash-course book, I encountered an issue with my sidebar placement. Despite trying various methods, I struggled to "override" my blocks in order to keep my sidebar fixed ...