Delete the space between "Align-items" in CSS Grid

I am experimenting with CSS Grid to create a dynamic layout for a small test blog. However, I have encountered an issue related to positioning.

Since the length of the content in the articles is unknown due to it being a blog, using fixed positioning to adjust the layout is not feasible.

Here is my current layout:

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

I would like the layout to resemble this:

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

My code structure currently looks like this:

CSS

.articles {
    margin: 20px;
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    grid-gap: 1em;
}
article {
    align-self: start;
}

HTML

<div class="articles">
    <article>lorem ipsum...</article>
    <article>lorem ipsum...</article>
    <article>lorem ipsum...</article>
</div>

The align-self property seems to be functioning correctly by removing the empty space created by the larger (Second) paragraph. However, the third paragraph remains in its original position instead of moving up to fill the now blank space where align-self: stretch should have worked.

Any suggestions on how to resolve this issue would be greatly appreciated.

Answer №1

When it comes to laying out content like this, CSS columns might be a more suitable option compared to using CSS Grid or Flexbox:

body {
  background: #FFF;
}

.cards {
  -webkit-column-width: 300px;
  -moz-column-width: 300px;
  column-width: 300px;
  -webkit-column-gap: 30px;
  -moz-column-gap: 30px;
  column-gap: 30px;
  width: 90%;
  max-width: 1100px;
  margin: 30px auto;
}

.card {
  margin: 15px 0;
  padding: 15px;
  background: #EEE;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.25);
  display: inline-block;
  -webkit-column-break-inside: avoid;
  -moz-column-break-inside: avoid;
  column-break-inside: avoid;
}
<div class="cards">
  <div class="card">
    <h2>Card Title</h2>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi finibus odio in enim feugiat sagittis. Mauris ac nulla ac nulla volutpat scelerisque vel eu nisi. Curabitur vehicula vestibulum ligula eu condimentum.</p>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi finibus odio in enim feugiat sagittis. Mauris ac nulla ac nulla volutpat scelerisque vel eu nisi. Curabitur vehicula vestibulum ligula eu condimentum.</p>
  </div>
  <div class="card">
    <h2>Card Title</h2>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi finibus odio in enim feugiat sagittis.</p>
  </div>
  <div class="card">
    <h2>Card Title</h2>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi finibus odio in enim feugiat sagittis. Mauris ac nulla ac nulla volutpat scelerisque vel eu nisi. Curabitur vehicula vestibulum ligula eu condimentum.</p>
  </div>
  <div class="card">
    <h2>Card Title</h2>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi finibus odio in enim feugiat sagittis. Mauris ac nulla ac nulla volutpat scelerisque vel eu nisi. Curabitur vehicula vestibulum ligula eu condimentum.</p>
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi finibus odio in enim feugiat sagittis. Mauris ac nulla ac nulla volutpat scelerisque vel eu nisi. Curabitur vehicula vestibulum ligula eu condimentum.</p>
  </div>
</div>

Answer №2

Working with CSS Grid doesn't make it easy to achieve the desired result of having elements arranged strictly in columns. While flexbox might offer a solution, the true ideal setup would be a grid with two columns, each acting as an independent flexbox container.

The struggle arises due to the fact that CSS Grid inherently operates in both rows and columns, making purely column-based layouts challenging to accomplish without explicit row definitions. The addition of auto-rows or auto-columns can further complicate matters by introducing rigid spacing constraints after the first element is added.

CSS Grid's strength lies in its ability to handle 2D layouts compared to the 1D focus of flexbox and normal document flow. This means that article heights directly influence row heights within the grid, presenting obstacles for achieving distinctive column-only arrangements without predefined content heights.

To mimic such a layout effectively, one must assign fixed heights to articles to enable spanning across multiple rows of varying sizes. Despite potential workarounds suggested by others for using Columns, Flexbox emerges as a more stable and widely supported alternative, especially considering the limited browser compatibility of the former approach when paired with CSS Grid.

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

Upgrading Pagination from Bootstrap 3 to Bootstrap 4: A Step-by-

I am currently transferring an application from Bootstrap 3 where the previous pager had this appearance: https://i.sstatic.net/0gqIo.png Here is the code snippet for the old pager: <nav> <ul class="pager"> <li class="previous ...

The snap drag position resets to its original placement when dragged for a second time

