A dynamic, multi-column, two-dimensional layout using CSS Grid

Within my HTML structure, there exists a single container div followed by a variable number of child divs. The objective is to implement a multi-column layout for these child divs based on their respective class names.

<div class="list-units">
    <div class="s">s</div>
    <div class="s">s</div>
    <div class="s">s</div>
    <div class="m">m</div>
    <div class="m">m</div>
    <div class="m">m</div>
    <div class="a">a</div>
    <div class="a">a</div>
    <div class="r">r</div>
    <div class="d">d</div>
    <div class="o">o</div>
    <div class="o">o</div>
    <div class="x">x</div>
</div>

The requirement is to arrange the child divs into columns based on their class name. For instance, all divs with the class s should occupy one column, while those with the class m should populate the next column in a 2-dimensional grid. This arrangement applies to all other class names as well. It's important to note that this HTML content is generated dynamically at runtime and cannot be modified.

The necessary styles are outlined below:

.list-units {}
.list-units .s {}
.list-units .m {}
.list-units .a {}
.list-units .r {}
.list-units .d {}

Answer №1

Utilizing CSS-Grid along with the grid-auto-flow:column property can achieve this layout.

.list-units {
  display: grid;
  margin: 1em;
  grid-auto-flow: column;
  gap: 1em;
  text-align: center;
}

.list-units div {
  border: 1px solid grey;
}

.list-units .s {
  grid-column: 1;
}

.list-units .m {
  grid-column: 2
}

.list-units .a {
  grid-column: 3;
}

.list-units .r {
  grid-column: 4;
}

.list-units .d {
  grid-column: 5;
}

.list-units .o {
  grid-column: 6;
}

.list-units .x {
  grid-column: 7
}
<div class="list-units">
  <div class="s">s</div>
  <div class="s">s</div>
  <div class="s">s</div>
  <div class="m">m</div>
  <div class="m">m</div>
  <div class="m">m</div>
  <div class="a">a</div>
  <div class="a">a</div>
  <div class="r">r</div>
  <div class="d">d</div>
  <div class="o">o</div>
  <div class="o">o</div>
  <div class="x">x</div>
</div>

Answer №2

To enhance layout design, consider implementing the Grid Column Property. You can learn more about this technique and see examples at this link.

 .list-units {
    display: grid;
 }
 .list-units .s {
    grid-column: 1;
 }

 .list-units .m {
    grid-column: 2;
 }

Answer №3


      .list-units {
      display: grid;
      margin: 10px auto;
      background: #ffffff;
      grid-template-columns: auto auto auto auto;
      grid-auto-flow: row dense;
      margin-bottom: 20px;
      border: 1px solid grey;
      }
    
      .item {
      height: 60px;
      background: pink;
      display: flex;
      justify-content: center;
      align-items: center;
      color: whitesmoke;
      border: 1px solid red;
      }
    
      .list-units .s {
      grid-column-start: 1;
      }
      .list-units .m {
       grid-column-start: 2;
      }
      .list-units .a {
      grid-column-start: 3;
    }
    .list-units .r {
      grid-column-start: 4;
    }
    .list-units .d {
       grid-column-start: 5;
    }
    .list-units .o {
       grid-column-start: 6;
    }
    .list-units .x {
       grid-column-start: 7;
    }
<div class="list-units">
    <div class="s">s</div>
    <div class="s">s</div>
    <div class="s">s</div>
    <div class="m">m</div>
    <div class="m">m</div>
    <div class="m">m</div>
    <div class="a">a</div>
    <div class="a">a</div>
    <div class="r">r</div>
    <div class="d">d</div>
    <div class="o">o</div>
    <div class="o">o</div>
    <div class="x">x</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

Guide to creating a vertical handler that can be resized

Did you know that you can resize tables in http://www.jsfiddle.net? I'm curious about how to resize just the "Vertical Handler". Could someone share the source code with me? If possible, please provide an example on http://www.jsfiddle.net. ...

How is a scrollbar's color determined in Firefox?

Short version: Firefox displays an intriguing behavior where it automatically styles the scrollbar in lime green but not light green. The question arises, why does it render one color but not the other? While responding to another query, I discovered that ...

Conflict with choosing options identified when using Select Box on IOS iPad mini

