Troubining CSS nth-of-type or child selectors with custom elements within additional custom elements may cause unexpected behavior

Struggling with CSS :nth-child and creating a unique layout with custom elements inside custom elements. The issue arises when trying to display different icons using the :before pseudo-element, but all icons end up being the same. Below is the code snippet:

Attempt 1: Using nth-of-type(number) in CSS

custom-div:nth-of-type(1):before {content:url('1.svg');}
custom-div:nth-of-type(2):before {content:url('2.svg');}
custom-div:nth-of-type(3):before {content:url('3.svg');}

Second attempt: Using child(number) in CSS

custom-div:nth-of-type(1):before {content:url('1.svg');}
custom-div:nth-of-type(2):before {content:url('2.svg');}
custom-div:nth-of-type(3):before {content:url('3.svg');}

Provided HTML structure:

<another-div><custom-div> text 1 </custom-div></another-div>
<another-div><custom-div> text 2 </custom-div></another-div>
<another-div><custom-div> text 3 </custom-div></another-div>

The desired outcome is to have distinct icons for each custom-div, however, regardless of the method applied, the result remains the same - displaying icon '1.svg' across all three custom divs.

UPDATE: It should be noted that custom-div is nested within another-div, both utilizing custom element names.

Answer №1

I discovered an alternative method in addition to @TanishqS.

One can utilize the custom-div as follows:

another-div:nth-of-type(1) custom-div::before { content: url("https://placehold.co/50"); }
another-div:nth-of-type(2) custom-div::before { content: url("https://placehold.co/80"); }
another-div:nth-of-type(3) custom-div::before { content: url("https://placehold.co/100"); }
<another-div><custom-div> text 1 </custom-div></another-div>
<another-div><custom-div> text 2 </custom-div></another-div>
<another-div><custom-div> text 3 </custom-div></another-div>

Answer №2

The issue lies in applying the :nth-of-type selector to the child component (custom-div) instead of the parent component (another-div)

To resolve this, try the following:

another-div:nth-of-type(1) custom-div::before {
content: url("https://img.cdn4dd.com/s/media/photosV2/1954ba82-d91f-43a2-8838-767fd2ad386f-retina-large.svg");
}

another-div:nth-of-type(2) custom-div::before {
content: url("https://img.cdn4dd.com/s/media/photosV2/edf12308-afd7-427e-b4d4-7e78c3efce01-retina-large.svg");
}

another-div:nth-of-type(3) custom-div::before {
content: url("https://img.cdn4dd.com/s/media/photosV2/0d63d52b-ff6f-4c39-8b52-f6c605ce38dc-retina-large.svg");
}
<another-div>
  <custom-div> text 1 </custom-div>
</another-div>
<another-div>
  <custom-div> text 2 </custom-div>
</another-div>
<another-div>
  <custom-div> text 3 </custom-div>
</another-div>

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

Exclude the HTML attribute prior to displaying

Is there a way to prevent the src attribute from being used for each img tag until after a certain action is completed? I am trying to modify how images are fetched. Here's what I tried: $('img').each(function() { var elem = $(this); ...

"jQuery's .each() method is only iterating through the last element in

I am encountering an issue with this function not operating correctly... only the last Element shows the box. NOTES: <aside> is set to position: fixed; and I understand that this is not the "correct" use of <article> tags, but it helps me dist ...

Graphic background integrated into form input

Is it advisable to create colored shapes in Illustrator, export them as SVG files, and then embed input tags or textareas within these shapes to allow users to enter text? Are there alternative methods we could use for this functionality? https://i.sstat ...

a pair of radio buttons accompanied by a single submenu

Is there a way to extract the following code snippet from this block of HTML? <input type="radio" name="one" checked="checked" id="1"/> <label for="1">1</label> <input type="radio" name="one" id="2"/> <label for="2">2< ...

Unable to completely conceal the borders of Material UI Cards

