Creating Repeating nth-child Patterns with CSS

Currently grappling with a unique CSS challenge related to patterns.

Here's what I'm aiming for:

  • For LI 1-3 = green background
  • For LI 4-6 = red background
  • For LI 7-9 = blue background

This sequence should repeat as follows:

  • LI 10-12 = green background
  • LI 13-15 = red background
  • LI 16-18 = blue background

This pattern needs to repeat infinitely in the same set of 9 blocks.

Answer №1

To achieve this, you can utilize the CSS selector nth-child.

Since your cycle covers 9 spaces (1-3, 4-6, 7-9), you would use 9n and then factor in the item's position in the list. For instance:

ul > li:nth-child(9n + 1),
ul > li:nth-child(9n + 2),
ul > li:nth-child(9n + 3) {
  background-color: green;
}

Below is a sample snippet demonstrating this concept:

ul > li:nth-child(9n + 1),
ul > li:nth-child(9n + 2),
ul > li:nth-child(9n + 3) {
  background-color: green;
}

ul > li:nth-child(9n + 4),
ul > li:nth-child(9n + 5),
ul > li:nth-child(9n + 6) {
  background-color: red;
}

ul > li:nth-child(9n + 7),
ul > li:nth-child(9n + 8),
ul > li:nth-child(9n + 9) {
  background-color: blue;
}
<ul>
  <li>One</li>
  <li>Two</li>
  <li>Three</li>
  <li>Four</li>
  <li>Five</li>
  <li>Six</li>
  <li>Seven</li>
  <li>Eight</li>
  <li>Nine</li>
  <li>Ten</li>
  <li>Eleven</li>
  <li>Twelve</li>
  <li>Thirteen</li>
  <li>Fourteen</li>
  <li>Fifteen</li>
  <li>Sixteen</li>
  <li>Seventeen</li>
  <li>Eighteen</li>
  <li>Nineteen</li>
  <li>Twenty</li>
</ul>

Answer №2

If you've experimented with using :nth-child(3n) but found it didn't quite achieve the desired outcome, here's a solution tailored to your needs:

Take a look at the following code snippet:

li{
  height: 30px;
  border: 2px solid #000;
  &:nth-child(9n + 1), 
  &:nth-child(9n + 2), 
  &:nth-child(9n + 3){
    background: green;
  }
  &:nth-child(9n + 4), 
  &:nth-child(9n + 5), 
  &:nth-child(9n + 6){
    background: red;
  }
  &:nth-child(9n + 7), 
  &:nth-child(9n + 8), 
  &:nth-child(9n + 9){
    background: blue;
  }
}

Even though it might seem like you're dealing with groups of 3, in actuality, it's groups of 9 that are repeating. This is due to having 3 sets of 3 elements, resulting in a total of 9.

nth:child(9n)

This selector will pinpoint every 9th element. All that's left to do is increment this setup to handle the initial 3 elements, then the subsequent 3, and finally the last 3.

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

Create a speech bubble using only CSS, featuring a partially see-through background and a stylish border

I wish to design a speech bubble using only CSS. There are specific requirements I need to meet: It should adjust its size based on content with some padding or a set size. The background color must be semi-transparent. You must be able to define a borde ...

What is preventing my accordion from closing completely?

I'm currently working on creating FAQ questions in an accordion format. However, I've encountered an issue where adding padding around the answer section prevents the accordion from closing completely. Removing the padding solves the problem, but ...

Having trouble with the contact form sending emails

My contact form box seems to be having trouble directing the mail to my email. When I hit the send message button, it just refreshes the page in a continuous loop. <div class="wow fadeInUp col-md-6 col-sm-12" data-wow-delay="1.6s"> <h1>I ...

What is the best way to split a Bootstrap row into five equal-sized columns?

Trying to divide a Bootstrap row into five columns can be tricky when you only have 12 units available on the device. How can this division be achieved successfully? ...

CSS fixed positioning involves positioning an element relative to the

My website includes a div positioned below the header that contains quick links. I have implemented the solution from http://davidwalsh.name/persistent-header-opacity to ensure this div remains on-screen at all times. However, I would like it to appear a ...

