Modifying the nth-child selection technique

I've been searching through multiple blogs without finding a solution to my issue. Perhaps you can help me out.

In the first ul (with class .first), I need to apply a different background to each li. To achieve this, I attempted to use pseudo-elements for learning purposes. I adjusted the selector numbers for each li element and utilized :after to move the image background with an absolute position. Here is the CSS code snippet:

> ul li:nth-child(1) a:hover:after
> ul li:nth-child(2) a:hover:after
etc

To add complexity, the second ul (with class .dropdown) should have a distinct background from the li elements in ul.first. However, all li items in ul.dropdown should share the same background. When I added a background, the problem arose - it displayed the background of ul.first. I want the CSS to specifically target the li elements of ul.first, allowing me to separately style ul.dropdown.

This is the HTML structure:

<nav>
    <a href="index.html">
        <h1 class="sprites-logo- ir">cepods</h1>
    </a>
    <ul class="first">
        <li>
            <a href="#">about</a>
        </li>
        <li>
            <a href="#">how we work</a>
        </li>
        <li class="dropli">
            <a href="#">design<i class="icon-right-open"></i></a>
            <ul class="dropdown">
                <li>
                    <a href="#">restaurant</a>
                </li>
                <li>
                    <a href="#">retail</a>
                </li>
                <li>
                    <a href="#">event space</a>
                </li>
                <li>
                    <a href="#">bar</a>
                </li>
                <li>
                    <a href="#">living</a>
                </li>
                <li>
                    <a href="#">hotel</a>
                </li>
            </ul>
        </li>
        <li>
             <a href="#">news</a>
        </li>
        <li>
             <a href="#">faq</a>
        </li>
    </ul>
</nav>

Answer №1

The beginning of your selector is:

ul li

This will target all li elements within the ul. Since the dropdown items are also within the first ul, they will be affected by this rule as well.

If you only want to select the direct children of the first ul, you should use

ul > li

This selects only the li elements that are immediate children of the ul.

However, since "dropdown" is also a ul element, the above rule would still apply to both. In order to specifically target the first ul and its direct children, you can do:

.first > li:nth-child(1) a:hover:after
etc..

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

Unlocking the Secrets of CssSelector Language

I've encountered an issue: unable to find element: {"method":"css selector","selector":".ftMtypSt-Primary > span"} This code is coming from the FireFox Developer Selenium IDE and exported to Python. driver.find_element(By.CSS_SELECTOR, ".ftMs-inpu ...

Leveraging jQuery to extract the value from a concealed form field

Seeking assistance with a jQuery issue... I am attempting to use jQuery to retrieve the values of hidden fields in a form. The problem I am facing is that there are multiple forms displayed on the same page (result set items for updating), and the jQuery ...

Issue with border styling in Material-UI vertical ToggleButtonGroup

In my current front-end project, I have implemented the React Material UI ToggleButtonGroup. For a Multiple Selection example with vertical styling, you can refer to this code snippet: https://codesandbox.io/s/eyk66?file=/demo.js However, I encountered a ...

In ReactJs, you can use break tags to format a lengthy string and also apply color to certain words within the string

When working with ReactJs, my objective is to format a lengthy string by inserting <br/>. I was able to achieve this using a JavaScript function as shown below: The long string in question: export const fadeAnimation = { \n initial: {opacity: ...

unable to successfully execute jQuery popup close functionality

I have a button on my mobile site that I want to function as follows: Upon clicking the button, a popup should appear with text and an OK button. When the OK button is clicked, the popup should disappear without affecting the page. My code for this functi ...

Verifying the textbox for emptiness still shows as empty despite entering text in my PHP/HTML code

This is the form I created in HTML: <form action="Belo_Connect2.php" method="POST"> <center> Name:<input type="text" name="namefordelete"> <p> //button that submits to php file &l ...

Checking the length of user input with JavaScript

To ensure that the user enters at least 4 letters in a text box and show an error if they don't, I need help with the following code. It is partially working but currently allows submission even after showing the error message. I want to prevent subm ...

Navigational Dropdown implemented on Bootstrap 4 Navigation Bar

I'm encountering a problem with aligning a dropdown menu on the navbar-brand section of the Bootstrap 4 Navbar. Instead of being in the correct position, it's showing up at the top left corner of the page. https://i.sstatic.net/xU6Wc.png I&apos ...

The submission of the form is not functioning properly after loading the page using AJAX

Having trouble submitting my form after loading the page with ajax. Here is the code I am using to load the page: The problem occurs after loading the ajax form as a modalbox. function account_list(url) { $.ajax({ type: 'get', url: url, befor ...

Add CSS styling to a section of a canvas

I want to use a specific cursor png for just a portion of a canvas. Currently, I have code that applies the cursor to the entire canvas like so: .myClass { cursor: url('../img/myCursor.png') 7 33, auto; /* img hotspot centred*/ } However, I ...

Enhancing accessibility: the use of sr-only or aria-label

According to MDN: In this scenario, a button is designed to resemble a standard "close" button, featuring an X in the center. Since there is no visual cue indicating that the button closes a dialog, the aria-label attribute is utilized to provide this i ...

Retrieve data stored in an array from the complete HTML document retrieved by an XMLHttpRequest

Here is a snippet of JavaScript code I am working with: var req = new XMLHttpRequest(); req.onreadystatechange = function() { if (req.readyState === 4) { var response = req.responseText; console.log(response); } }; req.open('G ...

Style table cells dynamically based on their content in HTML

As a complete beginner in this field, I am currently working on a simple xslt presentation file that includes a table. I want to customize the background color of either a cell or an entire row based on its content. For example, if the rating is "GOOD", " ...

Adding various image files to the canvas

I am facing an issue with my code where I need to find images inserted by the user into a div and then combine them into one image in a canvas when the "snap" button is clicked. While I can successfully locate, position, and resize the images inside the ca ...

Bootstrap cards that mimic a horizontal scroll but are actually static and

As I work on creating my website, I'm aiming to achieve a similar effect as shown in this example: https://codepen.io/GreenSock/pen/NWYxBKO, but using bootstrap cards instead of images. Here is the code for the bootstrap card: <div class="car ...

Vertically Center Image in a Div

I have been struggling to center an image within a div, but all the usual methods like using margin: auto; do not seem to work. I have tried various popular techniques with no success. <style> /* Add a black background color to the top navigation ...

Restricting the height of the grid list in Vuetify

Lately, I've been working with Vue.js and the Vuetify framework, and I encountered a challenge that has me stumped. In certain instances, I need to present data in a grid layout using the vuetify grid component. After examining the code, I decided t ...

Generate div elements with a row capacity of 3 and a column size of N

I am trying to arrange divs in a way that they can be scrolled horizontally. I have set up a flex container, but I'm unsure what other steps are needed to ensure the boxes align to the left. You can view the demo code here .container { height: 95px ...

Using table-responsive causes a modal to break inside because of overflow-scrolling touch in the CSS

When implementing a modal within a table that is inside a table-responsive container, the modal appears behind the table. To address this issue, I have tried using table-backdrop="false", but it is not an ideal solution as it restricts the full functional ...

Properly aligning text with checkboxes using HTML/CSS and tags like <span> or <div>

My goal is to have the text displayed as a block in alignment with the checkbox, adjusting based on the sidebar's width. For reference: Current Layout Preferred Layout I have shared the code on CodePen (taking into account screen resolution and wi ...