Using CSS nth-child to create a two-column checkers layout

I have formulated a form on my website that divides into two columns by using floats. I am interested in creating a "checkered" effect for these columns. Can anyone guide me on how to utilize nth-child to achieve this design layout? For reference, please check out the attached screenshot illustrating what I aim to accomplish. Below is a snippet of the HTML code:

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
    Grade Calculator
</title>
    <style type="text/css">
        .form-group {
            padding: 15px;
            width: 45%;
            float: left;
        }
        .form-group:nth-child(3n+0){
            background: #e4e4e4;
        }
        form {
            margin: 25px auto;
            border-width: 0px 1px;
            border-style: solid;
            border-color: #ccc;
            width: 50%;
        }
        label {
            width: 350px;
            margin: 0;
            padding: 0;
            display: block;
        }
        input {
            width: calc(100% - 20px);
            margin: 15px 0px;
        }
    </style>
</head>
<body>
    <form method="post" action="./Default.aspx" id="form1">
<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="Era4Q8Ttswx16Rg3XXwdNh7LuVHYalpdgKUCOJGwMcgBX7OP6eVRcScpUQ68BMteFlRltJ8L3vXUMx8wG+4DEf3Oi4RZcMfq/H6H7MbdpUbBLssudRCbS2QNV6vILQ9uBG64j4wb2CKGAAM2+aw+BYfCpxNSeloQ2BbcnUnOHjk=" />
</div>

    <div>
        <div class="form-group">
            <span id="aOneActualLbl">Assignment 1</span>
            <input name="aOneActualTxt" type="text" value="8" id="aOneActualTxt" /> 
            <span id="aOnePossibleLbl">Possible</span>
            <input name="aOnePossibleTxt" type="text" value="10" id="aOnePossibleTxt" />
        </div>
        <div class="form-group">
            <span id="aTwoActualLbl">Assignment 2</span>
            <input name="aTwoActualTxt" type="text" value="8" id="aTwoActualTxt" /> 
            <span id="aTwoPossibleLbl">Possible</span>
            <input name="aTwoPossibleTxt" type="text" value="10" id="aTwoPossibleTxt" />
        </div>
        <div class="form-group">
            <span id="aThreeActualLbl">Assignment 3</span>
            <input name="aThreeActualTxt" type="text" value="9" id="aThreeActualTxt" /> 
            <span id="aThreePossibleLbl">Possible</span>
            <input name="aThreePossibleTxt" type="text" value="10" id="aThreePossibleTxt" />
        </div>
        <div class="form-group">
            <span id="aFourActualLbl">Assignment 4</span>
            <input name="aFourActualTxt" type="text" value="9" id="aFourActualTxt" /> 
            <span id="aFourPossibleLbl">Possible</span>
            <input name="aFourPossibleTxt" type="text" value="10" id="aFourPossibleTxt" />
        </div>
        <div class="form-group">
            <span id="aFiveActualLbl">Assignment 5</span>
            <input name="aFiveActualTxt" type="text" value="8" id="aFiveActualTxt" /> 
            <span id="aFivePossibleLbl">Possible</span>
            <input name="aFivePossibleTxt" type="text" value="10" id="aFivePossibleTxt" />
        </div>
        <div class="form-group">
            <span id="aSixActualLbl">Assignment 6</span>
            <input name="aSixActualTxt" type="text" value="8" id="aSixActualTxt" /> 
            <span id="aSixPossibleLbl">Possible</span>
            <input name="aSixPossibleTxt" type="text" value="10" id="aSixPossibleTxt" />
        </div>
        <div class="form-group">
            <span id="aSevenActualLbl">Assignment 7</span>
            <input name="aSevenActualTxt" type="text" value="8" id="aSevenActualTxt" /> 
            <span id="aSevenPossibleLbl">Possible</span>
            <input name="aSevenPossibleTxt" type="text" value="10" id="aSevenPossibleTxt" />
        </div>
        <div class="form-group">
            <span id="aEightActualLbl">Assignment 8</span>
            <input name="aEightActualTxt" type="text" value="8" id="aEightActualTxt" /> 
            <span id="aEightPossibleLbl">Possible</span>
            <input name="aEightPossibleTxt" type="text" value="10" id="aEightPossibleTxt" />
        </div>
        <div class="form-group">
            <span id="aNineActualLbl">Assignment 9</span>
            <input name="aNineActualTxt" type="text" value="8" id="aNineActualTxt" /> 
            <span id="aNinePossibleLbl">Possible</span>
            <input name="aNinePossibleTxt" type="text" value="10" id="aNinePossibleTxt" />
        </div>
        <div class="form-group">
            <span id="aTenActualLbl">Assignment 10</span>
            <input name="aTenActualTxt" type="text" value="8" id="aTenActualTxt" /> 
            <span id="aTenPossibleLbl">Possible</span>
            <input name="aTenPossibleTxt" type="text" value="10" id="aTenPossibleTxt" />
        </div>
        <div class="form-group">
            <input type="submit" name="runCalculation" value="Calculate Grade" id="runCalculation" />
            <span id="currentGradeLbl">Your current grade is B</span>
        </div>
    </div>

Looking at the image example attached below, it represents assignments 2, 3, 6, 7, and 10 shaded in grey to create a checkerboard pattern. Is there a way to achieve this layout through coding?

https://i.sstatic.net/HGp6l.jpg

Answer №1

When attempting to solve this problem, I initially tried using a single formula but found it easier to break it down into multiple series.

