Color-matching cells must be positioned in the same column within the gridbox consecutively

In my current project, I am dealing with a large number of sections. Each section contains one or two rows with columns ranging from 2 to 15, all of equal width. The cells of the same color are placed in the same column, and the layout looks like this:

https://i.sstatic.net/3eS2c.png

To achieve this layout, I am using gridbox. I have almost solved the issue, but the alignment of the second row seems to be consistently off to the left.

.container {
                display: grid;
                width: 100%;
                grid-template-columns: repeat(auto-fit, minmax(0px, 1fr));
                gap: 5px 10px;
                background-color: steelblue;
            }

            .field {
                background-color: black;
                box-shadow: inset 0px 0px 2px #ffffff;
                height: 20px;
                color: white;
                text-align: center;
            }

            .second-row {
                grid-row-start: 2;
            }


            /* colours */

            .colour-black {
                background: black;
            }

            .colour-blue {
                background: blue;
            }

            .colour-yellow {
                background: yellow;
            }

            .colour-red {
                background: red;
            }

            .colour-orange {
                background: orange;
            }

            .colour-purple {
                background: purple;
            }

            .colour-green {
                background: green;
            }

            .colour-brown {
                background: brown;
            }

            .colour-violet {
                background: violet;
            }
<div class="container">
                <div class="field colour-black">1</div>
                <div class="field colour-blue">2</div>
                <div class="field colour-blue second-row">3</div>
                <div class="field colour-green">4</div>
                <div class="field colour-brown">5</div>
                <div class="field colour-orange">6</div>
                <div class="field colour-purple">7</div>
                <div class="field colour-red">8</div>
                <div class="field colour-red second-row">9</div>
                <div class="field colour-violet">10</div>
            </div>

Here are some important points to keep in mind:

  • Each color group can consist of only a maximum of 2 cells.
  • Cells of the same color are always grouped together.
  • Only CSS solutions are acceptable - no changes to HTML structure other than adding classes allowed.
  • A JavaScript solution is not feasible due to the size of the project and time constraints.
  • Alternative non-grid solutions are also welcome.

Answer №1

Simply include the following:

  • grid-auto-flow: column; in the container
  • grid-row: 1; in the field element

By doing this, your layout will function as intended:

.container {
  display: grid;
  width: 100%;
  grid-template-columns: repeat(auto-fit, minmax(0px, 1fr));
  gap: 5px 10px;
  background-color: steelblue;
  grid-auto-flow: column; /* included */
}

.field {
  background-color: black;
  box-shadow: inset 0px 0px 2px #ffffff;
  height: 20px;
  color: white;
  text-align: center;
  grid-row: 1;  /* included */
}

.second-row {
  grid-row-start: 2;
}


/* colours */

.colour-black {
  background: black;
}

.colour-blue {
  background: blue;
}

.colour-yellow {
  background: yellow;
}

.colour-red {
  background: red;
}

.colour-orange {
  background: orange;
}

.colour-purple {
  background: purple;
}

.colour-green {
  background: green;
}

.colour-brown {
  background: brown;
}

.colour-violet {
  background: violet;
}
<div class="container">
  <div class="field colour-black">1</div>
  <div class="field colour-blue">2</div>
  <div class="field colour-blue second-row">3</div>
  <div class="field colour-green">4</div>
  <div class="field colour-brown">5</div>
  <div class="field colour-orange">6</div>
  <div class="field colour-purple">7</div>
  <div class="field colour-red">8</div>
  <div class="field colour-red second-row">9</div>
  <div class="field colour-violet">10</div>
</div>

Answer №2

To enhance the color classes, you can incorporate the use of `grid-column-start` and set `grid-auto-flow` within the container class:

    .container {
        display: grid;
        grid-template-columns: repeat(10, 1fr);
        grid-auto-flow: column;
        gap: 5px 10px;
    }
    .colour-black {
        grid-column-start: 1;
        background: black;
    }
    .colour-blue {
        grid-column-start: 2;
        background: blue;
    }    
    .colour-green {
        grid-column-start: 3;
        background: green;
    }    
    .colour-brown {
        grid-column-start: 4;
        background: brown;
    }    
    .colour-orange {
        grid-column-start: 5;
        background: orange;
    }    
    .colour-purple {
        grid-column-start: 6;
        background: purple;
    }    
    .colour-red {
        grid-column-start: 7;
        background: red;
    }    
    .colour-violet {
        grid-column-start: 8;
        background: violet;
    }    
