How can I create a CSS grid with two groups of columns, each occupying 50% of the grid?

My goal is to create a CSS grid layout with 6 columns, but I want to divide them into 2 groups of 3 columns each. Each group should take up 50% of the width, and be left-aligned.

For example:

#parent {
  width: 100%;
  display: grid;
  grid-template-rows: 1fr;
  grid-template-columns: repeat(3, 1fr) repeat(3, 1fr);
}

.cell {
  background-color: yellow;
}

.cell2 {
  background-color: lightblue;
}
<div id="parent">
  <div class="cell">Label 1</div>
  <div class="cell">60</div>
  <div class="cell">44</div>
  <div class="cell2">Label 2 (xxxx)</div>
  <div class="cell2">80</div>
  <div class="cell2">11</div>
</div>

You can also view this layout on plunkr.

The current grid setup produces:

https://i.sstatic.net/slc44.png

However, I want it to look like:

https://i.sstatic.net/WCUXH.png

Each "group" of columns should have cells with min-content so they can grow as needed within their 50% width.

This is just a simplified example, in my actual project the number of divs is dynamic and controlled by an Angular component with *ngFor in the markup, meaning I can't predict the total number of divs in advance (though I know they will always be in groups of 3).

Is it possible to achieve this with a single grid layout?

Update 1

Upon further reflection, my initial example was too simple compared to my actual application requirements.

I am actually working with a table structure that may contain multiple rows. This complicates layouts which rely on nested divs, especially when dealing with different label widths.

For instance, consider the following structure proposed in one of the responses:

 <div id="parent">
  <!-- first row -->
  <div class="cell cell1">
    <div>Label 1</div>
    <div>60</div>
    <div>44</div>
  </div>
  <div class="cell cell2">
    <div>Label 2 (xxxx)</div>
    <div>80</div>
    <div>11</div>
  </div>

  <!-- second row -->
  <div class="cell cell1">
    <div>Label3 xxx</div>
    <div>60</div>
    <div>44</div>
  </div>
  <div class="cell cell1">
    <div>Label4 1xxx</div>
    <div>60</div>
    <div>44</div>
  </div>
</div>

#parent {
  width: 100%;
  display: grid;
  grid-template-rows: 1fr;
  grid-template-columns: 1fr 1fr;
}

.cell {
  display: flex;
  gap: 1rem; 
}

.cell1{
  background-color: yellow;
}

.cell2 {
  background-color: lightblue;
}

In this scenario, the columns won't align correctly:

https://i.sstatic.net/N86At.png

Answer №1

Combining grid and flex layouts can offer a versatile solution.

#container {
  width:100%;
  display: grid;
  grid-template-rows: 1fr;
  grid-template-columns: repeat(2, 1fr);
}

.item-left, .item-right {
  width:100%;
}

.item-left {
  background-color: yellow;
}
.item-right {
  background-color: lightblue;
}

.flex {
  display: flex;
  gap: 10px;
}
<div id="container">
  <div class="flex item-left">
    <div class="item">Label 1</div>
    <div class="item">60</div>
    <div class="item">44</div>    
  </div>
  <div class="flex item-right">
    <div class="item2">Label 2 (xxxx)</div>
    <div class="item2">80</div>      
    <div class="item2">11</div>      
  </div>
</div>

Answer №2

To create a layout with two columns, you can utilize a grid system where each column consists of a flexbox with three elements.

#parent {
  width: 100%;
  display: grid;
  grid-template-rows: 1fr;
  grid-template-columns: 1fr 1fr;
}

.cell {
  display: flex;
  gap: 1rem; //adjust as needed
}

.cell1{
  background-color: yellow;
}

.cell2 {
  background-color: lightblue;
}
<div id="parent">
  <div class="cell cell1">
      <div>Label 1</div>
      <div>60</div>
      <div>44</div>
  </div>
  <div class="cell cell2">
      <div>Label 2 (xxxx)</div>
      <div>80</div>
      <div>11</div>
  </div>