I am working on creating a unique slider that allows users to click and drag a circle along a curved path. The functionality is almost there, but whenever I drag the circle to a certain position, it snaps back to its original location. var s=Snap(' ...

Is there a way to style alternate iframes using CSS?

I need to select every second iframe in the image provided. Is it possible to achieve this using nth-child? I have tried with the following code (unsuccessfully): #main iframe:nth-child(2n+2) ...

What could be causing the inaccuracies in my precise web design layout?

Struggling with modifying HTML using Javascript? Take a look at this example: https://jsfiddle.net/02mwyvyo/ The objective is to shift a specific element downwards on the page. This involves inserting a spacer div before the target element, with style att ...

What causes Windows 7 users to consider the VS Code website unsafe?

What causes Windows 7 users to view the VS Code website as unsafe? Visit the image link here ...

Safari does not support JavaScript, while Firefox and Chrome do

My form has validation using Parsley. The submit button uses JavaScript code to animate via CSS. This works well in Firefox and Chrome, but not in Safari (both desktop and iOS). Validation is successful, but the animation doesn't play. After some inv ...

The CSS Scroll-Snapping feature seems to be overlooking one specific element that it should be snapping to

Currently, this website is designed to work exclusively for mobile devices. When viewed in Chrome's mobile view using the inspector, everything appears to be functioning correctly. However, when accessed through Safari or on an actual mobile phone, th ...

Exploring the html element through Xpath

I am currently attempting to locate an HTML element using Selenium XPath and C#. Here is the code snippet of what I have experimented with: var element = Driver.FindElement(By.XPath("//img[text()='logo-home.png']/@href")); if (element.Display ...

Is there a way to vertically center the form in order to position the logon at the top center?

https://i.sstatic.net/8gdCo.png Looking to vertically center align the login form. I've tried various solutions from Stack Overflow, but I'm new to Bootstrap 4. The goal is to position the logo at the top center of the login form, hence why I wa ...

Creating a Distinctive HTML Structure with Div Percentage (HTML 4)

I am new to web design and have a unique approach; I believe in the importance of flexibility. It puzzles me why pixels (px) are commonly used instead of percentages, maybe my perspective is misunderstood. I've been on the hunt for a basic HTML layo ...

Vertical Graph - Utilizing HTML and Cascading Style Sheets

My goal is to create a vertical bar chart that looks like this: However, the result I am currently getting is different: At the bottom of my vertical bar chart, both lines are connected together and I am trying to figure out how to separate them. I am a ...

Reaching for a particular design, yet ending up with something unexpected

I need some help achieving a specific layout for my website. Every time I try to do it, everything gets all jumbled up. I want it to look like the example image I have attached. Can anyone provide assistance? body { margin: 0p ...

Gather information from HTML elements that are updated with text dynamically via AJAX calls using Scrapy

My goal is to extract information about mobile phones listed on the 'amazon.in' website from the following link: here using scrapy. Below is the code I am using: from scrapy.spiders import CrawlSpider, Rule from scrapy.linkextractors import Lin ...

In what way is my identified ID behaving as an array?

Recently, I set up an edit-form on my project. The form is designed to update the values in the database based on a specific caseid. However, when I attempt to query the $caseid, I encounter an error that says: Error: conversion from array to string. ...

Struggling with displaying a CSS dropdown menu below its parent element?

I need help with my menu structure. <nav id="nav"> <ul> <li>Home</li> <li>Menu1 <ul> <li>Sub1</li> <li>Sub2</li> </ul> </li> <li>Menu2 <ul> & ...

Hidden submission button on form

I'm working on a form that is being validated with specific code limitations. For example, the username field must only contain alphabet characters. The validation function returns either true or false. function isAlphapet() { var alphaExp = /^[a-z ...

Delayed response of text effects in JQuery on page load

Within my rails app, I have the following code snippet: window.onload = -> $("#mycontainer").typewriter() $("#div1").fadeIn("slow") This code snippet interacts with the following block of content: <blockquote class="pull-left"> < ...

Loading New Page When Iframe is Loaded

I am currently working with an iframe that reflects the appscript I have created. Is there a way to automatically redirect the user to a different page when they change a link or submit a form within the iframe? <div class="embed-responsive em ...

What is the best way to remove a logo from a wordpress website?

I've come across a theme that I find quite appealing, but the "designed by" logo is positioned in a rather bothersome place - right in the center of the page on the left side. Is there a way for me to remove it from there and relocate it to the footer ...

Expandable Hover Navigation Bar with jQuery for Full Page Width Dimension

I currently have a vertical menu on my website and I am looking to implement a dropdown system for some of the links. I came across an example on the internet that inspired me: http://jsfiddle.net/4jxph/3018/ However, I specifically want a full-width subme ...