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

The execution of jQuery Ajax requests with "when" is encountering issues

My goal is to initiate 8 ajax GET requests N number of times, and this is the code I have written: var me = this; urls.forEach(function(requests){ $.when(requests.map(function(request){ return $.ajax(request) })).done(function(data){ me.result ...

Eliminating null values from arrays

Hello there! I'm currently working on inserting a list of locations into my database. $locations = array(); $locations = $_POST['loc']; $loc = implode(", ", $locations); However, when I insert the array into my database, the empt ...

Troubles encountered when converting JSON data into JSONP

I'm having trouble getting data in jsonp format for use with phonegap. The code seems to be not working on the web browser and there are no errors showing up in the console. It's clear that something must be wrong with the code, but since I have ...

AngularJS Error: The method serviceName.functionName() is not a valid function

I am trying to implement a function that will go back when the cancel button is clicked. Here is the view code: <div ng-controller="goodCtrl"> <button class="btn" ng-click="cancel()">Cancel</button> </div> And here is the Jav ...

Element was removed upon clicking only once

Can anyone help me figure out why the behavior of .remove() with $postNav.remove(); is different here? When you click on "I'm a tag" for the first time, both the <li> and <ol> are deleted as expected. However, on the second click, only the ...

Are you struggling to get basic HTML and JS code to function properly?

I'm currently working on developing a racing game, and my initial step was to create the car and implement movement functionality. However, I've encountered an issue where nothing is displaying on the canvas - neither the rectangle nor the car it ...

How can you create a dynamic bounce effect for text with jquery animate()?

I've been experimenting with Jquery to achieve a bounce effect, here's what I have so far: Html: <div id ="animation">bounce</div> Jquery: $("#animation").animate({ marginTop: "80px" }, 1500 ) .animate({ marginBotto ...

Steps for compiling SCSS to CSS using Angular-CLI and moving it to the assets directory

I am currently working on an Angular 5 project with an assets folder where I store my common CSS file and reference it in index.html. I now plan to create a new folder called "sasstyles" and place some .scss files there. When I compile or run the project ...

Utilize Node.js to retrieve the content from a website's body and display it using jQuery

I am attempting to achieve the following goal: Creating an endpoint in Express.js named /content.json. Upon calling this endpoint, I aim to display the HTML content within a label. The server code I have implemented is as follows: var http = require(&ap ...

Here is a guide on how to develop a PHP function that interacts with a textarea to display text in the specified color by using syntax like [color:red]

Is it possible to code a PHP function that can work alongside a textarea to display text in the specified color by using a syntax like [color:red]? This function operates in a similar manner to Facebook's @[profile_id:0] feature. ...

What could be causing my tab code to not function flawlessly?

I am attempting to implement a tab concept on my website. For example, the tab names are Monday...Tue.. up to Sunday. Each tab contains an image file based on the days (size 460*620). When I run my page, it shows all images, but what I need is for the imag ...

using hover/click functionality with a group of DIV elements

I have a group of DIV elements that I want to apply an effect to when hovering over them with the mouse. Additionally, when one of the DIVs is clicked, it should maintain the hover effect until another DIV is clicked. <div class="items" id="item1"> ...

In PHP, how to arrange list items in columns underneath the corresponding title characters

In the PHP code snippet below, Line B is used to count the title characters while grouping array results alphabetically. Line A outputs 4. DEMO PHP code: <?php $beta_lists = array ( 'Àpple' => 'http://www.abc.mno/apple/', ...

Are you searching for a stylish tabbed navigation menu design?

I am searching for a tabbed navigation menu specifically designed to showcase images as the tab items. Here is an example of what I have in mind: <div class="menu"> <a href="#"> <img src="images/Chrysanth ...

Show a specific form field based on the chosen option in a dropdown menu using Angular and TypeScript

I am looking to dynamically display a form field based on the selected value from a dropdown list. For instance, if 'first' is chosen in the dropdown list, I want the form to remain unchanged. However, if 'two' is selected in the drop ...

What is the best way to achieve a precision of 6 decimal places in JavaScript when working with decimals?

While working on coding to round numbers to six decimal places after performing some arithmetic operations, I encountered a problem. I was iterating through the elements of an array and conducting calculations based on the array contents. To achieve roundi ...

json_encode has stopped functioning (now)

Recently, I encountered a strange issue. I've been trying to display chart data, but the JSON_ENCODE function that is supposed to convert my data into JSON format is not returning anything. It was working fine before when I had less data, but now it&a ...

What is the best way to resize a primefaces inputTextArea to match the length of the

I am currently working on improving the appearance of <p:inputTextArea/>. When the page loads, I notice: And when I click on that TextArea: Here is the code: <p:inputTextarea rows="6" value="#{bean.object.value}" style="width: 100%;" /> Is ...

Why is it that my jquery code seems to be struggling with calculating the count accurately?

I'm currently working on restricting keyword input on my website. My goal is to automatically add a 'span' tag to any keyword entered by a user, as shown in the example html code below. Although I can limit the number of words, the count i ...

Send a string to the controller through an AJAX request

Seeking assistance on creating a search field to find users within my system. The idea is to input the user's name into the field, then use an Ajax function to pass that name from the search field to a method in the controller. This method will return ...