Choose the nth item in SCSS without utilizing recursion

Here is the structure that is causing the issue:

.parent-label {
  display: flex;
  flex: wrap;
  flex-direction: column;
  background-color: red;
  :nth-of-type(2) {
    background-color: green;
    padding-left: 30px;
  }
  .child-label {
     background-color: yellow;
     display: flex;
     flex-direction: column;
     justify-content: center;
     margin-top: 20px;
     margin-bottom: 20px;
  }
}
<div class="parent-label">
   <div class="child-label">child
   </div>
   <div class="child-label">
     <div>
        <span>test</span>
        <a href="test">click</a>
     </div>
     <div>
        <span>test</span>
        <a href="test">click</a>
     </div>
     <div>
        <span>test</span>
        <a href="test">click</a>
     </div>
   </div>
</div>

In this code snippet, there is a parent-label div that should contain 2 child-label divs.

The issue seems to be with the styling of the child-label, as it is not displaying the expected yellow background and the nth-of-type selector should turn it green.

Can anyone identify what might be wrong with the code?

Answer №1

The issue in the CSS code is due to misplacement of selectors. The :nth-of-type(2) selector and the .child-label selector should not be nested within the .parent-label selector.

Here is the corrected CSS:

.parent-label {
  display: flex;
  flex-flow: column wrap;
  background-color: red;
}

.child-label:nth-of-type(2) {
  background-color: green;
  padding-left: 30px;
}

.child-label {
  background-color: yellow;
  display: flex;
  flex-direction: column;
  justify-content: center;
  margin-top: 20px;
  margin-bottom: 20px;
}

You can view the updated code on JS Fiddle at this link: https://jsfiddle.net/cf3hkvtn/

IMPORTANT: Always review your code for any syntax errors before seeking help on platforms like Stack Overflow.

Answer №2

Your SCSS code implementation needs a small adjustment in the :nth-of-type(2) selector within the .child-label CSS class. Please review the revised snippet below for potential error correction.

.parent-label {
     display: flex;
     flex-wrap: wrap;
     flex-direction: column;
     background-color: red;

    .child-label {
     background-color: yellow;
     display: flex;
     flex-direction: column;
     justify-content: center;
     margin-top: 20px;
     margin-bottom: 20px;

        &:nth-of-type(2) {
            background-color: green;
            padding-left: 30px;
        }
    }
}

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

Stopping Defaults thwarts the ability to click on the entire document