2, 6, 10, 14, ... => 4n-2
3, 7, 11, 15, ... => 4n-1

Therefore,

.form-group:nth-child(4n-2), .form-group:nth-child(4n-1){
    background: #e4e4e4;
}

For further reference, you can view this JSFiddle.

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

Adjust Element Width Based on Scroll Position

I'm attempting to achieve a similar effect as seen here: (if it doesn't work in Chrome, try using IE). This is the progress I've made so far: http://jsfiddle.net/yuvalsab/op9sg2L2/ HTML <div class="transition_wrapper"> <div ...

Setting both the height and row height of the table is not allowed

Currently employing js DataTable, I aim to establish a default height for the table and row. My current approach is as follows: $(document).ready(function() { var table = $('#example').DataTable ({ iDisplayLength: 15, "bLen ...

Display and conceal different sections using hyperlinks

Hey there, I'm back with another issue related to this code snippet on jsfiddle: https://jsfiddle.net/4qq6xnfr/9/. The problem I'm facing is that when I click on one of the 5 links in the first column, a second div appears with 3 links. Upon clic ...

Tips for showing more rows by clicking an icon within an Angular 2 table

When I click on the plus (+) button in the first column of each row, only one row expands. How can I modify it to expand multiple rows at a time? Thanks in advance. <div> <table class="table table-striped table-bordered"> <thead> ...

What is the best way to split a single column into two using blocks?

I am working with a container that currently has all its blocks lined up in a column. I need to divide them into two columns, with 5 blocks in each. Unfortunately, the plugin I am using generates the code for me and does not allow me to insert my own div e ...

Error message: "An issue occurred: Unable to access undefined properties (specifically, borderRadius) in MUI react."

I recently created a navigation bar with an integrated search bar component. The styling for my search component was done using MUI styled from @emotion/styled in MUI library, where I applied a borderRadius: theme.shape.borderRadius. However, I encountere ...

What is the best way to eliminate HTML unicode content from the final item in a continuously changing list?

li:after{ content:"\00b6"; } <ol> <li>banana</li> <li>orange</li> <li class="last-class">apple</li> </ol> I am currently working on a dynamic list where the number of <li> tags varies over t ...

Angular JS: Automatically toggle the visibility of a div on a panel click event

There is a row, panel, and a closed div on load. Clicking on the panel should make the div appear while making the row and panel disappear. Clicking on X should make the panel and row appear again. <script src="https://ajax.googleapis.com/ajax/libs/ ...

Issue with Bootstrap modal not dismissing when using click function

This is my custom function and div. When I click the function, it executes successfully but the modal window remains visible. <script type="text/javascript> $(document).ready(function () { $("#btnRespuesta").click(function () { //al ...

My div is not displaying the background image properly after adding the Bootstrap CSS

Strangely enough, Bootstrap seems to be interfering with the background image of a div. The following is the HTML code: <div class="wide"> <div class="col-xs-5 line"> <hr> </div> <div class="col-xs-2 logo"> Log ...

an issue encountered while trying to print a webpage styled with CSS

Users are given the option to print the current page on the website by clicking on a menu element: <li> <a href="#" onClick="window.print()"> <i class="icon-print"></i> Print Page </a> </li> The page contai ...

Three divs arranged side by side with responsive design for mobile viewing

This code generates 3 divs for display. However, I am looking to optimize them for mobile devices. What steps can I take to accomplish this? Currently, the divs are oriented to the left and are not visible on a mobile device. I would like them to display ...

Grammarly is seamlessly inserting padding into my body element

After setting up my website on GitHub, I ran into an issue where the Grammarly Chrome extension was automatically adding a padding-top attribute to the body tag. <body data-new-gr-c-s-check-loaded="14.1028.0" data-gr-ext-installed="" ...

How can you position an image and text side by side without specifying the width using a div?

Having some trouble aligning an image (which is contained in a div) and some text (also in a div) on the same line without setting a width for the text. Here's what I've tried so far. <div id="container"> <div id="image" style="flo ...

Take a break in between keyframe animations

I have a basic animation set up with keyframes. @-webkit-keyframes rotation { 0% { -webkit-transform: rotate(10deg); } 25% { -webkit-transform: rotate(5deg); } 50% { -webkit-transform: rotate(10deg); } 75% { -webkit-transform: ...

Creating a magical transformation for the bootstrap carousel

Trying to create a bootstrap wizard with a carousel and disable auto-scrolling by setting bs-interval to "false" without success. Here is my code: <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" ...

Ways to resize column widths in tree views on Odoo version 10?

I have been struggling to adjust the column width of the columns in my tree view. I have attempted a few different solutions: Trying to change the width attribute in the field tag: width="100px" or width="15%%" Using the style att ...

Manipulate elements by adding and removing classes sequentially based on an array

Previously, some had difficulty understanding my question. I have an array of variables representing the ids of 5 divs. My goal is to change the color of each div sequentially for a brief moment before reverting back, mimicking the behavior of traffic ligh ...

What could be causing the svg to not be visible inside the div?

I am struggling to display an SVG element within a div as it is not visible at all. Can someone help me make the SVG visible in the div? Here is a link to the code: http://codepen.io/helloworld/pen/HjkhK <div style="height:100px;background:yellow;"> ...

"Create a notification pop-up in CSS that appears when a link is clicked, similar to

I am looking to create a model page that functions similarly to the inbox popup on Stack Overflow. When we click on the inbox icon, a small box appears with a tiny loader indicating messages or comments. The box then expands depending on the content inside ...