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:

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

Does adjusting the background transparency in IE8 for a TD result in border removal?

I attempted to create a JSFiddle, but it was not coming together as expected. My goal is to change the color of table cells when hovered over. I have been trying to adjust the opacity of the background color for this effect, but I encountered some strange ...

Having Trouble with Your Instafeed Script

I've been working on a project that involves integrating an Instagram feed into a website. However, I'm facing difficulties getting it to function properly. Here's the code that I have implemented. <script type="text/javascript"> ...

Guide to successfully navigating to a webpage using page.link when the link does not have an id, but is designated by a

This is my current code snippet: async function main(){ for(int=0;int<50;int++){ const allLinks = await getLinks(); //console.log(allLinks); const browser = await puppeteer.launch({ headless: true }); const page = await browser.newPa ...

What is the best way to update the content of a modal using jQuery and AJAX?

Despite previous inquiries on this matter, none of the answers provided have been helpful to me. Therefore, I am addressing the issue again... I am currently developing the frontend of a website that does not involve any backend functionality (no server c ...

What could be the reason that the Mui button styles are not being applied even after setting it to 'contained'?

Upon updating to the most recent version of MUI, I've noticed a problem with MUI buttons when using the contained variant. <div className="flex"> <Button variant="contained" color="primary" fullWidt ...

Avoiding scrolling when a button (enveloped in <Link> from react-scroll) is pressed in React

Currently, I am implementing the smooth scroll effect on a component using react-scroll. However, adding a button inside the component to create a favorite list has caused an issue where the smooth scroll effect is triggered upon clicking the button. Is ...

Adjust the size of an HTML image for printing using a JavaScript window.print() function

On my website, I have a print option set up where specific elements are hidden using @media. How can I adjust the size of an image within the @media query when my JavaScript print function is triggered? I am able to modify a regular div element easily, but ...

Experiencing difficulty adjusting the width of a Tumblr post with JavaScript or jQuery?

Calling all coding experts! I've been working on customizing a Tumblr theme, and everything is looking good except for one issue. I'm struggling to adjust the width of photos on a permalink (post) page. Check out this link: You'll notice t ...

What is the method to adjust the font size for section, subsection, and other sections in Doxygen?

I am looking to customize the font size of \section and \subsection in doxygen's html output. Additionally, I want to include numbering for the sections and sub(sub)sections in the format: 1 Section1 1.1 Subsection1.1 1.1.1 Subsubsection ...

Troubleshooting Issue with InfoWindow Display on Multiple Markers in Google Maps

I'm having trouble getting my markers to show different infowindows. No matter what I do, the markers always display the content of the last "contentString" in the loop. Despite reading through multiple posts on this issue, I haven't been able t ...

Implementing a Vue.js Scrollable Table

I am currently working on a table that is populated using the vue.js v-for method: <table> <tr><th>Name</th><th>Surname</th></tr> <tr v-for="user in users"><td>@{{user.name}}</td><td>@{ ...

Discovering the process of extracting a date from the Date Picker component and displaying it in a table using Material-UI in ReactJS

I am using the Date Picker component from Material-UI for React JS to display selected dates in a table. However, I am encountering an error when trying to show the date object in a table row. Can anyone provide guidance on how to achieve this? import ...

Is there a bug with text rotation in CSS 3?

I'm having trouble understanding how text rotation works in CSS3. For example, when I try to rotate text like you can see here, the rotated text never seems to be in the correct location, despite setting properties like: right: 0; bottom: 0; There ...

Issues with JQuery list selectors

Many individuals have raised concerns about selectors in the past, but no matter what I try, my code seems right yet it doesn't work as intended. My situation involves a list of actions that are displayed or hidden when their corresponding "folder" is ...

Is there an alternative solution to resolving this issue with OTP verification?

Working on a verification system where users are redirected to verify their account with an OTP code after signing up. However, I encountered an issue where the code doesn't seem to work as expected and no error messages are being displayed. <?php ...

Unable to activate check box button by clicking on it

Hi there! I am having trouble with a checkbox button on my website. The button appears fine, but for some reason it's not working properly (it doesn't get unchecked when clicked). Can anyone help me fix this issue? I'm still learning HTML an ...

HTML, JavaScript, and PHP elements combine to create interactive radio buttons that trigger the appearance and disappearance of mod

On my page, I have multiple foreach loops generating divs with different information. These divs are displayed in modals using Bootstrap. However, I am encountering an issue where if I select a radio button and then close the modal and open another one, th ...

Tips on Importing a Javascript Module from an external javascript file into an <script> tag within an HTML file

I'm facing an issue while attempting to import the 'JSZip' module from an external node package called JSZip in my HTML File. The usual method of importing it directly using the import command is not working: <script> import ...

The new Facebook login button in my app seems to be glitchy as it fails to redirect users to the appropriate page after

I am working on a web application and have successfully integrated Facebook login from developers.facebook.com. However, I am facing an issue where after the user logs in with their Facebook credentials, they are not redirected to my application. Additiona ...

Troubleshooting: Why isn't my jQuery Click Event functioning in Mozilla but working perfectly fine in

While I know this question has been asked before, I can't seem to locate the posts I remember reading about it. I apologize for any duplication, but I am trying to implement a "click anywhere BUT here to close element overlay" effect on my personal we ...