Can CSS Grid be used to mimic the traditional table spacing?

My challenge involves formatting a grid with two columns and multiple rows. Typically, one column has more content than the other. I want to achieve a layout similar to an old-fashioned table where free space is distributed proportionally based on the content in each cell, but using CSS Grid instead of a traditional table. The goal is to have the cells stack vertically on smaller screens.

While grid-template-columns: auto auto gets close to the desired effect, it evenly splits any remaining space, whereas I need it split at a 2:1 ratio or another specified ratio. Unfortunately, values like 2fr 1fr don't provide the exact result needed, and there isn't an equivalent of 2auto 1auto.

I've explored various combinations of CSS Grid properties to replicate the spacing of a classic table without success. Utilizing a table and overriding its display property at specific screen sizes seems clunky. Is there a way to achieve this using only CSS Grid?

table {
  border: 1px solid red;
}

td {
  border: 1px solid red;
}

.grid {
  display: grid;
  gap: 5px;
  border: 1px solid blue;
  padding: 5px;
  margin-bottom: 5px;
}

.grid>* {
  border: 1px solid blue;
  padding: 5px;
  grid-template-columns: 100%;
}

.grid0 {
  grid-template-columns: fit-content(100%) fit-content(100%);
}

.grid1 {
  grid-template-columns: auto auto;
}

.grid2 {
  grid-template-columns: 2fr 1fr;
}

.grid3 {
  grid-template-columns: minmax(auto, 2fr) minmax(auto, 1fr);
}

@media (max-width: 400px) {
  table.hacky,
  table.hacky tr,
  table.hacky tr td {
    display: block;
  }
}
<p>
Implementing my base grid structure without taking up additional space:
</p>
<div class="grid grid0">
  <div>
  This cell contains significantly more content compared to the other
  </div>
  <div>
  Less content here
  </div>
</div>
<p>
To illustrate, here's a representation using a classic table. Note how the first column receives more free space naturally due to higher content.
</p>
<table cellspacing="5" cellpadding="5" width="100%">
<tr>
  <td>
    This cell contains significantly more content
  </td>
  <td>
    Less content here
  </td>
</tr>
</table>
<p>
First Attempt: Trying "grid-template-columns: auto auto;".
</p>
<div class="grid grid1">
  <div>
  This cell contains significantly more content
  </div>
  <div>
  Less content here
  </div>
</div>
<p>
Second Attempt: Experimenting with "grid-template-columns: 2fr 1fr;".
</p>
<div class="grid grid3">
  <div>
  This cell contains significantly more content
  </div>
  <div>
  Less content here
  </div>
</div>
<p>
Third Attempt: Testing "grid-template-columns: minmax(auto, 2fr) minmax(auto, 1fr);".
</p>
<div class="grid grid3">
  <div>
  This cell contains significantly more content
  </div>
  <div>
  Less content here
  </div>
</div>
<p>
Fourth Attempt: Using a classic table and changing the "display" style below a certain breakpoint.
</p>
<table class="hacky" cellspacing="5" cellpadding="5" width="100%">
<tr>
  <td>
    This cell contains significantly more content
  </td>
  <td>
    Less content here
  </td>
</tr>
</table>

Answer №1

My approach involves using flexbox instead of grid, even though the original post mentioned grid. I believe that achieving a 2:1 ratio is feasible with flex, although the downside is that the growth rates are hardcoded and do not dynamically adjust to content.

table {
  border: 1px solid red;
}

td {
  border: 1px solid red;
}

.grid {
  display: flex;
  gap: 5px;
  border: 1px solid green;
  padding: 5px;
  margin-bottom: 5px;
}

.grid> #first {
  border: 1px solid blue;
  padding: 5px;
  flex-grow: 2;
}

.grid> #second {
  border: 1px solid blue;
  padding: 5px;
  flex-grow: 1;
}

.grid1 {
  grid-template-columns: auto auto;
}

.grid2 {
  grid-template-columns: 2fr 1fr;
}

.grid3 {
  grid-template-columns: minmax(auto, 2fr) minmax(auto, 1fr);
}

@media (max-width: 400px) {
  table.hacky,
  table.hacky tr,
  table.hacky tr td {
    display: block;
  }
}
<p>
Trying out flex for layout
</p>
<div class="grid">
  <div id="first">
  This cell has more, more, more, more, more, more, more content
  </div>
  <div id="second">
  Less content here
  </div>
</div>
<p>
Here's a comparison with a traditional table. Notice how the first column receives more space due to having more content by default.
</p>
<table cellspacing="5" cellpadding="5" width="100%">
<tr>
  <td>
    This cell has more, more, more, more, more, more, more content
  </td>
  <td>
    Less content here
  </td>
</tr>
</table>
<p>
Experiment 1: Testing "grid-template-columns: auto auto;" for layout.
</p>
<div class="grid grid1">
  <div>
  This cell has more, more, more, more, more, more, more content
  </div>
  <div>
  Less content here
  </div>
</div>
<p>
Experiment 2: Trying out "grid-template-columns: 2fr 1fr;" for layout.
</p>
<div class="grid grid3">
  <div>
  This cell has more, more, more, more, more, more, more content
  </div>
  <div>
  Less content here
  </div>
</div>
<p>
Experiment 3: Experimenting with "grid-template-columns: minmax(auto, 2fr) minmax(auto, 1fr);".
</p>
<div class="grid grid3">
  <div>
  This cell has more, more, more, more, more, more, more content
  </div>
  <div>
  Less content here
  </div>