<div class="container">
  <div class="field colour-black">1</div>
  <div class="field colour-blue">2</div>
  <div class="field colour-blue">3</div>
  <div class="field colour-green">4</div>
  <div class="field colour-brown">5</div>
  <div class="field colour-orange">6</div>
  <div class="field colour-purple">7</div>
  <div class="field colour-red">8</div>
  <div class="field colour-red">9</div>
  <div class="field colour-violet">10</div>
</div>

Answer №3

.containerMain {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-auto-flow: column;
  gap: 5px 10px;
  background-color: steelblue;
}

.colorBoxField {
  background-color: black;
  box-shadow: inset 0px 0px 2px #ffffff;
  height: 20px;
  color: white;
  text-align: center;
}

.second-row {
  grid-column-start: 2;
}

/* colours */

.colour-black {
  grid-column-start: 1;
  background: black;
}

.colour-blue {
  grid-column-start: 2;
  background: blue;
}

.colour-yellow {
  background: yellow;
}

.colour-red {
  grid-column-start: 7;
  background: red;
}

.colour-orange {
  grid-column-start: 5;
  background: orange;
}

.colour-purple {
  grid-column-start: 6;
  background: purple;
}

.colour-green {
  grid-column-start: 3;
  background: green;
}

.colour-brown {
  grid-column-start: 4;
  background: brown;
}

.colour-violet {
  grid-column-start: 8;
  background: violet;
}
 <div class="containerMain">
      <div class="colorBoxField colour-black">1</div>
      <div class="colorBoxField colour-blue">2</div>
      <div class="colorBoxField colour-blue second-row">3</div>
      <div class="colorBoxField colour-green">4</div>
      <div class="colorBoxField colour-brown">5</div>
      <div class="colorBoxField colour-orange">6</div>
      <div class="colorBoxField colour-purple">7</div>
      <div class="colorBoxField colour-purple">7</div>
      <div class="colorBoxField colour-red">8</div>
      <div class="colorBoxField colour-red second-row">9</div>
      <div class="colorBoxField colour-violet">10</div>
    </div>

You can test this method out as well!

 <div class="containerMain">
  <div class="colorBoxField colour-black">1</div>
  <div class="colorBoxField colour-blue">2</div>
  <div class="colorBoxField colour-blue second-row">3</div>
  <div class="colorBoxField colour-green">4</div>
  <div class="colorBoxField colour-brown">5</div>
  <div class="colorBoxField colour-orange">6</div>
  <div class="colorBoxField colour-purple">7</div>
  <div class="colorBoxField colour-purple">7</div>
  <div class="colorBoxField colour-red">8</div>
  <div class="colorBoxField colour-red second-row">9</div>
  <div class="colorBoxField colour-violet">10</div>
</div>

And CSS:

.containerMain {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-auto-flow: column;
  gap: 5px 10px;
  background-color: steelblue;
}

.colorBoxField {
  background-color: black;
  box-shadow: inset 0px 0px 2px #ffffff;
  height: 20px;
  color: white;
  text-align: center;
}

.second-row {
  grid-column-start: 2;
}

/* Colour Definitions */

.colour-black {
  grid-column-start: 1;
  background: black;
}

.colour-blue {
  grid-column-start: 2;
  background: blue;
}

.colour-yellow {
  background: yellow;
}

.colour-red {
  grid-column-start: 7;
  background: red;
}

.colour-orange {
  grid-column-start: 5;
  background: orange;
}

.colour-purple {
  grid-column-start: 6;
  background: purple;
}

.colour-green {
  grid-column-start: 3;
  background: green;
}

.colour-brown {
  grid-column-start: 4;
  background: brown;
}

.colour-violet {
  grid-column-start: 8;
  background: violet;
}

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

Comparing iPad and Desktop Devices

Working on the responsive site sandbox.mercomcorp.com, I encountered an issue where the CSS meant for an iPad device is affecting the desktop CSS. Shouldn't the iPad have its own separate media query from the desktop? Here's a snippet of the CSS: ...

transferring background-image in html between elements

Imagine having a three-level hierarchy HTML code structured like this: <section> <div id="outer"> <div id="inner"> </div> </div> </section> If we set background-image:someImage.jpg to the section, and backg ...

Using jQuery to add or remove multiple classes at once

My navigation tabs use jQuery to add and remove classes for the active tab. However, I'm facing an issue where the active class is not removed when a new tab is clicked or when the user scrolls to a new section. Please refer to the following gist for ...

