Changing up the grid with alternating colors

I am looking to create a two-column grid layout with alternating colors for the grid blocks. However, using just nth-child(odd) or nth-child(even) won't give me the desired outcome.

I believe I can achieve this by utilizing some clever nth-child techniques instead of resorting to a JavaScript solution, but I am not entirely sure how to do it.

Check out this example of what I'm aiming for: http://codepen.io/abbasinho/pen/Gbnze

Here is the HTML code from the example above—I would prefer to not use additional classes to designate the colors:

<div class="grid-holder">
  <div class="grid red"></div>
  <div class="grid blue"></div>
  <div class="grid blue"></div>
  <div class="grid red"></div>
  <div class="grid red"></div>
  <div class="grid blue"></div>
  <div class="grid blue"></div>
  <div class="grid red"></div>
</div>

This is the SASS code used in the example:

.grid-holder {
  width: 50%;
  margin: 0 auto;
  overflow: hidden;
}

.grid {
  width: 50%;
  height: 200px;
  float: left;

    &.red {
      background: red;
    }

    &.blue {
      background: blue;
    }

}

Answer №1

By following a pattern that repeats every 4 elements (red/blue/blue/red), you can easily achieve this using variations of :nth-child(4n+x):

<div class="grid-holder">

  <div class="grid"></div>
  <div class="grid"></div>
  <div class="grid"></div>
  <div class="grid"></div>
  <div class="grid"></div>
  <div class="grid"></div>
  <div class="grid"></div>
  <div class="grid"></div>

</div>
.grid-holder {
    width: 50%;
    margin: 0 auto;
    overflow: hidden;
}

.grid {
    width: 50%;
    height: 200px;
    float: left;
    background: red;

    // The 2nd and the 3rd element in our pattern will be blue
    &:nth-child(4n+2),
    &:nth-child(4n+3) {
        background: blue;
    }  
}

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

Incorporating HTML within PHP

I'm struggling with the following issue: after this, I end up with a blank page. Here is the code snippet in question: I am attempting to use HTML within PHP and then PHP again within HTML. The rest of the code is functioning correctly. If I replace & ...

Saving Bootstrap Data - Modals, Tags and Notifications

Check out my code snippets on this example/demo page. /* Messages Modal */ $(document).ready(function(){ var informer = $("#messageInformer a"); var refreshBadge = function(messageCount) { var badge = informer.find(".badge"); ...

Press the "show additional content" button on the nytimes.com website using selenium

I'm experiencing difficulty scrolling through this webpage. Once I reach the bottom of the page, I am unable to locate and click on the "SHOW MORE" button using selenium. self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") ...

Is a Selenium loop a viable option?

</head> <body> <table cellpadding="1" cellspacing="1" border="1"> <thead> <tr><td rowspan="1" colspan="3">test</td></tr> </thead><tbody> <tr> <td>click& ...

Alteration in relational shift of division placement

My webpage is divided into three sections with the following proportions: 15% - 65% - 20% In the right section, there is a div with the ID alchemy. I want to set the height of this div using <div style="height:150px;">. However, when I set the heig ...

Second word in the textbox receiving attention

When I type in the text box, it always focuses on the second word instead of the first word. If I set Maxlength=1, it doesn't allow me to type. I have to press backspace before typing. Can anyone help me with this issue? Below is my code: <input ...

Could we potentially implement a dropdown menu that spans the full width of the screen while still being contained within a wrapper set to a width of 1024

I have a query - can I create a full-width dropdown menu within a wrapper that has a width of 1024px (with contents centered on the screen)? My dropdown menu is causing some issues. Although the hover effect is not functioning yet, I am still working on st ...

I'm encountering difficulties utilizing ternary operators in TypeScript

I am struggling with ternary operators in TypeScript and need help understanding the issue. Please review the code below: const QuizQuestionContainer = ({ qa }: QuizQuestionContainerPropsType) => { const { question, option1, option2, option ...

Retrieve the scope object using angular.element and the $ID parameter

In order to transfer data from an angular application to scripts that operate outside of angular, I am seeking a solution since I cannot modify the code of the angular app. By utilizing Angular Batarang and NG-Inspector extensions in Chrome, I have identi ...

Images in SCSS distorted due to styling issues

I am encountering an issue with a round image displaying properly on a browser, but appearing distorted after building the apk and running it on my android phone. The image's width is greater than its height. How can I ensure that it remains round? M ...

Utilizing a constant variable and the setTimeout function to initiate an animated menu display

I'm currently utilizing jQuery to enhance my dropdown menu functionality by toggling the active class defined in my CSS. However, I've encountered a difficulty in adjusting the timeout for one of the elements. Below is some pseudo code to clarify ...

What is the best way to design the unique curly arrow/tail for a chat bubble in a React Native project?

Being new to react-native, I decided to create a chat bubble by first attempting it on a browser and then trying to replicate it in react-native. The challenge I'm facing is replicating the arrow in react-native. Do you have any ideas or suggestions? ...

Unable to load the content within the iFrame

While attempting to retrieve the URL of an older website, I encountered an error: Fetch API cannot load http://xyz. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://abc' is therefore not allo ...

Adjust the alignment of an expansion panel header to the right using Vuetify

Incorporating the Vuetify expansion panel, I am aiming to align a specific portion of the content within the <div slote="head"></div> to the right, resulting in this desired layout: https://i.sstatic.net/tbOL8.jpg https://i.sstatic.net/22NTS. ...

Exporting table data from Bootstrap 3 DataTable to Excel is not functioning as expected

I am attempting to export the data from a table to MS Excel by referring to this example: https://datatables.net/extensions/buttons/examples/styling/bootstrap.html While the content displays correctly on the page, the issue arises when I try to export it. ...

Unleashing the power of Sass and CSS in your Next Js project

Trying to incorporate sass and css modules into my next.config.js has been challenging due to an error I keep encountering: Failed to compile. ./node_modules/@riskalyze/react-ui/node_modules/@riskalyze/calendar/assets/index.css Unknown word (1:1) > 1 ...

Align a div to the center horizontally at the top of the screen

Is there a way to vertically center text within a div and also horizontally center the div at the top of the screen? Visit this link for more details <div id='custom'> example text </div> #custom{ width: 50%; background: #FF00 ...

Is it possible to make the `<body>` element respect the offset (i.e. the user's current scroll position) when the 'custombox' modal is triggered?

I'm currently facing an issue with the scroll bars in the browser while using custombox.js in combination with BS4. Let me provide some context: I am working with a Custombox modal, which is similar to a BS4 modal. When a modal is opened in BS4, a c ...

Error: Bootstrap/CSS not functioning properly on IE and ED browsers

Currently, I am tackling a web application project, and much to my surprise, I discovered that the CSS/bootstrap is not functioning properly on Internet Explorer and Microsoft Edge. Despite attempting various solutions found on Stack Overflow, none seem to ...

The `d-flex flex-column` is causing the image within the div to be hidden

The setup looks like this: <div className="d-flex flex-column"> <div> Text </div> <div style={{ backgroundImage: 'url(...)' }}> </div> </div> The URL is functioning correctly. The div element is ...