How can I adjust the height of a specific box in a CSS grid without impacting the layout of the surrounding boxes?

I am currently in the process of creating a layout that mirrors the one shown in the attached screenshot. To accomplish this, I have chosen to utilize CSS Grid for its simplicity and adaptability.

However, I have come across a difficulty. I desire for each box to automatically adjust its height based on its content, without impacting the heights of other boxes within the same row. Presently, when one box increases in height due to its content, it alters the heights of all other boxes in the row.

Is there a method to achieve the desired behavior without compromising the responsiveness of the layout? If CSS Grid cannot fulfill this, would you suggest an alternative approach that aligns better with my requirements?

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  background-color: blueviolet;
  padding: 5%;
}

.parent-container {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  grid-gap: 2rem;
}

.box {
  background-color: aqua;
  min-height: 300px;
}
<div class="parent-container">
  <div class="box">**Lorem ipsum 200**
  </div>
  <div class="box">Item 2</div>
  <div class="box">Item 3</div>
  <div class="box">Item 4</div>
  <div class="box">Item 5</div>
  <div class="box">Item 6</div>
  <div class="box">Item 7</div>
  <div class="box">Item 8</div>
  <div class="box">Item 9</div>
  <div class="box">Item 10</div>
  <div class="box">Item 11</div>
  <div class="box">Item 12</div>
  <div class="box">Item 13</div>
  <div class="box">Item 14</div>
  <div class="box">Item 15</div>
  <div class="box">Item 16</div>
  <div class="box">Item 17</div>
  <div class="box">Item 18</div>
  <div class="box">Item 19</div>
  <div class="box">Item 20</div>
</div>

Answer №1

It's simple to achieve this effect. Just apply the align-items property to the .parent-container class, and the height of the box will automatically adjust based on the content inside.

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    background-color: blueviolet;
    padding: 5%;
}

.parent-container {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    grid-gap: 2rem;
    align-items: flex-start;
}

.box {
    background-color: aqua;
    min-height: 200px; 
}
<div class="parent-container">
        <div class="box">**Lorem ipsum 200** it is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
        </div>
        <div class="box">Item 2</div>
        <div class="box">Item 3</div>
        <div class="box">Item 4</div>
        <div class="box">Item 5</div>
        <div class="box">Item 6</div>
        <div class="box">Item 7</div>
        <div class="box">Item 8</div>
        <div class="box">Item 9</div>
        <div class="box">Item 10</div>
        <div class="box">Item 11</div>
        <div class="box">Item 12</div>
        <div class="box">Item 13</div>
        <div class="box">Item 14</div>
        <div class="box">Item 15</div>
        <div class="box">Item 16</div>
        <div class="box">Item 17</div>
        <div class="box">Item 18</div>
        <div class="box">Item 19</div>
        <div class="box">Item 20</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

The dropdown submenu in Bootstrap 3 remains persistently open

I'm currently working on creating a submenu dropdown and following this reference link: https://www.w3schools.com/bootstrap/tryit.asp?filename=trybs_ref_js_dropdown_events2&stacked=h However, I am facing an issue where all my submenus are always ...

Modify the text or background shade of the Bootstrap date picker

I have successfully integrated the bootstrap date picker (view figure below) and it is functioning as expected. The code appears like this: <div class="input-group date"> <input name="departure" type="text" class="form-control" id="departure" ...

Place a circular grid at the center of a webpage

Is there a way to align this grid in the center of a webpage? It appears skewed on mobile view but not on larger screens. The issue seems to be primarily with mobile devices. For instance, when viewed on a mobile device, the circles are positioned to the r ...

"Choose" with the icon permanently attached

I am looking to customize a select element to choose a month. I want the dropdown list to display only the name of the months (e.g., January, February). However, in the selected value box of the select element, I would like to show a FontAwesome icon repre ...

Guide to customizing color themes using Twitter Bootstrap 3

Recently, I dove into the world of Twitter Bootstrap 3 without any prior experience with earlier versions. It's been a learning curve, but I managed to set up a WordPress theme using it as the framework. All my elements are in place, but they all see ...

