What can I do to ensure that the cells within a CSS grid row have consistent heights across various columns?

Seeking for a solution to this issue seems futile - every response I find is about a slightly different issue, where all the rows in a single column end up being the same height as the tallest cell in that column. The suggestion is to add grid-auto-rows: 1fr to the grid container. While that works, it is not exactly what I am trying to address.

Let me provide a code snippet to illustrate my point. I aim for the "aaa" cell to match the height of the "BBB" cell, and for the "ccc" cell to match the height of the "DDD" cell, and so on.

.grid-container {
  display: grid;
  grid-template-columns: 1fr 2fr;
  grid-auto-rows: 1fr;
  margin: 8px 0;
  width: 600px;
  gap: 2px;
  align-items: center;
}

.grid-item {
  border: 1px solid grey;
  background-color: #fea;
}

.bigger-text {
  font-size: 21px;
  line-height: 36px;
}
<div class="grid-container">
  <div class="grid-item">aaa</div>
  <div class="grid-item bigger-text">BBB</div>
  <div class="grid-item">ccc</div>
  <div class="grid-item bigger-text">DDD</div>
  <div class="grid-item">eee</div>
  <div class="grid-item bigger-text">FFF</div>
</div>

Answer №1

The problem arises specifically from using the property align-items: center, which adjusts the height according to the content and then centers the item vertically.

If necessary, you can utilize flexbox to center the text:

.grid-container {
  display: grid;
  grid-template-columns: 1fr 2fr;
  margin: 8px 0;
  width: 600px;
  gap: 2px;
}

.grid-item {
  border: 1px solid grey;
  background-color: #fea;
  /* centers text vertically */
  display: flex;
  align-items: center;
}

.bigger-text {
  font-size: 21px;
  line-height: 36px;
}
<div class="grid-container">
  <div class="grid-item">aaa</div>
  <div class="grid-item bigger-text">BBB</div>
  <div class="grid-item">ccc</div>
  <div class="grid-item bigger-text">DDD</div>
  <div class="grid-item">eee</div>
  <div class="grid-item bigger-text">FFF</div>
</div>

Answer №2

To ensure consistent cell height across columns in a CSS grid row, you can use the align-self property to stretch each item in the row to match the height of the tallest item. Check out the updated code snippet below:

.grid-container {
  display: grid;
  grid-template-columns: 1fr 2fr;
  margin: 8px 0;
  width: 600px;
  gap: 2px;
}

.grid-item {
  border: 1px solid grey;
  background-color: #fea;
  align-self: stretch;
}

.bigger-text {
  font-size: 21px;
  line-height: 36px;
}

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

I'm puzzled as to why a scroll bar materializes

I recently encountered an interesting issue while working on my responsive web design project. Upon resizing the browser to a width of 615px or less, a horizontal scroll bar unexpectedly appeared. It's puzzling as to which element is causing this prob ...

The application encountered an exception and has ceased to respond, displaying a custom error page

I have a .NET application running on IIS 7 that is accessed from IE7. When a specific exception occurs, the page is redirected to a plain .htm page with a back button that should take the user back to the previous page. This exception handling is implement ...

Angularjs drop-down menu changes images with animation

<body ng-controller="myCtrl"> <input type="checkbox" ng-model="loaded"/> <select ng-model="list"> <option ng-repeat="option in data.availableOptions" value="{{option.name}}">{{option.id}}</option> </select> {{list}} &l ...

Change the input field to uppercase using JavaScript but do not use inline JavaScript

Hey there! I have a basic script set up (see below) with an input field allowing users to enter a query. This query is then sent to a socrata webservice to request specific data, which is displayed in an alert box. So far, everything works smoothly. var l ...

The orientation of Bootstrap flex - horizontal row or vertical column -

Bootstrap operates based on the class assigned to the parent element for flex row/column behavior. .bd-highlight { background-color: rgba(86, 61, 124, .15); border: 1px solid rgba(86, 61, 124, .15); } <link href="https://stackpath.bootstrapcdn.co ...

