Tips for aligning elements of varying sizes seamlessly with CSS grid?

I am currently working on creating a CSS grid layout. The issue I'm facing is that the first element has a fixed height, causing the entire first row to have the same height. I want the elements in the second row to move up and fill the empty space in the first row. Is there a way to achieve this using CSS grid?https://i.sstatic.net/GRuv9.png

The webpage where I plan to implement this grid will have elements that vary in size based on user input.

I've experimented with grid-auto-columns, grid-auto-rows, and grid-auto-flow: dense; but so far, I haven't been able to achieve the desired outcome. Any suggestions would be greatly appreciated.

.container {
  display: grid;
  width: 800px;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(2, 1fr);
  grid-gap: 1rem;
}


/* OTHER STYLES */

body {
  background-color: #3b404e;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

.item {
  height: 100px;
  background-color: #1EAAFC;
  background-image: linear-gradient(130deg, #6C52D9 0%, #1EAAFC 85%, #3EDFD7 100%);
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.19), 0 6px 6px rgba(0, 0, 0, 0.23);
  color: #fff;
  border-radius: 4px;
  border: 6px solid #171717;
}

.item1 {
  height: 250px;
}
<div class="container">
  <div class="item item1"></div>
  <div class="item item2"></div>
  <div class="item item3"></div>
  <div class="item item4"></div>
  <div class="item item5"></div>
</div>

Check out my practice on Codepen: Codepen Link Here

Answer №1

To keep things simple, just include the following code:

.primary {
  grid-row: span 2;
}

However, I opted to assign a CSS class to the specific element you want to emphasize. To achieve this:

.container {
  display: grid;
  width: 800px;
  grid-template-columns: repeat(3, 1fr);

  grid-template-rows: repeat(2, 1fr);
  grid-gap: 1rem;
}


/* OTHER STYLES */

body {
  background-color: #3b404e;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

.item {
  height: 100px;
  background-color: #1EAAFC;
  background-image: linear-gradient(130deg, #6C52D9 0%, #1EAAFC 85%, #3EDFD7 100%);
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.19), 0 6px 6px rgba(0, 0, 0, 0.23);
  color: #fff;
  border-radius: 4px;
  border: 6px solid #171717;
}

.item1 {
  height: 250px;
}

.primary {
  /* This expands the .primary element(s) over
     two of the grid-row tracks: */
  grid-row: span 2;
}
<div class="container">
  <div class="item item1 primary"></div>
  <div class="item item2"></div>
  <div class="item item3"></div>
  <div class="item item4"></div>
  <div class="item item5"></div>
</div>

Check out this JS Fiddle demo.

You can also achieve the same result without adding a class name by specifying an :nth-child() element:

/* or .container > div:first-child: */
.container > div:nth-child(1) {
  grid-row: span 2;
}

.container {
  display: grid;
  width: 800px;
  grid-template-columns: repeat(3, 1fr);

  grid-template-rows: repeat(2, 1fr);
  grid-gap: 1rem;
}


/* OTHER STYLES */

body {
  background-color: #3b404e;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

.item {
  height: 100px;
  background-color: #1EAAFC;
  background-image: linear-gradient(130deg, #6C52D9 0%, #1EAAFC 85%, #3EDFD7 100%);
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.19), 0 6px 6px rgba(0, 0, 0, 0.23);
  color: #fff;
  border-radius: 4px;
  border: 6px solid #171717;
}

.item1 {
  height: 250px;
}

/* or .container > div:first-child: */
.container > div:nth-child(1) {
  grid-row: span 2;
}
<div class="container">
  <div class="item item1 primary"></div>
  <div class="item item2"></div>
  <div class="item item3"></div>
  <div class="item item4"></div>
  <div class="item item5"></div>
</div>

Another JS Fiddle demonstration.

For more information, refer to:

Answer №2

One limitation is trying to achieve the layout with only 2 rows, but using Grid provides a structured solution.

With 3 rows, it becomes achievable.

.container {
  display: grid;
  width: 800px;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  grid-gap: 1rem;
}


/* OTHER STYLES */

body {
  background-color: #3b404e;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

.item {
  height: 100px;
  background-color: #1EAAFC;
  background-image: linear-gradient(130deg, #6C52D9 0%, #1EAAFC 85%, #3EDFD7 100%);
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.19), 0 6px 6px rgba(0, 0, 0, 0.23);
  color: #fff;
  border-radius: 4px;
  border: 6px solid #171717;
}

.item1 {
  height: 250px;
  grid-area: 1 / 1 / 3 / 2;
}

.item2 {
  grid-area: 3 / 1 / 4 / 2;
}

.item3 {
  grid-area: 1 / 2 / 2 / 3;
}

.item4 {
  grid-area: 1 / 3 / 2 / 4;
}

.item5 {
  height: 250px;
  grid-area: 2 / 2 / 4 / 3;
}

.item6 {
  height: 250px;
  grid-area: 2 / 3 / 4 / 4;
}
<div class="container">
  <div class="item item1"></div>
  <div class="item item2"></div>
  <div class="item item3"></div>
  <div class="item item4"></div>
  <div class="item item5"></div>
  <div class="item item6"></div>
</div>

Utilizing grid-area position for each item is one approach to achieving this layout.

Alternatively, achieving the same layout can also be done using span.

Answer №3

One possible solution to tackle this problem...

.container {
  display: grid;
  width: 800px;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(2, 1fr);
  grid-gap: 1rem;
}


/* OTHER STYLES */

body {
  background-color: #3b404e;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
    box-sizing:border-box;
}

.item {
  height: 100px;
  background-color: #1EAAFC;
  background-image: linear-gradient(130deg, #6C52D9 0%, #1EAAFC 85%, #3EDFD7 100%);
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.19), 0 6px 6px rgba(0, 0, 0, 0.23);
  color: #fff;
  border-radius: 4px;
  border: 6px solid #171717;
}

.item1 {
    grid-column-start: 1;
    grid-column-end: 2;
    grid-row-start: 1;
    grid-row-end: 3;
    height: 250px;
}
.item4 {
    grid-column-start: 2;
    grid-column-end: 3;
    grid-row-start: 2;
    grid-row-end: 3;
        
}
.item5 {
    grid-column-start: 3;
    grid-column-end: 4;
    grid-row-start: 2;
    grid-row-end: 3;
}
  <div class="container">
  <div class="item item1">item 1</div>
  <div class="item item2">item 2</div>
  <div class="item item3">item 3</div>
  <div class="item item4">item 4</div>
  <div class="item item5">item 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

Guide on creating a toggle effect for a div with querySelector and addEventListener

I utilized the email and password divs provided by Bootstrap. The CSS for the id dropdownlogin includes display: none; in this post, I hope that I have shared adequate information. <script> document.addEventListener('DOMContentLoaded', ...

Here is a helpful guide on updating dropdown values in real time by retrieving data from an SQL database

This feature allows users to select a package category from a dropdown menu. For example, selecting "Unifi" will display only Unifi packages, while selecting "Streamyx" will show only Streamyx packages. However, if I first select Unifi and then change to S ...

Some elements in the DOM appear to not be rendering even though they are present in the HTML source code

As a newcomer to web development, I have stumbled upon a perplexing issue. In my usage of d3.js (specifically the script found here), I have been attempting to incorporate additional features. I modified my JavaScript code to include a simple <p> ele ...

What is the best way to unselect the "all" selector if one of the inputs is no longer selected?

I am facing an issue with a search filter functionality. When all filters are selected and then deselected individually or together, the "all" button remains selected. I need help in ensuring that when any filter is deselected, the "all" button also gets d ...

The footer element is missing from the body section of the HTML code, causing the box shadow to not encompass the footer as intended

Having a problem in CSS where the footer is not appearing within the body element in the HTML code. The footer is placed between the body tags, but the box shadow defined for the body is not being applied to the footer. The code snippet below shows the CSS ...

CSS files imported from the node modules are mysteriously disappearing

Encountered a unique issue while attempting to import a CSS file from a node module. In the App.tsx file, the following imports were made: import './App.css'; // successful import '@foo/my-dependency/dist/foo.css' ...

JavaScript: displaying the value of a checked checkbox

Here is the HTML code snippet I am working with: <form id="my form"> <input type="checkbox" name="interest" value="swim"/> <input type="checkbox" name="interest" value="baseball"/> <input type="checkbox" name="interest" value="basketb ...

What is the best way to showcase the following values using Vue.js?

{"status":true,"data":[{"ref_id":"22","agent_id":"68","p_id":"84","description":"i am interested"},{"ref_id":"24","agent_id":"68","p_id":"84","description":"For more information about Bootstrap and Bootstrap Glyphicons, visit our Bootstrap Tutorial.For mor ...

How to manage the three states (InProgress, Success, Error) of a dropdown dynamically in AngularJS with ng-show/hide?

Currently, I have implemented a bootstrap dropdown that loads a list of countries by making an external API call. My goal is to display three different states of behavior (progress, success, error) in the dropdown using ng-show/hide. However, I am unsure h ...

Toggle between bold and original font styles with Javascript buttons

I am looking to create a button that toggles the text in a text area between bold and its previous state. This button should be able to switch back and forth with one click. function toggleTextBold() { var isBold = false; if (isBold) { // Code t ...

Guide to smoothly scroll to the top of an element containing collapsible panels using Jquery

I've encountered an issue with a series of divs that are set to toggle the state of a collapsible div upon clicking (similar to an accordion widget). The functionality works as intended, but I'm facing a challenge - I want the page to scroll to t ...

JavaScript: When setting focus on a DOM element, the document.activeElement is not automatically updated

I am facing an issue where I need to manually set focus on two buttons consecutively. These buttons are jQuery-Objects stored in an array named pMenus. Below is the code snippet: function OpenSubMenus(pMenus) { pMenus[pMenus.length - 1].get(0).focus() ...

In search of a render function that can effectively apply styles to HTML strings using React JS

I have been struggling to convert the result field value, including HTML attributes string, into respective styles for one column in my antd table. Below is the string I am trying to convert: The exhaust air requirements for fume hoods will be based on m ...

What is the best way to include an ellipsis after a couple of lines of

Is there a way to add an ellipsis after two lines of text? I've been able to successfully implement the ellipsis in one line, but what if the text spans across two lines? And what if it's even longer than that — can we show an ellipsis after th ...

Trouble with retrieving data from localStorage

On my webpage, I have set up multiple input fields where users can enter data. When they click a button, the data from these inputs is stored inside <span>...</span> elements (the intention being that the text remains visible in these <span& ...

Navigate to the end of a container

Is there a method to automatically scroll to the bottom of a div when the page is loaded? I have attempted several solutions without success. If you have any insights, please share them. Thank you! ...

Using ASP.NET Server Controls to Toggle Textbox Visibility Based on Dropdown Selection

What is the optimal method for displaying or hiding a textbox or an entire div section based on a user's selection from a dropdown menu? I am uncertain if this can be achieved with server controls, so would it require using standard client-side HTML c ...

The Chakra Card component is being displayed in a vertical layout instead of the usual horizontal

I'm currently working on rendering a card and organizing each card in rows of three, with the added flexibility of altering this number responsively. I'm struggling to identify which parts of the code are conflicting with SimpleGrid, and have ex ...

Tips on how to maintain the underline when text is split into two sections

Revised This question is distinct from the duplicate one. I am specifically addressing the issue of ensuring the underline continues until the end of the text, regardless of how many lines it spans. The challenge I am facing is with displaying the underl ...

Processing file to retrieve information (JavaScript)

As someone who is new to the world of JavaScript and website development, please forgive me if this question seems a bit basic. The concept is to have a popup message appear on every page with some information. The issue arises when wanting to change this ...