</div>

If you prefer not to use the top-level grid, a similar layout can be achieved with two nested flexboxes. Make sure to set the flex basis of 0 on both flex items to ensure they are the same size.

#parent {
  width: 100%;
  display: flex;
}

.cell {
  display: flex;
  flex: 1 1 0px;
  gap: 1rem; //adjust as needed
}

.cell1{
  background-color: yellow;
}

.cell2 {
  background-color: lightblue;
}
<div id="parent">
  <div class="cell cell1">
      <div>Label 1</div>
      <div>60</div>
      <div>44</div>
  </div>
  <div class="cell cell2">
      <div>Label 2 (xxxx)</div>
      <div>80</div>
      <div>11</div>
  </div>
</div>

Answer №3

#container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  justify-items: start;
}
.child-row{

display:flex;
padding-left:10px;
padding-right:10px;
}
<div id="container">
 
  <div class="child-row">
  <div>first--->  1.1</div>
  <div>content 1.2</div>            
  <div>content 1.3</div>
  </div>
  <div class="child-row">
  <div>content 1.1</div>
  <div>content 1.2</div>
  <div>content 1.3</div>
  </div>

 <div class="child-row">
 <div>second--->  2.1</div>
 <div>content 2.2</div>    
 <div>content 2.3</div>
 </div>
  <div class="child-row">
  <div>content 2.1</div>
  <div>content 2.2</div>
  <div>content 2.3</div>
  </div> 

  <div class="child-row">
  <div>third--->  3.1</div>
  <div>content 3.2</div>    
  <div>content 3.3</div>   
  </div>
  <div class="child-row">
  <div>content 3.1</div>
  <div>content 3.2</div>
  <div>content 3.3</div>
  </div>
</div>

Answer №4

To achieve the desired layout, consider using the minmax() function:

#parent {
  display: grid;
  grid-template: 1fr / repeat(2, max-content max-content minmax(25%, 1fr));
}

