Ensure uniform column width for recurring rows in CSS grid, irrespective of content width

Is there a way to ensure that a CSS grid maintains the same width for repeating rows, even if the content in each cell varies in length?

I am attempting to set column 1 to be 10% width, column 2 to be 45% width, and allocate the remaining space to column 3. To achieve this, I have used the following code:

grid-template-areas:
'user-l user-m user-m user-m user-m user-m user-m user-r user-r user-r user-r'

The user-container data is repeated for "n" number of users.

body {margin: 0}

.user-l {grid-area:user-l}
.user-m {grid-area:user-m}
.user-r {grid-area:user-r}

.user-container {
  display: grid;
  grid-template-areas:
  'user-l user-m user-m user-m user-m user-m user-m user-r user-r user-r user-r'
}

.user-l,
.user-m,
.user-r {
  border: 1px solid #ddd;
  padding: 20px;
}

.user-l {background-color: #2196F3}
.user-m {background-color: #219683}
.user-r {background-color: #216683}
<h1>Grid Elements - How to keep width the same ?</h1>

<p>In the example below, blue and green should have the same width all the time. </p>

<div class="user-container">
  <div class="user-l">
  <p>Name: jsdsdsd djsds</p>
  </div>
  <div class="user-m">
     <p>Age: 23</p>
  </div>
  <div class="user-r">
  <p>Occupation: Doctor</p>
  </div>
</div>

<div class="user-container">
  <div class="user-l">
  <p>Name: ANother longer Names</p>
  </div>
  <div class="user-m">
     <p>Age: 23</p>
  </div>
  <div class="user-r">
  <p>Occupation: Another Occupation</p>
  </div>
</div>

Answer №1

Implemented changes to the width of user-l user-m user-r and added

grid-template-columns: 10% 45% 1fr;
to control the size of the div.
body {margin: 0}

.user-l {grid-area:user-l}
.user-m {grid-area:user-m}
.user-r {grid-area:user-r}

.user-container {
  display: grid;
 grid-template-columns: 10% 45% 1fr;
  grid-template-areas:
  'user-l user-m  user-r'
  
}

.user-l,
.user-m,
.user-r {
  border: 1px solid #ddd;
  padding: 20px;
}

.user-l {background-color: #2196F3; }
.user-m {background-color: #219683;}
.user-r {background-color: #216683;}
<h1>Grid Elements - How to keep width the same ?</h1>

<p>In the example below, blue and green should have the same width all the time. </p>

<div class="user-container">
  <div class="user-l">
  <p>Name: jsdsdsd djsds</p>
  </div>
  <div class="user-m">
     <p>Age: 23</p>
  </div>
  <div class="user-r">
  <p>Occupation: Doctor</p>
  </div>
</div>

<div class="user-container">
  <div class="user-l">
  <p>Name: ANother longer Names</p>
  </div>
  <div class="user-m">
     <p>Age: 23</p>
  </div>
  <div class="user-r">
  <p>Occupation: Another Occupation</p>
  </div>
</div>

Answer №2

To resolve the issue, utilize the following code snippet. The solution was provided based on feedback from VXp.

<!DOCTYPE html>
<html>
<head>
<style>

.user-container { display: grid;}
.user-container  {
grid-template-columns: 10% 45% 1fr; 
  }
  
.user-l {
background-color: #2196F3;
border: 1px solid #ddd;
padding 20px;
}
.user-m {
background-color: #219683;
border: 1px solid #ddd;
padding 20px;
}
.user-r {
background-color: #216683;
border: 1px solid #ddd;
padding 20px;
}

</style>
</head>
<body>

<h1>Maintaining Equal Width for Grid Elements</h1>

<p>The goal is to ensure that the blue and green elements always have the same width.</p>


<div class="user-container">
  <div class="user-l">
  <p>Name: Example Name</p>
  </div>
  <div class="user-m">
     <p>Age: 23</p>
  </div>
  <div class="user-r">
  <p>Occupation: Developer</p>
  </div>
</div>

<div class="user-container">
  <div class="user-l">
  <p>Name: Another Example Name</p>
  </div>
  <div class="user-m">
     <p>Age: 27</p>
  </div>
  <div class="user-r">
  <p>Occupation: Designer</p>
  </div>
</div>

</body>
</html>

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

`place the table inside a hidden div`

I am trying to create a non-editable table that can be overlayed with a div under specific conditions. However, it seems like my current implementation is not working as expected. What could be causing this issue? CSS <style type="text/css"> # ...

Customizing the Twitter bootstrap navigation in Wordpress

I've been working on theming my navigation menu using Twitter Bootstrap. I want it to resemble the demo example of a fixed top banner, which can be seen here: http://getbootstrap.com/examples/navbar-fixed-top/ Although I have managed to get it to wor ...

Guide on including an arrow to the right of a drop-down list button using CSS

I'm currently working on developing a drop down list using polymer. I am facing an issue with the CSS styling. Below is an example for reference: Access jsfiddle example here <div style="width:200px" class="button initial gray"> <div style ...

Alignment of UI Divs with CSS Bootstrap and HTML

I am facing an issue with two divs in my UI. One div has a label, and the other contains a file selector. Currently, they are stacked vertically instead of being side by side. https://i.stack.imgur.com/fIz03.png How can I align these divs horizontally wit ...

Determining the dimensions of a div with a scrollbar using Jquery

Is there a way to get the width and height of a div with scroll that includes both visible and hidden content using JQuery? If anyone has a solution for getting these values, please share. <div id="area" class="divLeftCem area"> <div id="pan ...

Changing the Position of HTML Scripts in React

I am facing an issue where I need to relocate an external script within a specific section of my page. However, I am unable to access the CSS attributes associated with this item. It seems that it is displayed as an iFrame, making it difficult to modify th ...

Leveraging .Net ConfigurationManager.AppSettings in Cascading Style Sheets

I am facing an issue where I have a specific color key in my AppSettings for the company's brand color, which needs to be applied to a CSS class. The challenge is how to incorporate this key into my stylesheet. Is there a way to access the Configurati ...

Discrepancy detected in AGM Map printout

When attempting to print my Angular AGM Map from Chrome, I noticed a large grey gap in the map. This gap is visible even when "background graphics" are turned off and it causes the map image below it to shift downwards. If you want to reproduce this issue ...

Utilizing jQuery Methods within Node.js

Recently delved into the world of node.js and created multiple pages (such as login, signup, blog, etc). If I desire an effect in which: 1. Hovering over the "News" button triggers a slider to appear downwards, To make adjustments to an image within ...

Place a list with scrolling and overflow features side by side

Having trouble getting my headers to scroll with overflow auto and white-space nowrap. Can't figure out why it's not working. I want to create hyperlinks within headers to link to specific parts of the website. I have the code for hyperlinking s ...

What could be causing the lack of changes when trying to use justify content within the parent div?

I've been delving into CSS and trying to utilize justify-content in flex to center my content, but unfortunately, it's not cooperating. Here is the code I'm working with: .top-container{ display: flex; flex-flow: row nowrap; ma ...

I attempted to update the text on an HTML page using the content of "conetnt", however, it was unsuccessful

.custom-div { outline: none !important; width: 450px; margin: auto; background-color: #ccc !important; text-indent: -9999px;} .custom-div::before{content: 'sample';color: black;} ...

When using HTML/Bootstrap5, the ms-auto class does not correctly align items to the right in the navbar

I'm currently working on mastering bootstrap through an online tutorial, but I've hit a roadblock when it comes to right-aligning items in a navbar. I've double-checked my code against the instructor's, and it's a perfect match: &l ...

Chrome Flexbox Hacks: Controlling the Size of Nested Items

How can I ensure that an element nested within a flexbox member in Google Chrome is limited to the size of its container? For example, consider the following scenario: <div style="display:flex; flex-direction:column; height:100vh;"> <div style= ...

Performing PHP MySQL table database insertion utilizing the $_GET approach using Codeigniter-3 and Dropzone-4.1 plugin

Currently working with CodeIgniter 3 and running into an issue with "id_case" showing as undefined index. When I click a link in index.php: <?php echo "<td><a href='/ci_dropzone/?id_case=".$row['id_case']."'>Upload File ...

Tips for showcasing several images with accompanying content once the webpage has finished loading

I am faced with an issue on my PHP website. The website is a social networking platform with numerous images and contents. I am seeking a way to first display only the content to the user, then show the images after they have all finished loading. Addition ...

What is the best way to conceal an element so that it only becomes visible when a user begins to input text

Hey there! I'm in the process of adding a search feature to my Jekyll site, and I've opted to use Simple-Jekyll-Search, which you can check out here: Link. Take a peek at what's inside my search.html: <input type="text" id="my-search-in ...

Personalized Carousel using Ng-Bootstrap, showcasing image and description data fields

I have been working on customizing an Angular Bootstrap Carousel and have managed to successfully change the layout. I now have two columns - with the image on the right and text along with custom arrows on the left. My goal is twofold: First, I am lookin ...

Calculator built with HTML, CSS, and JavaScript

Hi there, I'm experiencing some issues with my calculator. The buttons seem to be working fine and lining up correctly, but for some reason, nothing is showing up on the monitor or getting calculated when I press the buttons. Here's the code that ...

Ensure that the input box expands to occupy the entire HTML page

After reviewing numerous pages and questions related to this topic, I have located the correct solution but am struggling to implement it. My goal is to achieve a similar outcome to the second question, but I'm having difficulty figuring out how to do ...