Is there a way to align this in a horizontal inline block?

I have this element and I would like it to display inline-block but horizontally. Currently, it is displaying as inline-block but vertically. Here is my HTML: <div class="ondisplay"> <div class="left-text"> <p&g ...

Tips for decreasing the width of a Grid column in MUI React

I have a total of 8 columns. The first column should be the smallest to accommodate a checkbox and does not require much space. I attempted to adjust the xl, lg, md values to 0.5 but was unsuccessful. Is there an alternative method to change the width of t ...

Creating a border indentation for a table row using the <tr> tag

In my database, there is a table that shows the parent-child relationship. Check out this link for more details The HTML structure of the table is as follows: <table> <tr class="parent_row"> <td >1</td> <td>2</t ...

Having a challenge with aligning a form in a view, as neither bootstrap nor plain CSS seem to be helping with centering it correctly

What techniques can I use to center this form properly in both bootstrap and plain CSS? So far, I have been able to center some parts of it, but when I try to center others, they shrink and create a mess. When I use bootstrap to center the elements, they b ...

Create an interactive list with the ability to be edited using HTML and

I'm currently working on a UI scenario that involves a text box field with two buttons beneath it. When the user clicks the first button, a popup will appear prompting them to input an IP address. Upon submitting the IP address in the popup, it will b ...

Error: The function execute_script() is expecting between one and two arguments, however three arguments were provided

While attempting to utilize the Selenium method execute_script() in an automated UI test script, I encountered a type error. The error message indicates that there are too many arguments being passed. TypeError: execute_script() takes from 1 to 2 positiona ...

Refreshing the parent page in Oracle Apex upon closing the child window.When the child window

I have created an interactive report with a form where the interactive report is on page 2 as the parent page and the form page is on page 3 as the child page. In the parent page, I have written JavaScript to open a modal window (form page - page 3) using ...

Is there a JavaScript toolkit specifically designed for animating mobile devices?

Currently in search of a JavaScript Library that is similar to impress.js, allowing me to create dynamic animations on text content that are compatible with iOS/Android. Although Hype by Tumult seems promising, I am not interested in an editor but rather ...

Looking for a way to track the number of visits your website receives using Splunk?

Examples I've attempted: "url" | stats count index="indexname" "url" |stats count Is it necessary to establish logging on my webpage before I can access the hit count? ...

The Problem of Restoring Column Height in Tabulator 4.6.3 Filters

The Issue After activating and deactivating header filters, the column height does not return to its original state. Is this the expected behavior? Is there a way to reset the column height? Check out this JS Fiddle example: https://jsfiddle.net/birukt ...

Header becomes distorted while scrolling down, then returns to normal when scrolling back up

Something strange is happening on my client's website. Whenever I scroll down and then back up, the header displays a large white area where the address/contact bar should be. You can see it in action on the site: rijschool-wolvega.nl I'm not w ...

Having issues with the addClass() method in JavaScript not functioning properly on hover

Coding Challenge <ul class="navBarExtended"> <li><a href="#">Link</a></li> </ul> Styling Solution .onHover{ text-decoration: underline; } Interactive Scripting $("ul.navBarExtended li a").hover( function() ...

Guide on inserting the elements <label>, <input>, and <span> into a <li> in a particular sequence

var btn = document.querySelector('.add'); var remove = document.querySelector('.draggable'); function dragStart(e) { this.style.opacity = '0.4'; dragSrcEl = this; ...

Include a tab button within a vertical tab list using Angular Material

I have utilized Angular Material to create a vertical tab, and I would like to incorporate an Add Tab button within the tab listing itself. Currently, when I add the button, it appears at the bottom of the layout instead. For reference, you can access the ...

The onchange() function for a multi-checkbox dropdown is failing to work as

Issue with Multiple Drop Down Checkbox Functionality: The first parameter is always received as undefined, and the onchange event only captures the value of the first checkbox selected. <select name="multicheckbox[]" multiple="multiple" class="4colacti ...