Despite my efforts to blend the card with the background, I am still struggling with the tiny exposed corners. I've spent hours searching for a solution, but nothing seems to work. I've tried various methods such as adjusting the border radius in ...

How can I center a Bootstrap modal vertically and make it responsive?

Is there a way to vertically center the bootstrap modal? I have been searching for a solution, but none seem to be responsive or effective. My website is using Bootstrap 3. The modal does not adjust properly on smaller screens or when the browser window i ...

Interactive user-friendly form with real-time response features

I am currently working on an application form where users need to answer questions, and I am in need of some responsiveness. Specifically, I would like to implement a feature that shows additional fields based on the user's selection. For example, if ...

UI-Select fails to display the already selected value

Does anyone have a solution for binding a UI-Select control to a Class[] list? The filling up part works fine, but I'm having trouble getting the selected item to display. Any suggestions on how to fix this? Below is my ui-select code: <ui-select ...

HTML dynamic voting system

I'm new to coding in HTML and I have a specific challenge that I need help with. I want to display a value on my webpage that increases every time a user clicks a button. Below is the code I have written so far: <script type="text/javascript& ...

Displaying PHP processes within a DIV instantly using JQUERY, without the need to wait for them to complete

I currently have two scripts that I am working with. My goal is to use jQuery to send a form to a PHP file and display the process in a DIV. However, there is a delay in showing the progress until it completes, which can sometimes take a few seconds, caus ...

Establish the Image Aspect Ratio within an Unordered List

I'm having some trouble adjusting the aspect ratio of an image within a ul. Currently, the image is displaying too large in terms of height and I've tried various solutions, such as wrapping it in another div and applying padding-top: 56.25%; to ...

What is the specified height for the AppBar and Toolbar components in Material-UI?

I'm trying to figure out the exact height being used in this scenario: <AppBar position="static"> <Toolbar> because I'll need that information for a calculation in another component. My current assumption is that it's 64px, b ...

The JavaScript linked list causes the program to come to a halt

I am currently working on developing a simple web page embedded program using JavaScript. One of the tasks I am tackling involves creating a linked list of all active buttons displayed on the screen at any given time. However, I have encountered an issue w ...

Creating a Cancel Button in JSP/HTML/JS/CSS Form: A Step-by-Step Guide

It is necessary to incorporate the functionality of "save" and "cancel" in the JSP code for this particular form. By selecting "save", the data entered into the form will be submitted to its intended destination. Alternatively, choosing "cancel" will dismi ...

Stop CSS Grid cell from causing vertical expansion of cells in adjacent columns

I believe that visual aids are more effective than text in illustrating the issue: My Objective https://i.sstatic.net/vBAsI.png Current Situation Looking at the example, you can see that the title and description expand equally to accommodate the gallery ...

Using HTML and JavaScript, we can set two different color values - one for the background and one for the h1 title

I am trying to save two values, one for the h1 tag and one for the body background. I want the user to select color 1 and color 2. When I press the third button, all changes should be applied and the colors should change. I have attempted this but with no ...

Having Numerous HTML Tables All in One Page Accompanied by Written Content

I used to have a webpage with multiple (4) HTML tables all contained within one div that spanned the entire page, serving only as an indicator for the tables' starting point. It all worked perfectly until a recent Chrome update caused chaos with the t ...

Submitting forms with Ajax in IE(8)

Sample Google form Related spreadsheet I modified the original code to create two custom forms: First created form Second created form Both forms are functional on most browsers except for IE(8). Any idea why? First form: <!DOCTYPE html> <h ...

Having trouble with the rendering of the Stripe Element Quickstart example

Currently, I am diving into the world of Stripe's Element Quickstart. Take a look at this fiddle that I have been working on. It seems to be quite different from the example provided. Although I have included the file, I can't seem to figure out ...

How can I apply a unique CSS class to specific rows within an h:dataTable?

I am looking to apply unique CSS classes to certain rows within my <h:dataTable>. Is it possible to manipulate and style the individual table rows in a customized manner? ...