Vertically centering elements within columns using CSS Grid

I attempted to bring a unique design to life using CSS Grid. While initially exploring Isotope JS for the same layout, I encountered some difficulties. Eventually, I was able to achieve a close representation of the design through CSS grid.

There are a total of 7 dynamically generated items from a for loop. These items need to be arranged into 3 columns and 3 rows. The first two columns should contain two items each, while the last column should hold the remaining 3 items. Achieving the desired spacing in the first two columns proved challenging, especially with vertically aligning the items in the middle column.

For reference, here is an image depicting the layout I aim to implement: https://i.sstatic.net/CYIdT.png

Here is my current code attempt:

.pr-soft-masonry-layout {
  display: grid;
  grid-gap: 20px;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(2, 1fr);
  align-items: center;
}
{/* Additional CSS rules */}
{/* HTML structure */}

Answer №1

I totally agree with the input provided by @ralph.m that you should include more rows in your grid. I made use of ten rows in this code snippet, making sure each grid item spans at least three rows.

let s = ''
for (i = 0; i < 7; i++) {
  const n = Math.floor(Math.random() * 256);  
  s += `<div style="background: hsl(${n},50%,50%);">${i + 1}</div>`
}
document.querySelector('.d1').innerHTML = s
.d1 {
  color: white;
  display: grid;
  grid-template-columns: repeat(3, 10em);
  grid-template-rows: repeat(10, 1em);
  gap: 1em;
}
.d1>div {
  border-radius: 5px;
  padding: 1em;
  box-sizing: border-box;
}
.d1>div:nth-child(1) {
  grid-column: 1;
  grid-row: 2 / span 3;
}
.d1>div:nth-child(2) {
  grid-column: 1;
  grid-row: 5 / span 4;
  width: 7em;
  justify-self: end;
}
.d1>div:nth-child(3) {
  grid-column: 2;
  grid-row: 3 / span 3;
}
.d1>div:nth-child(4) {
  grid-column: 2;
  grid-row: 6 / span 4;
}
.d1>div:nth-child(5) {
  grid-column: 3;
  grid-row: 1 / span 3;
  width: 7em;
}
.d1>div:nth-child(6) {
  grid-column: 3;
  grid-row: 4 / span 4;
}
.d1>div:nth-child(7) {
  grid-column: 3;
  grid-row: 8 / span 3;
  width: 7em;
}
<div class="d1"></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

Nested lists with consistent height regardless of the quantity of items

I am currently working on a dropdown multicolumn menu that contains nested lists. My goal is to ensure that all three lists (or columns) have the same height, even if they contain a different number of items. Since the lists are dynamic and the number of i ...

Implementing dual backgrounds in CSS

I am trying to achieve a gradient background for li elements along with different image backgrounds for each individual li. Is there a way to accomplish this? List of items: <div id="right_navigation_container"> <ul> <li id ...

Narrowing the width of a line-broken <h1> heading

I have encountered a problem that can be best described with two images provided below. Let's focus on the relevant CSS snippet: h1 { display: inline-block; background-color: pink; } The first example shows an <h1> tag with a short ti ...

Exploring the depths of CakePHP 3's debug_kit in a subdirectory

After developing locally, I transferred everything to the server. The functionality is working fine, but I am having issues with the debugkit not displaying correctly. I am unable to access the js, css files, etc. All I see is an empty box with a placehol ...

Expandable sidebar using responsive Twitter Bootstrap

I am in search of a CSS solution to implement a button that can toggle a sidebar on and off using twitter bootstrap. Is there a specific term for those small icons seen on webpages that resemble pull tabs when the sidebar is closed and then move along wit ...

Apply a style to the div element that contains a TextInput component

As a beginner in the world of React and Material UI, I am currently working with Material UI version "1.0.0-beta.17", React 15.6.2, styled-components 2.0.0, and styled-components-breakpoint 1.0.1. Within a div element, I have two TextInput fields. const ...

CasperJs was unable to locate the CSS selector after transitioning from jade to pug, causing the test to fail

After transitioning my templates from jade to pug, all of my CasperJS tests started failing. The majority of the errors are related to the css selectors that cannot be found. Below is an example of the issue: CasperJS test: casper.thenOpen("http://local ...

Switching from 'click' to 'hover' will make the dropdown menu more accessible for keyboard users

I'm currently updating an existing dropdown menu that meets WCAG standards but I need to tweak the functionality so that it activates on hover instead of click. Below is the code snippet: $(document).ready(function() { $('.has-submenu'). ...

Ensuring text is perfectly centered within a label and creating a label that is entirely clickable

Struggling to style a file input button and encountering some issues with labels. Here is the button in question. Firstly, how can I center the text in the label? Secondly, how can I make the entire button clickable instead of just the upper half? Also, ...

``The background color will dynamically change according to the result of a function

My function named shift_color generates different color codes like "#FF5F74", "#5FFF66", and "#5F8AFF". I am looking to use this output to style a navigation menu background. I have tried the following code: .topnav { background-color: <?php echo shi ...

Ways to switch out the background image using jQuery

I am currently using jQuery to dynamically change the background image of a web page. Right now, I have implemented two separate buttons that toggle between Image A and Image B. By default, Image A is displayed on the page. My goal is to enhance this func ...

Elevating the table and row dimensions in Bootstrap

I'm currently working with Bootstrap 3 and I'm having trouble adjusting the height of my table and row. I've tried setting properties like line-height, but nothing seems to work. How can I increase the height? <style> div#descrip ...

Enhance Material UI BottomNavigationAction by adding a pseudo-element

Trying to enhance a selected item with an additional UI element using a pseudo-element. Specifically, looking to add a line above the menu item. https://i.stack.imgur.com/MZnmw.png The code I've implemented is not displaying the desired line: const ...

Creating a mobile-responsive Ignite gallery experience

I'm currently working on optimizing a website for mobile devices, but I'm having trouble getting an Ignite Image gallery to shrink properly. I've attempted adjusting the widths of all the container tags and the image itself to use max-width ...

CSS popup experiencing issues due to Iframe integration

As someone who is relatively new to web development and completely self-taught, I've encountered a challenge with an iframe on my page. I have a CSS and JavaScript popup that is triggered by jQuery. The desired behavior is that when a link is clicked, ...

The FAB button animation is causing delays in the transition process and is not functioning as originally anticipated

I am facing an issue with the FAB button and 3 Icons. The functionality is working fine on click for both show and hide actions, but the transition is too delayed. I want the icons to appear step by step, even though I have adjusted the transition delay se ...

"Enhance Your Website with Various Hover Effects Using CSS across Multiple Elements

Has anyone experienced trouble with making the hover effect work in my onHover class and applying a display effect to span.box? Any ideas on how to fix this? .onHover { display: block; width: 200px; height: 20px; background: green; } .onHover:ho ...

Creating a unique Bootstrap background design

I have recently started using bootstrap and I am facing an issue. I want to change the background of a container when hovering over it, but I am having trouble getting it right. HTML <div class="container bg-black h-25 w-25 box"></div&g ...

Creating two adjacent squares can be achieved by arranging columns and rows in a specific way

Looking to create a website with a gallery block but running into some issues. I only want to utilize Bootstrap along with my custom CSS for the finishing touches. The layout should consist of 2 squares next to each other, with the left square being 2 colu ...

Troubleshooting CSS issues for Compatibility View on Internet Explorer

I have been using CSS to create a dropdown navigation menu without the use of JavaScript. It seems to be working perfectly in most browsers, however, I am encountering some issues with certain versions of Internet Explorer. I've spent hours trying var ...