When it comes to my website, I've set up a system where I swap out HTML content using AJAX when a visitor clicks on a link. Here's the code that handles this task: $(document).on('click','a', function (e){ e.preventDefault ...

Using PHP to iterate through an array and output the values within a

Hey there! I have a question about incorporating PHP foreach and echo into JavaScript to make it dynamic. Here is the current static JavaScript code: <script type="text/javascript"> $(document).ready(function(){ $('input[type="checkbox"]&ap ...

Adding items to a jCarousel using C# programming language is a simple process that involves following

I am struggling to display images in jcarousel using a C# Webmethod and jQuery Ajax. Here is my setup: My HTML structure: <div> <ul id="mycarousel" class="jcarousel-skin-tango" style="float: left"> </ul> </div ...

Double execution issue with jQuery radio button change event function

I have a simple function on my website that triggers when radio buttons are changed: $( 'form .radio_buttons' ).change( doTheFunction ) However, I recently noticed that the function is running twice whenever a change occurs. After some ponderin ...

Verify if the script has already been loaded. If not, proceed to load it asynchronously

I'm currently working on implementing an asynchronous method to load jQuery JS and then execute callback functions once it is fully loaded. I anticipate using this code repeatedly, so I want to ensure that jQuery (or any other script) is not duplicate ...

What is the proper way to reference an EJS function that is declared in a different EJS file?

document 1: jsfunction.ejs <% function testFunction() {return 42;} %> file 2: data.ejs <% include jsfunction.ejs %> <% testFunction(); %> result: ReferenceError: 1| <% include jsfunction.ejs %> >> 2| <% testFuncti ...

Is there a way to create a smooth transition for a background image, starting from grayscale and gradually transitioning to

I have a Bootstrap website featuring a colored background image. I am looking to achieve a specific effect where the image begins in grayscale and slowly transitions into color as the page loads, without requiring any user interaction. How can I accomplis ...

The Angular ng-repeat directive is populating a list with items, but the parameters enclosed in double curly braces

After calling an API, the response contains data in the following format: [{name: "ABC", age: 20}, {name: "DEF", age: 25}] Even though the $scope.names variable is assigned with the array from the API, when viewed in the browser, it displays 2 rows but th ...

Unable to align only one link on the right as flex-end is not functioning correctly

I need help rearranging my row of items displayed at the top of the page. I want one item to appear on the far right while the rest are aligned to the left. I attempted using align-self: flex-end but it didn't work. Here's the current code snippe ...

Simple way to extract the values from every input element within a specific <tr> tag using JQuery

Is there a way to align all input elements in a single row within my form? For example, consider the snippet of code provided below, which includes a checkbox and a text input box. I would like to retrieve the values from both of these input types and pres ...

Currently studying PHP. The classic "Hello world" message is not appearing when using EasyPHP and coding in Notepad++

<html> <!--HTML--> <head><title>a simple test</title></head> <body>a basic test</body> <p>javascript</p> <!--javascript--> <p><script> document.write("hello universe") </sc ...

Discover the secret to loading multiple Google charts simultaneously, despite the limitation that Google charts typically only allow one package to load at a time

I currently have a pie chart displaying smoothly on my webpage, but now I am looking to add a treemap as well. The code snippet for the treemap includes the package {'packages':['treemap']}. It has been stated that only one call should ...

The compatibility issue between jQuery popover and AngularJS causes functionality disruptions

One clever trick with jQuery popover involves populating a hidden div with our data and instructing the clicked button to show a popover with that hidden div as its content. However, when dealing with multiple divs, we may encounter issues with classes. ...

The compatibility of Cordova CSS properties varies across different devices

I am currently using the ionic framework to develop my upcoming app. I encountered an issue while trying to set a background SVG image for body. The background displayed perfectly on desktop browsers like Firefox and Chrome, as well as on my Nexus 4 device ...

Having trouble with customizing multiple DatePicker elements in jQuery UI

I have been customizing the jQuery UI DatePicker widget to match my own style. However, I am encountering difficulties when it comes to styling the title of the date picker: 1. The title currently displays the full month name with a capital letter, how ...

The weight of the Blender model is excessive

Seeking advice on optimizing loading times for my threeJS website. Currently experiencing long initial loading due to 4 models and textures. Check it out: Any suggestions on how to effectively reduce model size would be greatly appreciated! Thank you in ...

The transitionend event fails to trigger if there is no active transition taking place

Take a look at this: http://jsfiddle.net/jqs4yy0p/ JS $('div').addClass('switch').on('transitionend', function(e){ alert('end'); }); CSS div { background-color: red; /*transition: background-colo ...

eliminate gaps between hyperlinks using cascading style sheets

I developed a webapp for iOS which has a specific page containing a centered div with 3 buttons. Inside this div, there is a total of 460px with each link measuring as follows: the first and last are 153px while the middle one is 154px. The main issue ar ...

Having trouble extracting data from Moz Bar through selenium using Python?

Having trouble retrieving the spam score from Moz bar using Selenium WebDriver? I've attempted various methods such as XPath, class, and tag name without success. Any assistance would be greatly appreciated: from selenium.webdriver.common.by import By ...

AngularJS $HTTP service is a built-in service that makes

I am facing an issue with pulling the list of current streams from the API and iterating that information with AngularJS. I have tried inputting the JSON data directly into the js file, and it works fine with Angular. However, when I use an http request as ...