Can CSS identify a specific previous node?

I am trying to locate the previous sibling of each of my list items and change its color. Here is an example for reference:

    <ul>
        <li> 1
        <li>  2
        <ul>
            <li> 3
            <li> 4
            <ul>
                <li> 5
                <li> 6
            </ul>
         </ul>
         <li>  7
        <ul>
            <li> 8
            <li> 9
            <ul>
                <li> 10
                <li> 11
            </ul>
         </ul>
    </ul>

In the given scenario, I aim to apply CSS/SASS styles to nodes 2, 4, 7, and 9 without any element classes or scripting capabilities.

Answer №1

Arkellys made a good point by mentioning that there is no direct “previous sibling” selector in CSS/SASS.

However, you can utilize the + selector (adjacent sibling combinator) as an alternative. This might be helpful in your case where you want to target the second li element in each list.

https://developer.mozilla.org/en-US/docs/Web/CSS/Adjacent_sibling_combinator

ul > li {
  color: black;
}

ul > li + li {
  color: red;
}
<ul>
    <li>1</li>
    <li>2
        <ul>
            <li>3</li>
            <li>4</li>
            <ul>
                <li>5</li>
                <li>6</li>
            </ul>
        </ul>
    </li>
    <li>7
        <ul>
            <li>8</li>
            <li>9
                <ul>
                    <li>10</li>
                    <li>11</li>
                </ul>
            </li>
        </ul>
    </li>
</ul>

Answer №2

To apply custom styling, try this code snippet:


ul>li{
    color:black;
    &+li{
        color:red;
    }
    &>&>&{
        color:black;
    }
}

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

Unable to load JQuery from a div element

My goal is to create a standard .html file containing the navigation, footer, and other elements that will be used across multiple pages for a small site I'm building. I want to keep it simple and avoid using php or other programming languages. I&apo ...

Use Jquery to select the checkbox inside the td element and mark it as

I've created an HTML table with checkboxes in the td elements, like so: <table id="my_table"> <tr> <td><input type="checkbox" name="td1"></td> <td><input type="checkbox" name="td2"></td> </ ...

How can I ensure the end of the list is clearly visible?

I'm experiencing an issue where the last element in my list is getting cut off. When I check the console logs, I can see that it's rendering properly. If I remove the 'position: fixed' style, I can see the element at the bottom of the l ...

Is there a way to implement word wrapping in li text without making any changes to its width

Is there a method to wrap the content inside li elements without modifying their width? As an illustration, consider the following structure - <ul> <li>Text Example</li> <li>Text Example</li> <li>Text Exampl ...

Fading text that gradually vanishes depending on the viewport width... with ellipses!

I currently have a two-item unordered list positioned absolutely to the top right of the viewport. <header id="top-bar"> <ul> <li> <a href="#">David Bowie</a> </li> <li> ...

Tips for resolving vertical alignment styling for images of varying sizes within Vue.js transition-group?

I have created an image slider example, but I'm facing a styling issue when using images of different dimensions. The element leaving the array is positioned absolutely for smooth transitions, but this causes the image to move up. I am trying to vert ...

Slideshow through each item using Typescript and Angular8

I came across a solution in this carousel on by one link, but I'm struggling to work with the jQuery code even though I have JQuery installed in my project. For example: const next = jQuery(this).next(); I am looking to convert the JQuery code from ...

Exploring the benefits of incorporating the vendor folder in website development

I'm embarking on the journey of creating a website from scratch with python, django, and bootstrap. I've noticed that it's common practice to store js, css, img, and fonts in a folder named vendor, like this: /static/js/vendor/bootstrap/boo ...

Guide to swapping images based on conditions using Angular JS

I am trying to display an image based on data received from an API response instead of text. If the value is true, I want to show an access symbol. If the value is false, I want to show a deny symbol. However, when attempting this, I am only getting th ...

What is the best way to dynamically update the selected option in a dropdown menu within a Rails application using a variable?

Working on a project that involves a select drop-down menu containing a list of currencies. Want to enhance user experience by automatically selecting the default value in the dropdown based on the user's country (will be utilizing the geoip gem). To ...

What is the best way to trigger ajax when the user clicks the back button to return to the previous webpage?

Check out my code snippet below: HTML Code <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="body"> <div class="dropdown_div"> <select id="q_type" class="dropdown" ...

Is it possible to globally modify the component reference <dropdown-component> name in Angular during runtime in a dynamic manner?

I am currently working on an application that utilizes a component called "dropdown-component" throughout its pages. However, due to specific business requirements, I have been tasked with replacing "dropdown-component" with "custom-dropdown-component". Un ...

Navigating through parent folder HTML files from child folder HTML files

Seeking guidance as I embark on a new project! Check out this link to see my skills in action: collegewebsite.zip Now for the query at hand. I am working on a project named Coffee Cafe for my tuition assignment. It involves utilizing CSS and HTML with J ...

Show the text area content in an alert when using Code Mirror

When I try to alert the content of a textarea that is being used as a Code Mirror on a button click, it appears blank. However, when I remove the script for Code Mirror, the content displays properly. I am puzzled about what could be causing this issue wi ...

Automatically press the button when the display style is set to block

Hello everyone, I am completely inexperienced with this and I had a question - is there a way to automatically click a button when using display:block in styling? I would greatly appreciate it if someone could guide me in the right direction. <div i ...

Every time I try to access my website, all I see is the WordPress installation process page

After initially hosting a WordPress website, I made the decision to switch over to SPIP. However, when attempting to access the site on my laptop, it continues to bring up the WordPress installation process. Interestingly enough, the website appears to lo ...

What is the best way to implement validation in a textarea to restrict the word count to a minimum of 250 and a maximum

I want to implement jQuery validation for a textarea field, with a requirement of 250 minimum words and 1000 maximum words. Additionally, I need to display the word count in a span tag next to the text area. How can I ensure that this validation works even ...

Exploring scraping capabilities in Node.js

Currently, I am in the process of developing a scraping engine using Node.js to collect data for my currency exchange graphs. I am utilizing request and cheerio for this task. However, due to some bank websites not following standard HTML practices by not ...

Why must I handle Access-Control-Allow-Origin issues with XMLHttpRequest() but not with forms?

When attempting to make a request using XMLHttpRequest(), I encounter the Access-Control-Allow-Origin error. However, when I use a form like the one below, no issues arise. Is there something about forms that allows access? What could be the reason for t ...

What is the best way to make a CSS class appear in my javascript using the method I have been using before?

I am facing an issue in making this code work properly. It functions correctly for my caption element as there is only one caption tag in my HTML. However, the same code does not work for my TR element since it requires a for loop to iterate through multip ...