Setting up the nth-of-type() function to dynamically target elements in sequence

I need to apply specific CSS styles to elements in a certain order within an image carousel. The carousel may contain any number of images, but only three images are displayed at a time. I want to style the first and third images differently from the second image.

Below is a simplified version of the code I am trying to achieve. I require a dynamic solution as the number of images in the carousel is unknown. Is there a formula, such as nth-of-type(4n-7), that can be used to target this order dynamically?

li:nth-of-type(1),
li:nth-of-type(3),
li:nth-of-type(4),
li:nth-of-type(6),
li:nth-of-type(7),
li:nth-of-type(9) {
  color: red;
}


li:nth-of-type(2),
li:nth-of-type(5),
li:nth-of-type(8) {
  color: blue;
}

Answer №1

To extract every third image/item from your text, starting with the 2nd one, you can use the following selector: element:nth-of-type(3n+2)

li:nth-of-type(3n+2) {
  color: red;
}

li:nth-of-type(3n) { /* for demo only */
  margin-bottom: 1em;
}
<ul>
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
  <li>Item 4</li>
  <li>Item 5</li>
  <li>Item 6</li>
  <li>Item 7</li>
  <li>Item 8</li>
  <li>Item 9</li>
  <li>Item 10</li>
  <li>Item 11</li>
  <li>Item 12</li>
  <li>Item 13</li>
  <li>Item 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

The footers on each page are not consistent

I have noticed that two pages utilizing the same CSS are displaying differently. Check them out here: 128.48.204.195:3000 128.48.204.195:3000/formats If you look closely, you'll see that below the footer, the /formats page appears to have no extra s ...

Using Javascript to generate a button that randomly chooses links within the webpage

Looking for help with JavaScript to select a link when a button is clicked on the page? Check out my code and let me know what I'm doing wrong. For the full code, visit: here HTML <!doctype html> <html lang="it" xmlns="http:/ ...

Ensure the top section of the page remains fixed in place

Looking to keep the header section of my webpage sticky at the top while allowing the content section to scroll over it? I need a solution using JavaScript or jQuery that will achieve this effect. Here is the HTML structure: <div id="header"> & ...

Issues arise when trying to implement Angular SCSS with ngx-datatable

I am encountering an issue with my SCSS. It seems that the CSS command from the SCSS file below is not being utilized. This particular SCSS file is generated with the component. Interestingly, when I place the code in the styles.scss file included in the ...

Block of white colors located on the right side beyond the confines of an HTML structure

I am encountering an issue where a white block is appearing outside the HTML on my page. Despite having no content, Firebug indicates that it is located outside the html tag. I suspect the problem may be related to an image that is too wide or padding exc ...

Having trouble aligning material-ui GridList in the center

I am currently working with a GridList in order to display Cards. The styling for these components is set up as shown below: card.js const useStyles = makeStyles({ card: { maxWidth: 240, margin: 10 }, media: { heigh ...

Expanding the navigation bar causes it to reach the second row after being translated

I am currently working on a website that will be dynamically displayed in both English and Spanish. One issue I have encountered is that the longer words in Spanish are causing the navigation bar to spill onto a second row. For more details, please visit: ...

Achieving a transparent background color for a div without affecting the content with CSS

I am attempting to design a div element with a basic background color and some text displayed on it. I would like to lower the opacity of the background color in this div, but every time I try, the opacity of the text also changes. Is there a way to adjust ...

Set a CSS rule to style every other row in a table, starting from the second row and resetting after

Is there a way to refresh the even and odd count in CSS whenever a specific row is shown? Here's an example setup: header header header even even even odd odd odd Subhead Subhead Subhead even even even How can I ensu ...

Is your 100% height not beginning at the top of the page?

The link provided showcases a tutorial that has a white column extending to the bottom of the browser. In this example, the white column begins from the very top, with a header in a different shade of grey covering it. In my scenario, I need the header to ...

Strange Interaction with Nested Divs in CSS Layouts

I am working on a webpage layout and currently it looks like this: https://i.sstatic.net/s4TOb.jpg I am trying to align the purple box inline with the pink box inside the yellow box, which is inside the green box. However, when I change the display proper ...

Is there a way to align the mat card title and subtitle on a single line?

My code is utilizing Angular material and here is the HTML snippet: <mat-card> <mat-card-header *ngFor="let club of result[0]"> <mat-card-title>{{club.clubName}}</mat-card-title> <mat-card-subtitle>Clu ...

Troubleshooting problems with Bootstrap on tablets: responsiveness for medium-sized screens

I am having trouble with a responsive design using Bootstrap 3. The navbar looks fine on mobile and desktop screens, but breaks on tablets. Here are the views for mobile, tablet, and desktop: The tablet view is broken, while the mobile and desktop views ...

Modify the color of a unique word within a cell in a gridview

How can I change the color of specific keywords in a gridview cell without affecting all words? Here is the sample code: protected void gvContents_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) ...

I noticed that my CSS style sheet is only working on three of my pages. What should I do to make sure it affects the fourth page as well?

I am currently facing an issue with my website where I have linked one .css stylesheet to 3 different pages. This is just a temporary template until I finalize the individual look of each page. After performing some div positioning on the site, most pages ...

Straightening the border of the final item

In my Angular project, I'm currently implementing a progress bar with the following code: .bar { --d: 1rem; /* arrow depth */ --gap: 0.3rem; /* arrow thickness, gap */ display: flex; margin-right: var(--d); } .bar-step { flex: 1; ...

Issues Arise with Bootstrap Side Navigation Tab Content Navigation

As someone new to web development, I am currently working on a web app and struggling with the page design. Specifically, I am attempting to create a fixed side navbar with content displayed in the second column of a split row. Below is the code I have man ...

Waiting for all promises to resolve: A step-by-step guide

I need to make two different API calls and perform calculations based on the results of both. To wait for both promises to resolve, I am using Promise.all(). const getHashTagList = async () => { loader.start(); try { await getAllHashTag ...

The vertical alignment of the wrapper div does not support multiple lines of text

I am having trouble achieving vertical alignment (middle) for a div with 2 lines of text. I can do it with one line, but not two. I want one of the lines to be formatted like a h2 heading, and the other like a regular paragraph. I understand that I could s ...

Background in CSS ::after pseudo-element not showing up

I've been attempting to show a tick image after a div element, but no matter what CSS properties I try, I can't seem to get it right. Previous answers to similar questions haven't provided the solution I need. HTML: <div class="link-in ...