What are the best ways to ensure a website is responsive in a vertical

When designing a responsive webpage, I encountered an issue with the body element not properly expanding the content vertically. Despite enlarging the web content horizontally, the body failed to adjust its height accordingly, resulting in unwanted black a ...

How can I eliminate the empty spaces around a bar chart in Kendo UI?

Struggling to eliminate the extra space surrounding the Kendo UI chart below. Could this be due to a gap or spacing issue? The goal is to create a single-line bar chart with only grey appearing on the right side. Access JSFiddle Codes Here $(doc ...

Vanish and reappear: Javascript block that disappears when clicked and shows up somewhere else

My goal is to make three squares randomly disappear when clicked on the page. However, I am facing an issue where some of them, usually the 2nd or 3rd one, reappear elsewhere on the page after being clicked. I have created a jsfiddle to demonstrate this: ...

Is it necessary to execute 26 individual mysql queries in order to group items alphabetically in a directory listing page?

I am looking to create a directory listing page for my website that displays all of my articles grouped by the first letter of their titles. Do I have to execute 26 MySQL queries like: mysql query like "SELECT * FROM articles Where title like 'a%&ap ...

Challenges with locating text within the innerHtml property of a VB WebBrowser Object

Using Visual Studio 2010 with Visual Basic.NET In my project, I have created a form that utilizes a WebBrowser control to load an HTML file. Users are able to search for specific words or phrases within the document, prompting me to extract the innerHtml ...

Enhancing jQuery Component while making an Ajax request

When a user clicks a button, I am making an ajax call to send out multiple emails. My goal is to update a "Please wait..." div before and after the call with status updates, as well as report any errors that may occur. However, I have encountered an issue ...

What is the best way to choose the first parent element that includes a specific class within it?

I am searching for a method to identify the initial parent element that includes another element with a specified class within it. For instance: <div class="wrapper"> <div class="item"> <div class="inner-eleme ...

Creating a fixed sidebar in Bootstrap 3 that spans the full width of the column

I'm working with two columns - col-md-8 and col-md-4. In the second column, I have a fixed sidebar that I want to be 100% width of the col-md-4 column. The black box in the image below represents the second column. Despite trying setting the width to ...

CSS Animation Effect on Transparent PNG Image

Today, while working on my website, an interesting CSS effect came to mind. I vividly remember seeing it a few months ago, but unfortunately, I couldn't find it among my bookmarks. The website featured a captivating design with a prominent logo that ...

The height and rows of a textarea element do not adjust properly to the content in FireFox

Looking for a way to create a textarea with dynamic height that adjusts as the user adds new content? The resize property is set to none and overflow is hidden. It seems to be working correctly in Chrome, but Firefox is presenting some mysterious issues wi ...

Extract the variables from the while loop

Is there a way to store the variables outside of the while loop? For instance, I have a slideshow displaying images for each category and a button that when clicked leads to the image gallery related to that category. However, due to graphical reasons, the ...

Getting an 'undefined function' error while calling PHP in two separate includes?

My website template consists of four PHP files: - functions.php (contains main functions for the website) - index.php (main template file to bring other documents together) - header.php (website header) - footer.php (website footer - this is where ...

Exploring the art of JSON interpretation within Highcharts

Below is an example snippet that utilizes static data: Highcharts.chart("container", { title: { text: "Highcharts pie chart" }, xAxis: { categories: [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Ju ...

Tips on creating adaptable images for mobile viewing

My coding conundrum involves the use of two columns - one for an image and the other for a description of that image. However, when viewing my site on mobile devices, the image is cut off at only half its height. Adjusting both columns to col-sm-6 results ...

Navigating to the most recent item within ng-repeat (AngularJS or JavaScript)

I am working on a feature where posts are displayed using ng-repeat in a div, and users can enter new posts in an input box. The posts are sorted so that the latest one appears at the bottom. After adding a new post, I want to automatically scroll down t ...

"Troubleshooting jQuery html() Function Issue in Firefox: Dealing with an Unterminated

Trying to insert the following string into a <p> element: (without specified charset) Цена: 4,80 BGN Поддръжка: (directly from .XML file) &#1062;&#1077;&#1085;&#1072;: 4,80 BGN Encountering ...

Extracting an ID value from a select box in Vue.js

I'm attempting to extract the value of idTipoExame from the following JSON: { "idTipoExame": "11", "mnemonico": "AUR", "exame": "ACIDO URICO" }, { "idTipoExame": "24&qu ...