/* Custom styling for demonstration */
.cell { background-color: yellow; box-shadow: inset 0 0 2px #000; }
.cell:nth-child(6n+4),
.cell:nth-child(6n+5) { background-color: lightblue; }
.cell:nth-child(3n) { background-color: red; }
<div id="parent">
  <div class="cell">Label 1</div>
  <div class="cell">60</div>
  <div class="cell">44</div>
  
  <div class="cell">Label 2 (xxxx)</div>
  <div class="cell">80</div>
  <div class="cell">11</div>
  
  <div class="cell">Label 3 (xxxx)</div>
  <div class="cell">60</div>
  <div class="cell">44</div>
  
  <div class="cell">Label 4</div>
  <div class="cell">80</div>
  <div class="cell">11</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

Search for and swap out a parameter value within an array located in a div using jQuery

A query I have is regarding a div where I am aiming to update the value of a parameter within an array. The PHP code I am using to create the HTML is as follows: <div id="data1<?php echo $data['id']; ?>" data-url="<?php echo 'p ...

What is the best way to extract the content within a nested <p> element from an external HTML document using the HTML Agility Pack?

I'm currently working on retrieving text from an external website that is nested within a paragraph tag. The enclosing div element has been assigned a class value. Take a look at the HTML snippet: <div class="discription"><p>this is the ...

The problem with Text Input and Label Effects

This code is currently in use. It seems to be working fine, but there's one issue that needs addressing: when moving from one input field to the next, the labels for each field are getting mixed up with their corresponding inputs. This problem will b ...

Issues encountered with integrating external jQuery/JavaScript functions with Wordpress child theme template

After creating a custom template in my WordPress child theme, I added a link to the Bootstrap v3.0.3 .js file stored on my site. While the popup modal is functioning properly, the jQuery tabs seem to be having some issues. Although they are being display ...

Never-ending accessible document available for download to test internet speed

For my download speed tester project, I am implementing a straightforward method. I download a large image file from the internet and check if it arrives within a reasonable time frame. The specific file I am using is sourced from here. However, I am conc ...

What is the best way to customize the appearance of a form element within an angular-schema-form schema without affecting the overall

Currently, I am in the process of constructing a form using the incredible angular-schema-form. Creating the form schema object has been a success so far. My goal is to configure all the form components within the schema using the x-schema-form property in ...

CSS: Issue with rounding errors in Em units

Recently, I decided to update the CSS file for a website I'm working on by making most elements and fonts dynamic in size using the em unit instead of px. The new sizes seem to be working, but there's an issue that's been bothering me. It s ...

What is the best way to assign a secondary name to a form field in Django python forms?

My website features a form that is dynamically generated using a python loop to call each field by its variable name. The following is the snippet of HTML code used: {% csrf_token %} {% for form_field in task_form %} <div class ="wrap-input2 vali ...

Managing numerous range sliders in a Django form

My Request: I am looking to have multiple Range sliders (the number will change based on user selections) on a single page. When the sliders are moved, I want the value to be updated and displayed in a span element, as well as updating the model. The Issu ...

Issues with Autofocus while Searching and Selecting from Dropdown menu

Here is the JavaScript code I am using: <script type="text/javascript"> function handleSelectionChange(selectElement, nextField) { nextField.focus(); } function handleKeyPress(event, currentField, nextField) { i ...

Detecting media queries for the Samsung Galaxy S3 and S4 devices

I have been working on implementing media queries to show text boxes with different styles for iPhone 5, iPhone 6, Samsung Galaxy SIII, and Samsung Galaxy S4. Initially, everything was working fine for iPhone 5 and iPhone 6, but when I switched from an i ...

When using Angular 5's ngModel, the user interface displays the updated value dynamically without requiring the

When filling out my form, I encounter an issue with a select element and a bind variable. If I make a change to the value and save it, everything works as expected. However, if I make a change in a modal window but then close the modal without saving the v ...

Is there a way to redirect links within an iframe when a user decides to open them in a new tab?

I am currently developing a web application that allows users to access multiple services, such as Spark and others. When a user selects a service, like Spark for example, the app will open a new tab displaying my page (service.html) with user information ...

What could be causing the malfunction in this positioning and display?

<article id="l1"> <img class="active_pic" id="a1" src="img/load.gif"/> </article> <article id="l2"> <img class="active_pic" id="a2" src="img/load.gif"/> </article> <article id="l3"> <img class="activ ...

Retrieving the chosen option from a drop-down menu using FormCollection in MVC

I need to extract the selected value from a drop-down list in my HTML form, which is posting to an action using MVC. How can I achieve this? Here is my HTML form: <% using (Html.BeginForm()) {%> <select name="Content List"> <% ...

Using the <object> tag in AngularJS partials for improved functionality

Trying to incorporate the <\object> tag or <\iframe> within a partial HTML file and then include that HTML in another page using ng-include. Here is my attempt: <div class="container"> <!-- This section doesn't displ ...

Using a single AJAX function to write on various tags

Information regarding the likes and dislikes count is retrieved from the server using an ajax function with the following queries: $q3="select count(value) as a from rank where personID='$person' and itemID='$itemID' and value>0 ...

Asynchronous loading of HTML templates using JQuery/JavaScript

Currently, I am in the process of creating a webpage that includes code snippets loaded from txt files. The paths and locations of these txt files are stored in a json file. First, the json file is loaded in, and it looks something like this: [ {"root":"n ...

Adjust the position of an image to move it closer to the top using CSS

I have a fiddle where I am trying to adjust the position of an image (keyboard) to match a specific design. https://i.sstatic.net/flqCH.png In my fiddle, the keyboard image is slightly lower than the desired position shown in the design. I am looking for ...

Having trouble with the tool tip not showing up correctly in Internet Explorer?

Check out the code snippet below: <table> <tr> <td class="bgimg" valign="middle" width="10%" title="some title" nowrap> <img src="images/SomeLogo.gif"/> </td> </tr> </table ...