</div>
<p>
Experiment 4: Using a classic table, but changing the "display" property after a certain breakpoint.
</p>
<table class="hacky" cellspacing="5" cellpadding="5" width="100%">
<tr>
  <td>
    This cell has more, more, more, more, more, more, more content
  </td>
  <td>
    Less content here
  </td>
</tr>
</table>

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 <base> tag functions properly when used with the <a href> attribute, but encounters issues when used with JavaScript's document

Encountering an issue with the <base> tag in Internet Explorer (tested on IE11). Despite successfully getting my links to work using the <a href> tag, they fail to work when attempting to use JS document.location. To see this issue in action, ...

Restrict modifications in Sharepoint 2013 to text only

Can content editors be restricted to only adding text on websites? I want to prevent them from using inline styling in HTML or buttons in the editor to add styles. I'm dealing with a few rebellious editors who like to get creative with colors and sty ...

Struggling to delete a table row using jquery

Currently, I am encountering an issue with removing a specific "tr" element within a table using jQuery. Here's the situation: I have a table where rows are clickable. Upon clicking on a row, I can update the data associated with that particular obj ...

Dealing with jQuery hover disruption while toggling classes

I have a simple script below where I want to change the background color on hover for different buttons (regular/inactive and active) and toggle between the two states upon clicking. Although the code successfully changes the background color during hover ...

The ideal choice for a default background image in terms of style and dimensions

I'm using this code to set a background image for my website. body{ background: url('path_to_image'); background-size: 100%; } I find that the background-size property works well for handling different screen sizes. I'm creating ...

Tips and Tricks for Resizing Images within an Angular iframe

Can anyone help me figure out how to make the image content inside my iframe fit perfectly within the frame? I noticed that when displaying a PDF it scales correctly, but when displaying a PNG image it does not scale properly and looks distorted. Any ass ...

Unable to post form data into database due to Javascript undefined data situation

I've been working on an HTML form that can interact with a PHP API to retrieve client data and then update user stats in a MySQL database. Currently, I am converting the data into a JSON object and using JSON.stringify to create a string for sending ...

Create a div element that initially aligns to the left, expands to the full width of the window, and then shrinks back

Hi there! I am currently diving into the world of javascript and jQuery, with a specific goal in mind. I want to create functionality where a div expands to the width of the window when clicked, then shrinks back down to its original size but switches alig ...

Setting an action when clicking on a slice of a Doughnut chart using Chart.js

I have been working on integrating chart.js into my Django Project, and so far it has been going smoothly. I successfully created a doughnut chart with two slices. Now, I am trying to implement separate actions for each slice when clicked, such as redirect ...

JavaScript and the importance of using commas in arrays

I am developing a system that displays text in a textarea when a checkbox is checked and removes the text when the checkbox is unchecked. The functionality is mostly working as intended, but I am facing an issue where commas remain in the textarea after un ...

Align the ion content (or text or label) to the center vertically

I am striving to achieve a similar look as shown in the following images: https://i.sstatic.net/YFMay.png However, I am currently at this stage: https://i.sstatic.net/ZvpBV.png Please note that the first image showcases a bootstrap form, while the seco ...

What is the best way to determine the width of a device, rather than just the

I'm currently working on my debut website and I’ve encountered a minor setback with the meta tag. It seems to be defining a fixed width instead of adapting to the device screen, which is hindering my CSS animation endeavors. Most of the design displ ...

Ion-row appears out of frame

This is a simple layout: <ion-content> <ion-grid *ngIf="loaded"> <ion-row> <ion-col>Stuff</ion-col> <ion-col>Stuff</ion-col> <ion-col>Stuff</ion-col> </ion-row> < ...

Localization for Timeago JS Plugin

Currently, I have integrated the jQuery TimeAgo plugin into my project and included the following code snippet for localization in Portuguese: // Portuguese jQuery.timeago.settings.strings = { suffixAgo: "atrás", suffixFromNow: "a partir de agora", ...

"Exploring the Impact of Font Style on Text Color in Gtk3 Text

After creating a CSS provider using gtk_css_provider_new and loading it with gtk_css_provider_load_from_data, the style data "textview { color: red; font: 30px serif; }" is applied to a gtk_text_view using gtk_style_context_add_provider. However, the resul ...

What is the process for changing an image on a webpage based on a dropdown list selection?

When I came across the site , I was impressed by their feature that allows users to select their currency. By changing the currency, the symbol beside the amount entered also changes accordingly. I am interested in incorporating a similar feature on my we ...

CSS3 List Style Floating Challenge

I have encountered a seemingly simple question, but I am struggling to find a solution. In my HTML code, I have two ul elements. Each li within these lists is styled with float:left in my CSS. The reason behind this styling choice is that I am working on ...

Creating a sleek and modern look with Bootstrap 4: The Transparent sticky

Currently in the process of developing a webpage with sticky navigation. The main challenge I am facing is that I have multiple sections with background images and a transparent navigation bar. It's crucial for the logo to be displayed above the navig ...

Stop the initial expansion of the first accordion with JavaScript on page load

I have a simple accordion created using only pure javascript without jQuery and Pure CSS. Everything is functioning correctly, but the problem I am facing is that the first accordion is automatically expanded when the page loads. I have tried various metho ...

Is it possible to adjust the height of ion.RangeSlider?

Currently, I am using the ion.RangeSlider in my project, which can be accessed here: Although it functions well, I am interested to know if there is a way to increase the size/height of the slider. I have attempted adjusting the height and padding but hav ...