The color of the text on the Homepage appears differently on iOS Safari

At the top of my homepage, I have displayed the company phone number with white text color. It appears correctly as white on my desktop browsers (Firefox and Chrome), also on my Droid phone, but shows up as dark gray on my iOS phone using Safari. Why is th ...

Creating a Delicious Earth Progress Pie

I'm looking to incorporate a unique progress bar design on my website, similar to this one: The progress pie Can anyone guide me on how to create a progress pie with an image inside it that changes color as it moves? ...

Exploring the magic of cubic-bezier transitions in combination with Bootstrap Tabs buttons

I'm experimenting with creating a tab button switching animation similar to a specific reference, but without using Bootstrap Tabs. var tabs = $('.tabs'); var selector = $('.tabs').find('a').length; //var selector = ...

Choosing <label> elements, while disregarding those <label> elements that include <input>

I need assistance with CSS rules to target only <label> elements that do not contain an <input> field inside of them. Is there a specific rule I can use for this? <label for="type">Regular Label</label> <label for="type"> ...

Unusual happenings detected in Magellan Fixed with Foundation 5

I need help creating a product summary sidebar similar to Apple's. I'm using Magellan but it seems to break the page when the width is below 960 pixels. It might be related to the table, but I'm not sure. Any suggestions or assistance would ...

HTML - What steps can I take to ensure my website displays appropriately on various screen sizes?

Currently, I'm having some trouble with the appearance of my website. Let me explain: I've written some basic code (Please note that this code includes margins. I'm not sure if this is causing the issue) but the content doesn't display ...

An empty space separating the header and the navigation bar

As a beginner in HTML and CSS, I decided to create a basic website. However, I encountered an issue with the layout - there seems to be a gap between my header image and navigation bar, but I can't figure out what's causing it. HTML <!DOCTYPE ...

Adjusting the widths of <input> and <select> elements

I am facing an issue with aligning an input text box under a select element, where the input is intended to add new items to the select. I want both elements to have the same width, but my attempts have not been successful (as I do not wish to specify diff ...

Creating line breaks for ToolTip titles in Material-UI can be achieved by using the appropriate syntax and

I am currently working with the ToolTip component and I have a query regarding displaying two lines for the title text - one line for each language rather than having them displayed on a single line. Is it possible to achieve this and how can I style the c ...

Guide on adjusting padding for jQuery Flot graph for improved styling

I am utilizing jQuery Flot to generate a basic graph. I do not want the axes to display on either side, but this is causing the points to be slightly cut off. Any suggestions on how to resolve this issue? $.plot('#'+id, [ { data: data.values ...

Shuffle letters in a word when hovered over, then unscramble them back to their original order

I have noticed a fascinating effect on several websites. To give you an idea, here are two websites that showcase this effect: Locomotive and TUX. Locomotive is particularly interesting to look at as the desired effect can be seen immediately when hovering ...

Tips for correctly linking JS and CSS resources in Node.js/Express

I have a JavaScript file and a stylesheet that I am trying to link in order to use a cipher website that I created. Here is my File Path: website/ (contains app.js/html files and package json) website/public/css (contains CSS files) website/public/scri ...

How can we use jQuery to extract an HTML element's external stylesheet and add it to its "style" attribute?

My goal is to extract all CSS references from an external stylesheet, such as <link rel="stylesheet" href="css/General.css">, and add them to the existing styling of each HTML element on my page (converting all CSS to inline). The reason for this re ...

The layout in Next.js production builds appears to be distinct from the layout in the

Currently, I am working on a website where I have implemented a dark theme by adding a class to the body element. Everything is functioning perfectly in the development environment, but I am encountering issues in the production build. Here is the root l ...

Is it possible to access the HTML template prior to Outlook applying any additional classes during rendering?

I recently received an email template created in a different platform via Outlook. I am attempting to recreate this email in the Salesforce Marketing Cloud Platform, but I am encountering numerous unnecessary tables, classes set by Outlook (MsoNormal, Ms ...