Distinctive design for three different list items

Currently working on a project that involves the use of ul and li elements.

I need to change the background for every 3rd li element in alternating patterns. Since the li elements are generated from the backend, I do not know how many there will be.

I attempted using CSS3 child classes but was unsuccessful. Is there a way to achieve this using CSS or jQuery?

.list-item li
{
  width:30%;
  display:inline-block;
 }
<ul class="list-item">
  <li>list item 1</li>
  <li>list item 2</li>
  <li>list item 3</li>
  <li>list item 4</li>
  <li>list item 5</li>
  <li>list item 6</li>
  <li>list item 7</li>
  <li>list item 8</li>
  <li>list item 9</li>
</ul>

The goal is to have the same background color for list items 1, 2, 3, 7, 8, and 9, and a different color for list items 4, 5, and 6.

In the provided image, you can see that a table is used to achieve this effect by utilizing nth-child(even)/(odd) properties. Due to limitations, a table cannot be used in this case, so the solution must involve manipulating the li elements.

Answer №1

To apply styles to the 6nth, 6n-1th and 6n-2th items, use the following CSS:

.list-item li
{
  width:30%;
  display:inline-block;
  background: #cfc;
 }
.list-item li:nth-child(6n-2),
.list-item li:nth-child(6n-1),
.list-item li:nth-child(6n){
  background: #ccf;
}
<ul class="list-item">
  <li>list item 1</li>
  <li>list item 2</li>
  <li>list item 3</li>
  <li>list item 4</li>
  <li>list item 5</li>
  <li>list item 6</li>
  <li>list item 7</li>
  <li>list item 8</li>
  <li>list item 9</li>
  <li>list item 10</li>
  <li>list item 11</li>
  <li>list item 12</li>
  <li>list item 13</li>
  <li>list item 14</li>
  <li>list item 15</li>
  <li>list item 16</li>
  <li>list item 17</li>
  <li>list item 18</li>
</ul>

Answer №2

Try out this CSS code for a unique style:

li:nth-child(3n+0) {
    background: red;
}
li:nth-child(3n+1) {
    background: green;
}
li:nth-child(3n+2) {
    background: blue;
}

For a demonstration, visit this link

Answer №3

Are you aiming to accomplish this goal?

If you wish to change the styling for every third item, use the :nth-child() selector with an argument like 3n+0. By solving this equation as n increases from 0, you will determine which items will be targeted.

Just a heads up:

2n+0 is essentially even numbers
2n+1 is essentially odd numbers

Edit noted:

I have included a JavaScript function to achieve alternating rows as shown in the picture that was later added to the question. Check out the demo on the fiddle!

var alternate = (function() {

  var count = 0;
  var flipAt = 3;
  var condition = false;

  function init(num) {
    flipAt = num;
  }
  
  function process() {
    if(count === flipAt) {
      condition = condition ? false : true;
      count = 0;
    }
    count++;
    return condition ? true : false;
  }

  return {
    init : init,
    process : process
  }
})()


$('li').each(function(index, el) {
  if (alternate.process()) {
    $(el).css('background', 'blue');
  }
});
li:nth-child(3n+0) {
    background: red;
}

