Prevent the cascading of CSS styles on child list items

When constructing my menu, I have organized it using the following HTML structure:

<div id=”navigation”>
<ul>
    <li class=”level1”>Top Level Menu item
        <div class=”sublevel”>    
            <ul>
                <li class=”level2”>Second Level Item1
                    <ul>
                        <li class=”level3”>Third Level Item1</li>
                        <li class=”level3”>Third Level Item2</li>
                        <li class=”level3”>Third Level Item3</li>
                    </ul>
                </li>
<li class=”level2”>Second Level Item2
                    <ul>
                        <li class=”level3”>Third Level Item4</li>
                        <li class=”level3”>Third Level Item5</li>
                        <li class=”level3”>Third Level Item6</li>
                    </ul>
                </li>
            </ul>
        </div>
    </li>
</ul>
</div>

I have applied a CSS style to the level 2 class as shown below:

#navigation .level1 ul li {
background: #acacac;
padding-bottom: 40px !important;
border-radius: 10px;
position: absolute;
}

The challenge arises when this styling is inherited by all child menu items. How do I go about targeting only the level2 items and exclude the level3 ones?

Despite consulting CSS documentation and attempting different combinations of > and +, I have not been successful in resolving this issue.

Answer №1

It's easy, just utilize the :not() selector

#menu .main ul li:not(.sub) {
  background: #acacac;
  padding-bottom: 40px !important;
  border-radius: 10px;
  position: absolute;
}

For more information on :not(), visit MDN

The negative CSS pseudo-class, :not(X), is a functional notation that requires a simple selector X as an argument. It selects an element that does not match the argument. X should not contain another negation selector or any pseudo-elements.

Answer №2

To fully optimize your code, modify the selector to incorporate the direct child operator:

#navigation .level1 .sublevel > ul > li {

Answer №3

Give this a shot:

#menu .first-level .sub-level li {
background-color: #acacac;
padding-bottom: 40px !important;
border-radius: 10px;
position: absolute;
}

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

Shopify's feature prevents users from entering negative numbers in the quantity field

Struggling with Shopify coding, specifically the issue of negative numbers being accepted as input in my Shopify ajax setup. I've implemented a solution to reload the page if the quantity is less than 1, but ideally, I want to restrict the input to a ...

Distribute the elements evenly between two separate divs

Hello everyone! I have a unique challenge that I hope someone can help me with. I have a list of links to various tests, but different sections require these links to be displayed in different ways. One section needs them spread over two columns, while an ...

jquery-tokeninput not populating field with content

I have set up a form where users can create a profile and select keywords from an autocomplete token field using jQuery tokenInput. However, I'm facing an issue where the text in the token field is not getting submitted when the form is sent. I am usi ...

An efficient method for extracting data from a JSON array and integrating it into a DataTables display

I am struggling to populate data tables with values from a PHP file that returns JSON responses. Despite receiving the JSON response, I am unable to successfully append the data to the data table. Below is the code snippet used for generating the JSON resp ...

"Utilizing jQuery to Connect with Google's Services

I'm trying to figure out how this Google code works. Here's what I have: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> < ...

Supported video formats in various web browsers

Are there detailed records of video codec compatibility across various desktop and mobile web browsers? ...

Is it possible to constrain jQuery Draggable elements within the boundaries of the

I need to restrict my draggable element so that it stays within the viewable window. It works fine when the user is at the top of the page, but as soon as you scroll down, it gets messed up. Do you know how I can fix this issue? $(".chat-wrapper > li. ...

Swapping link positions with jQuery

Is it possible to rearrange links using jQuery? Under the navigation menu, there will be content div for each tab. The selected link should always be positioned next to the first "sixrevision" link. The code snippet is shown below: <ul id="crumbs" ...

What are some creative ways to animate a highlight effect on a CSS

I'm struggling to implement a sliding underline effect for my top navigation bar on hover. I've tried using transitions and keyframes, but so far it's not working as expected. My current attempt only triggers the effect on the parent elemen ...

Creating multi-level nested lists with numbering using HTML and CSS

Is there a way to create a unique numbering format using only HTML and CSS lists (<ul> or <ol>)? 1. Item A 2. Item B 3. Item C 3.1. Subitem C.1 3.2. Subitem C.2 3.3. Subitem C.3 4. Item D 4.1. Subitem D.1 4.1.1 Sub-subi ...

Developing a PHP foreach loop that integrates seamlessly with a W3 responsive grid

Is there a way to generate multiple sets of three columns using w3.css and a foreach loop to fill each set with data from a sample database? Attempted code resulted in all the columns being in a single row: <?php foreach ($products as $index => $pro ...

Working with CSS styles for the <datalist> element

I currently have a basic datalist drop-down menu and I am looking to customize the CSS to match the style of other fields on my website. However, as someone new to web design, I am unsure of how to go about this process. Check out the code here: http://j ...

What is the best way to save and retain cache data for an audio file?

I have a website where I am using an audio file in .swf format. This audio file can be turned on and off with the options provided: AUDIO ON and AUDIO OFF. However, I am facing the issue that the audio keeps playing repeatedly even after being turned off. ...

What is the best way to transfer data from one function to another file in React without directly calling the component or using props?

filename - Header.jsx import { useEffect, useState } from 'react' import { getValue } from './Input' import TextField from '@mui/material/TextField'; export const Header = () => { const [search, setSearch] = useState( ...

Confused in the world of .net and jQuery

I've come across similar questions on this topic before, and there are several resources available. However, I'm still feeling pretty lost and unsure about the next steps. My situation involves a textarea that contains data I need to send to a s ...

Is there a way to encase numerous <tbody> elements together?

My table design has specific css requirements where I am using multiple <tbody> tags. It looks like this: Example with multiple tbody tags However, I also need a wrapper for the multiple tbody tags (similar to a common tbody parent) that can be scr ...

Tips for Changing Background Color Beyond Body Tag

When using my demo below on Safari for iOS and scrolling up and down, you'll notice that you can scroll outside of the body until removing your finger from the touchscreen. The background color outside of the body is white. Is there a way to change th ...

Step-by-step guide on achieving a radiant glow effect using React Native

I am looking to add a glowing animation effect to both my button and image elements in React Native. Is there a specific animation technique or library that can help achieve this effect? While I have this CSS style for the glow effect, I am uncertain if ...

The ReactJS script encountered an error at line 2 of the jquery.min.js file, displaying an unrecognized expression syntax error with the hashtag

I have encountered an issue with React-route-dom Link while using it in the Navbar. The following error is displayed: Uncaught Error: Syntax error, unrecognized expression: #/login at Function.ga.error (jquery.min.js:2) at ga.tokenize (jquery.min. ...

Utilizing jQuery and Isotope for intricate filtering

My isotope instance contains elements with multiple parameters, as shown below: <element data-a="1 2 3 4 5" data-b="1 2 3 4 5" data-c="1 2 3 4 5" Filtering for an element that has A1, B2, and C3 is straightforward: .isotope({ filter: '[data-a~=1 ...