reduce input to 2 characters using jQuery

Is there a way to trim the input text down to just the first two characters when a button is clicked? For example, if I enter "BT2J43" into the input field, can it be automatically shortened to "BT" upon clicking the button? I'm new to learning jQue ...

Step-by-step guide on creating a clickable button that triggers the appearance of a block showcasing a dimmed photo upon activation

Is there a way to create a button that triggers a pop-up block featuring a darkened photo when clicked? ...

Embeddable javascript code can be enhanced by incorporating references into the header

I have developed a basic calculator application using HTML, JavaScript, JQuery, and CSS. My intention is to allow others to embed this calculator app on their own web pages by utilizing a file named generate.js. Within this file, there are several documen ...

Issues with screen scrolling becoming stuck in React and Material UI

Currently facing an unusual issue where scrolling on my mobile website becomes sticky, with intermittent responsiveness to touch. Struggling to identify the root cause as there are no console errors detected. Provided below is a snippet of code for a subse ...

Navigate to the first item on the menu in the Chrome browser

Why does the First Menu Item (.chosen) jump in Chrome Browser when refreshed? It seems like it's changing the Padding-Right and Padding-Bottom. Please review the code and help me solve this irritating issue. It works fine in Firefox. <body> ...

I am having trouble styling a pre-existing class in bootstrap using my custom CSS file

html <section id="title"> <div class="container-fluid"> <!-- Nav Bar --> <nav class="navbar navbar-expand-lg navbar-dark"> <a class=" navbar-brand">tindog</a> ...

How to modify the floating label color using Bootstrap 5

I'm currently utilizing bootstrap 5 to craft a form featuring floating input functionality. Below is an example of the input I am utilizing: <div class="form-floating mx-auto mb-3 w-25"> <input type="text" class=" ...

The collapse button in Bootstrap 3.3 and jQuery 1.1 appears to be visible but unresponsive when clicked

This code isn't functioning properly as it displays the three horizontal bar button, but nothing happens when clicked. bootstrap-3.3 jquery-1.1 Toggle navigation CopyHere <div class="collap ...

Click to enlarge the text on button press

Recently, I've been working on a project involving a table that has a button at the end. My goal is to double the text size of the rows in the table when the button is clicked. As someone who is new to JQuery, this question may sound basic but any hel ...

The chosen color for the link button in CSS

PHP CODE : $getOID=$_GET["id"]; for($i=$getOID-($getOID-1); $i<=5;$i++){ echo '<a class="current" href="postlogin.php?id='.$i.'">'.$i.'</a>'; } CSS STYLING FOR BUTTONS : .pagging { height:20px; ...

What are alternative ways to divide CSS without relying on CSS-in-JS and CSS modules?

Recently, I transitioned to next.js and I'm eager to develop an application with CSS styles without resorting to css-in-js or inline styling. In my exploration, I've come across two potential options: using CSS modules or importing global styles ...

"Clicking on the radio button does not activate when placed within an <a> tag

Why is it that when the text is clicked, the radio button is checked, and when the little green patch is clicked, the frame is working, but they both don't work at the same time? Any assistance in resolving this issue would be greatly appreciated. ...

What method does nosotroshq.com use to achieve the navigation effect when hovering over a menu item?

Seeking advice from skilled individuals familiar with JQUERY: Can anyone explain the technique used by nosotroshq.com in their top navigation bar? Specifically, how do they create the slow animation when hovering over "ABOUT US"? Any insights would be ap ...

Unable to Apply Margin to Element within ASP.Net View Code Section

I've exhausted all my ideas and even searched Google without finding a solution. <div class="col-md-12"> <div class="card-header text-white" style="background-color:cornflowerblue"> <div class=& ...

Menu options are unresponsive to clicks in the dropdown

Need help fixing my dropdown menu issue. Whenever I hover over the dropdown, it disappears before I can click on any items. Here is a snippet of the code causing the problem: #navContainer { margin: 0; padding: 0; padding-top: 17px; width: 220 ...