Choosing the first occurrence of each element using the nth-of-type selector

I've created this HTML structure:

<div class="container">
    <div class="item">1</div>
    <div class="item">2</div>
    <div class="item">3</div>
    <div class="item">4</div>
    <div class="item">5</div>
    <div class="item">6</div>
    <div class="item">7</div>
    <div class="item">8</div>
    <div class="item">9</div>
    <div class="item">10</div>

    <div class="item">1</div>
    <div class="item">2</div>
    <div class="item">3</div>
    <div class="item">4</div>
    <div class="item">5</div>
    <div class="item">6</div>
    <div class="item">7</div>
    <div class="item">8</div>
    <div class="item">9</div>
    <div class="item">10</div>

    <div class="item">1</div>
    <div class="item">2</div>
    <div class="item">3</div>
    <div class="item">4</div>
    <div class="item">5</div>
    <div class="item">6</div>
    <div class="item">7</div>
    <div class="item">8</div>
    <div class="item">9</div>
    <div class="item">10</div>
</div>

And I have applied the following CSS styling:

.item {
    float: left;
    width: 10%;
    background-color: red;
}

The items labeled from 1 to 10 in each row occupy the full width of the screen and are displayed with a red background.

I have divided these items into 3 separate 'rows'.

To change the background color of the last item in each row to yellow, I used the following CSS code:

.item:nth-of-type(10n) {
    background-color: yellow;
}

However, when attempting to do the same for the first item in each row, the styling does not apply:

.item:nth-of-type(1n) {
    background-color: yellow;
}

Is there a way to give the first item in every row a different background color without modifying the HTML elements?

Check out the demo here

Answer №1

As interpreted, 1n signifies each and every one.

.child:nth-of-type(10n+1) {
     background-color: green;
}

ALTERNATIVELY

.child:first-child, .child:nth-of-type(10n) + .child {
     background-color: green;
}

Answer №2

To give cells different colors, such as (0, 11, 21, etc.), you can use the following CSS code. The "N" value will start from 0 and increment by 1 for each subsequent cell. This means coloring cells like (0*10 + 1), (1*10 + 1), (2*10 + 1) and so on. For more information on using nth-of-type in CSS, refer to this link.

 .child:nth-of-type(10n+1) {
  background-color: green;
 }

Check out the Fiddle Demo here

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

Tips for maintaining focus while tabbing in a contenteditable field?

Why does the table lose focus every time I press tab instead of inserting a white space? How can I change this so that pressing tab just inserts a regular tab space? ...

Dispense CSS using a React element

My React component index.js contains styling in style.css. I am looking to share this component on npm, but I am unsure of how to simplify the process for other developers to use the styling. After exploring different options, I have come across three ap ...

In what way can a programmer create HTML tailored to a designer's needs, even without a comprehensive grasp of

Currently, I am diving into my most ambitious website project yet. It's worth mentioning that I'm utilizing ASP.NET MVC 2 and the Microsoft stack. I value design and aesthetics greatly, knowing they can make or break the success of this endeavor ...

Trouble with CSS Positioning: Resizing my browser causes my image to shift in position

Upon resizing the browser window, my website layout adjusts accordingly and looks as intended at a smaller size: However, as I begin to expand the browser width, the centrally positioned image starts shifting towards the left. I aim for the image to remai ...

Experience seamless video playback on both mobile and web browsers with JW Player

I have integrated the JW Player for video playback on both mobile and web browsers. Currently, I have a library of 100 videos with dimensions set at 1280*700 pixels. These videos work well on web browsers and load quickly depending on internet speed. Howe ...

Is there a way to conceal the second td element?

This is the code I'm using: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> </head> <body style="width:100%;" > ...

Discovering an element using methods other than its ID, class name, or other identifiers

Here is an example of HTML code: <img src="/image.png" value="-" alt="collapse"> To select this element using Selenium, you can use the following XPath: driver.find_element_by_xpath('//img') You can also select elements by id or class_n ...

How to position a dropdown menu to the right of a textbox in a Bootstrap form

Seeking assistance with customizing my accordion. I have a flex-row textbox with icons on the right, and I want to incorporate a menu, as depicted in the image linked below: Accordion Image Below is the code snippet: <html> <head> <s ...

Expanding a JavaScript list using HTML inputs

I have a script where a grid dynamically displays a list of words. Is there a way to populate the word list in the grid using HTML? Currently, I am doing this... var listOfWords = ["mat", "cat", "dog", "pit", "pot", "fog"]; Can additional words be adde ...

What is the best way to duplicate several HTML input fields using jQuery?

My div is quite intricate with input fields structured like this <input type="text" name="firstname"> <input type="text" name="lastname"> <input type="text" name="email"> <input type="text" name="address"> <div id="section_toC ...

jQuery is mistakenly showing a width of zero

When attempting to determine the width of specific elements using jQuery's .width() function, I occasionally encounter a return value of 0 unexpectedly. Could this be a common problem? Is it possible that an element must be visible in order for this f ...

Is there a way to compel @keyframes to continue playing until it reaches its conclusion even after a mouseout event

Whenever I hover over an element, a keyframe animation starts playing. However, when I move the cursor away, the animation stops abruptly. Is there a way to ensure that it plays until the end? I've tried using the animationend event, but it doesn&apos ...

Turning select2 array output into a single string: a step-by-step guide

Is there a way to convert the output of select2 array into a single string separated by commas, like "task_owner":"Administrator,abc2", instead of having it as a single array like "task_owner":["Administrator","abc2"]? This is because the DB receives strin ...

Ways to receive notification during the user's selection of an option by hovering over it

Is there a way to receive an event when a user hovers over a select option? I thought that using the onMouseOver or onDragOver props on the option component would work, but it seems like they only trigger for the parent select component. Below is a simpli ...

Is there a way to hide the thumbnail image when the title is hovered over without using CSS?

I'm currently using a simple jQuery script to switch between two thumbnails when hovering over a div. The code works perfectly for the .thumbs class, but I also want this functionality to work with the post titles. Unfortunately, adding the .headline ...

Issue with D3: Events not triggering transitions

I am currently working on creating a bar chart using D3 in Angular4. // Enter Phase this.chart.selectAll('.bar') .data(this.barData) .enter() .append('rect') .attr('class', 'bar'); // Update Phase let bars ...

Having trouble importing font-face in scss

Currently, I am working on incorporating a Google font into my project. This is the desired outcome: https://i.sstatic.net/jIO7q.png However, the actual result is different: https://i.sstatic.net/V2KhW.png The code snippet in App.vue looks like this ...

Printing JSON data with multiple rows in HTML

How can I display multiple rows returned by JSON using my JavaScript function? function getScheduleDate() { //alert("enters1"); //var usrname= getParameterByName('uname'); var postVal=$.post('http://localhost/ipack/salesvisit.ph ...

The Send.php script is missing the input fields for Name and Email

Running my own website at CrystalVision.com, I have some challenges with my PHP skills. The issue lies within my send.php file. Although the email functionality works fine and I receive messages, the Name and Email fields filled in by users are not display ...

Turning off iPhone-Specific Features on Web Browsers

Is there a way to hide the elements marked as (1) and (2) in the image I have attached? Is it possible to disable the "select", "copy", and "replace" pop-up that appears when tapping on a text field twice using HTML5/CSS3 on an iPhone browser? Can the da ...