Tips for changing div visibility depending on class name

I'm looking to optimize the sorting of two sets of elements with equal amounts. Currently, they appear clustered together. Is there a method to arrange them into a single column, alternating based on their class names? For example, .item1, .item2, .item1, .item2, and so on.

.item1 {
  grid-area: myArea1;
}

.item2 {
  grid-area: myArea2; 
}

.item1, .item2 {
  opacity: 0.2;
}

.grid-container {
  display: grid;
  grid-template-areas: 'myArea1' 'myArea2';
  grid-gap: 10px;
  background-color: #2196F3;
  padding: 10px;
}
<div class="grid-container">
  <div class="item1">1</div>
  <div class="item1">11</div>
  <div class="item1">111</div>
  <div class="item1">1111</div>
  <div class="item2">2</div>
  <div class="item2">22</div>
  <div class="item2">222</div>
  <div class="item2">2222</div>
</div>

I'm also interested in non-grid, pure (S)CSS alternatives. The number of items may vary.

Answer №1

A creative workaround akin to my earlier response

.item1 {
  grid-column: 1;
}

.item2 {
  grid-column: 2;
  align-self: end;
}


.grid-container {
  display: grid;
  grid-template-columns: 0 auto;
  grid-auto-rows: 2lh;
  grid-auto-flow: dense;
  background-color: #2196F3;
  padding: 10px;
}
<div class="grid-container">
  <div class="item1">1</div>
  <div class="item1">11</div>
  <div class="item1">111</div>
  <div class="item1">1111</div>
  <div class="item2">2</div>
  <div class="item2">22</div>
  <div class="item2">222</div>
  <div class="item2">2222</div>
</div>

Answer №2

If you have an idea of how many columns you need, or can determine it programmatically, one option is to use the columns: 2; CSS property on your grid-container div.

.grid-container {
  columns: 2;
}

Check out this jsfiddle demo

For more information, refer to the columns documentation

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

Is it possible to automatically refresh a webpage with the same URL when accessed on the same server?

Is there a way to trigger a URL page refresh using JS code located on the same server? For example, I have a URL page open on multiple devices connected to the same server. How can I ensure that when I click 'Update,' the page refreshes simultan ...

Transferring JavaScript to a different page using EJS

Feeling a little stuck here and could use some assistance. It seems like a simple issue, but I'm tired and could really use a fresh pair of eyes to take a look. I'm trying to pass the 'username' variable, but since I'm new to this ...

Guide on displaying the name attribute of a button along with its price in a separate div when clicked

Upon clicking the Koala button, I want to display a message showing the id name of the button and the corresponding price for Koalas. Here is the HTML code snippet: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <link h ...

Sending simple form information through AJAX to a PHP web service

Attempting to send form data via jQuery and Ajax to a PHP web service (php file) for echoing the results. This is my index.html file <html> <head> <title>PHP web service &amp; AJAX</title> <link rel="stylesheet" type="text/ ...

What is the process for submitting a form to a PHP page?

Could you assist me in posting a form with two fields on a php page using $_POST['json']; where the object will be of json type? <div id="result"></div> <form name="form" id="form" method="post" > Name: <input type="text ...

What is the best way to center content without aligning text in the center using Bootstrap?

When using Bootstrap, I have divided my main page into two grids to enable content to stack vertically in mobile view. However, I am struggling to center the text horizontally and vertically within the grid without aligning the text to the center using the ...

What is the best way to create a dynamic hyperlink that leads to a detailed information page based on the specific link clicked?

I'm currently working on a web page that displays a list of users, and I want each user's name to be clickable, leading to a page with specific details about that user. I'm new to this field, so I'm unsure where to start. My idea is to ...

What is the best way to adjust the size of a background image on Safari

Using Chrome When using Chrome, the functionality is just like what's depicted in the image above. However, when using Safari The issue arises, as it does not work as expected in Safari. See below for the CSS code: background-image: url(r ...

Signal for a complete 360 degree rotation of an image

Looking to enhance my 360 image rotator with an indicator that fades out after the first image. I added this function to the end of my 360.js: (function(){ if( $('#first').css('visibility','hidden')) { $('#rotat ...

Ways to specifically load a script for Firefox browsers

How can I load a script file specifically for FireFox? For example: <script src="js/script.js"></script> <script src="js/scriptFF.js"></script> - is this only for Firefox?? UPDATE This is how I did it: <script> if($. ...

Reorganize containers without relying on bootstrap styling

Is there a method to rearrange the order of two divs without using the bootstrap library? <div class='parent'> <div class='child-1'>Abc..</div> <div class='child-2'>Xyz..</div> </div> I ...

Tips for ensuring consistent sizing of card items using flexbox CSS

I'm attempting to create a list with consistent item sizes, regardless of the content inside each card. I have experimented with the following CSS: .GridContainer { display: flex; flex-wrap: wrap; justify-content: space-around; align-item ...

Is transforming lengthy code into a function the way to go?

I have successfully implemented this code on my page without any errors, but I am wondering if there is a more concise way to achieve the same result. Currently, there is a lot of repeated code with only minor changes in class names. Is it possible to sh ...

The React Vite application encountered an issue: There is no loader configured for ".html" files at ../server/node_modules/@mapbox/node-pre-gyp/lib/util/nw-pre-gyp/index.html

**Encountered errors in a React Vite web app** ** ✘ [ERROR] No loader is configured for ".html" files: ../server/node_modules/@mapbox/node-pre-gyp/lib/util/nw-pre-gyp/index.html ../server/node_modules/@mapbox/node-pre-gyp/lib/node-pre-gyp.js:86 ...

What is the best way to incorporate buttons that trigger PHP scripts within a MySQL database?

I have a MySQL database filled with various records that I've displayed in an HTML table on a webpage. I want to add two buttons next to each record, both of which trigger PHP scripts. For example, let's say the table lists the names of three pe ...

Is there a way to customize the default padding applied to Material-UI components across the board?

I've been struggling with getting the AppBar component to cover the entire area by default. I've searched through the documentation and scoured Google for a solution, but so far I haven't found one that works. I also attempted to adjust th ...

The captivating logo animation of Google Chrome

For optimal viewing experience, it is recommended to use Chrome or any WebKit browser. https://www.google.com/intl/en/chrome/browser/ Hovering over the chrome logo reveals an amazing effect. I tried downloading the page source, but got lost in it. The s ...

Verify Radio Buttons in AngularJS

I thought validating a selection from a radio group would be simple, but I'm struggling to find the right solution. In my form, I need to ensure that at least one option is selected from the radio buttons. Using the 'required' attribute on e ...

Aligning text at the center of the page, stacked on top of each other

I am struggling with creating a responsive main page for a website. I am new to this and feeling lost on how to proceed. My goal is to stack text on top of each other in a readable way while also ensuring it looks good on mobile devices. However, my curren ...

Clicking the set button in Jquery mobile datebox resets the time

I am experimenting with the jquery mobile datebox (courtesy of Jtsage) to select time and date. Unfortunately, every time I reset the time by clicking the set button, I am unable to retrieve the correct time. Below are my code snippets: $.extend($.jtsag ...