I am currently attempting to display a select box with scrolling on an iOS iPad mini. Issue: The options are not appearing inside the select box, instead they are overflowing outside of it in the popover. CSS .indicators_geography { font-size: 12px; ...

Enhance the Bootstrap parallelogram navbar menu by ensuring the menu items are perfectly aligned

I am working on customizing Bootstrap 5's navbar menu to create a rollover menu with parallelogram-shaped active menu items. I followed the approach recommended here, and while it works, the output is not as polished as I would like because the parall ...

How can I retrieve the span html element without using a class with ajax?

Trying to work with Ajax syntax for the first time, I have this HTML code. <div class="select2-container select2" id="s2id_ServiceID-2-1" style="width: 100%;"> <a href="javascript:void(0)" onclick="return false;" class="select2-choice" tabind ...

Perhaps dividing numbers with regular expressions

My HTML textarea allows users to input numeric serial numbers in various formats - either inline, separated by any character, or in columns from an Excel file. The serial codes are either 14 characters long if the first character is "1," or always 15 char ...

The ng-repeat ng-switch combination is not functioning as expected

My data can be simplified to the following in my code: function DemoCtrl($scope) { $scope.myData= [ {text: "blah", other: 3, V: 'caseOne'}, {text: "blah", other: 3, V: 'caseTwo'}, {text: "blah", other: 3, V ...

It seems like Chrome does not recognize 0 as a numerical value

Take a look at this snippet of HTML code: <!DOCTYPE html> <html> <head> <title>Test</title> </head> <body> <form name='test'> <input type='number&apo ...

The best way to horizontally arrange card-images side by side in react native

Recently delving into the world of react native, I encountered an issue with aligning card images side by side using react-native-elements. My goal is to have two rows on phones and three rows on tablets. However, despite setting the flex-direction to row ...

Using Jquery to target all elements except for their child elements

I have the following code: <div id="mn"> <span></span> <span> <span></span></span> <span></span> <span> <span></span></span> <span></span> </div& ...

HTML DOCTYPE causing havoc with my CSS styling

When using the materialize framework, I've noticed that adding <!DOCTYPE html> causes my CSS to not be recognized. However, everything works fine without <!DOCTYPE html>. Any suggestions on how to resolve this issue? Update : <?ph ...

Using Javascript to validate passwords and Bootstrap for design enhancements

I've been working on learning Javascript and some basic HTML using bootstrap v5 recently. One of the tasks I'm currently tackling is creating a Sign-in form and implementing validation for the required fields. While following the Bootstrap docu ...

The button's color doesn't seem to change to the correct one when clicked

Despite my efforts, I can't seem to get the button border to match the color of the hover state. I've tried using the focus selector and even the active state, but nothing seems to work. Below is the relevant code snippet from the project, and yo ...

What is the proper way to bind CoreUI's CSwitch component?

I am trying to incorporate the CSwitch component into a DevExtreme data grid. While the DxSwitch component functions correctly, I am unable to get the CSwitch to work properly. It seems like I might be using the wrong binding. Could that be the issue? < ...

Tips for Transferring Values Between Multiple Dropdown Menus with jQuery

Hello there, can anyone guide me on how to transfer selected items from one multiple combo box to another multi-combo box? I would really appreciate it if someone could provide an example for this scenario. <HTML> <HEAD> <TITLE></T ...

Why am I unable to attach this to JQuery?

$("input").keydown(function(event){ if(event.keyCode === 13){ return false; } }); I need to prevent the default action when the "enter" key is pressed in any of my text input fie ...

Utilizing the map function in React to create a button reference

I am currently facing an issue that is relatively simplistic and doesn't have any real-world application. My goal is to locate a 'green' button and make it blink for a duration of 3 seconds. How can I achieve this using React? const btnLay ...

Nested elements not transitioning using CSS

Why is my transition not being applied to the submenu <a> tag? The color change is working fine, but the transition does not occur on hover. When I use the same rules on another element with a different main class, it works perfectly. It seems like t ...

Bottom-anchored horizontal navigation bar for seamless scrolling experience

Is there a way to create a navbar with horizontal scrolling at the bottom of the page without compromising its scrollability? When I use position: fixed;, the navbar becomes non-scrollable. div.scrollmenu { background-color: #333; overflow: auto; ...

Struggles with implementing CSS Grid's dynamic column heights and expanding rows

Linked partially to Removing empty space in CSS Grid, with specific adjustments I am aiming for. Essentially, I want my rows and columns to expand and contract based on the content present. In this CodePen example, you can observe that the content of the ...