li:nth-child(3n+1) {
    border: 2px solid green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
    <li>5</li>
    <li>6</li>
    <li>7</li>
    <li>8</li>
    <li>9</li>
    <li>10</li>
    <li>11</li>
    <li>12</li>
    <li>14</li>
</ul>

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

Link not functioning correctly on mobile-responsive CSS

Having some trouble with hyperlinks over a background image. The hyperlinks work fine normally, but in mobile view, nothing happens when clicking on them. Below is the code for the hyperlinks. You can also visit the site and see that the "Post a Project" ...

It is recommended to place Laravel JavaScript files either in the view or public directories for optimal

When working with a JavaScript that requires a root directory path provided by PHP to call a JSON file, the question arises whether to place the code in public/js or within the template.blade.php file. If the code is placed in public/js, an absolute path ...

Retrieve data from an HTML table using PHP

This snippet of code is encapsulated within table tags and retrieves data from a database to populate the table. Within the database, there is an auto-increment column named 'id'. The objective is to obtain the 'id' value associated wit ...

Embedding HTML within a PHP conditional statement

I recently encountered an issue while trying to display a source image using PHP. The initial code worked perfectly: <html> <h3>First Test</h3> <img src="example1.php" /> </html> However, I wanted to implement user validatio ...

Implementing CSS Transform for Internet Explorer

Hello, is there a way to make this function in IE ?? I have been looking into IE transformations but it appears that they are not supported.. Is there an alternative method or something else? It works in other browsers like Firefox, Chrome, and Opera. I s ...

You can view the object's variable 'this' for access

When attempting to access the variable this inside the slider options object, I encountered an issue: $('.slider').slider({ prevButton: $(this).find('.prev'), nextButton: $(this).find('.next'), }); However, modifying ...

`Trying to conceal the tr elements on button click proves tricky with jquery`

I have a pair of buttons, #btn1 and #btn2, as well as two table rows, #tr1 and #tr2. The goal is to toggle the active state of the buttons when they are clicked by the user. The specific requirements are: When the button is set to active, the elements ...

Configuring JSON in PHP and JavaScript

Although I have already asked this question before, I am experiencing some issues in my code. I know that utilizing JSON is necessary and after researching multiple sources, I grasp the concept but somehow am unable to implement it correctly. Here is my co ...

Transforming an HTML5 game into a native mobile application

I'm currently working on creating a HTML5 game. While I find HTML5 to be the most user-friendly programming language, I am facing an issue. I want my game to run as a native application on desktops (Windows, Mac, and Linux) rather than in a web browse ...

Exploring the jQuery syntax within Twitter Bootstrap JS files

Questioning the syntax used in Twitter Bootstrap's JS files. Take for example this snippet from bootstrap-dropdown.js: !function ($) { /* ... */ /* Querying the Standard Dropdown Elements -- Inquiry Code * ================================= ...

How to modify checkbox and textarea values in a MySQL database with Jquery

In my MySQL database, I have a textarea and three checkboxes. By default, the textarea value is null and the checkbox values are set to zero (0). When I enter text into the textarea, I am able to update the text value in my database. However, I am facing ...

FadeOut/FadeIn Iframe Animation

Help needed with making an iframe fade in and out using jQuery. Something seems off with the code and I can't figure out why it's not working. Can anyone spot the mistake? HTML <body> <center> <div class="headbackground" ...

Convert JavaScript back into an HTML attribute

Is there a way to include a JavaScript code in an HTML attribute like shown below? <div class="xx" animation="yy" animate-duration="<script>Code goes here<script>" ...> </div> I'm struggling to figure it out, is there a solut ...

The JQuery datepicker fails to provide the date in the format mm/dd/yy

Recently, I attempted to transform a date into the dd/mm/yy format using JQuery datepicker. Unfortunately, my results displayed in the dd/mm/yyyy format instead. Here is the code snippet that I utilized: chkIn = $.datepicker.formatDate("dd/mm/yy", cinDate ...

When using ngClass and the has-error class on load, the class will still be loaded even if the expression evaluates

When attempting to load the has-error class from Bootstrap using ng-class with a condition on a variable injected to the controller from my factory, I encounter an issue where even though the initial value of the variable is false, the has-error class load ...

Choose a specific inner div element within another div using jQuery

Trying to target a specific div within another div in my HTML structure. Here's how it looks: <div id="Stage_game_page1"><div id="cube0">[...]</div><div id="cube1">[...]</div></div> I am attempting to select #cube ...

jticker.js enables autoscroll feature on a mobile device

I'm currently utilizing jticker.js to showcase my data. While everything works fine on larger screens, I'm encountering an issue on mobile devices where the scroll stops once the height of the screen is reached. I would like the scroll to contin ...

Identify and emphasize CSS codes within PHP files using Notepad++

I'm working with a PHP file in Notepad++ that correctly highlights PHP code. Within this file, I have JavaScript code which is properly recognized between the <script type="text/javascript"> and </script> tags. However, the CSS code withi ...

Embedding complex JSON information into an HTML dropdown menu

Here is a JSON string that I am working with: { "electronics": { "cameras": [ "sony", "nikon", "canon" ], "TV": "none", "laptop": [ "hp", "apple", "sony" ] }, "home": { "furniture": [ ...

Ways to ensure a <div> element adjusts its height based on inner content dimensions

When using buttons in a chatbot that have text which changes based on selected options, I've encountered an issue where the text length sometimes exceeds the button's space. I'm looking for a way to make the containing div automatically adj ...