Unable to successfully implement the CSS not selector

My webpage's code was created through the use of Wordpress.

<div class="header-main">
            <h1 class="site-title"><a href="http://wp-themes.com/" rel="home">Theme Preview</a></h1>

            <div class="search-toggle active">
                <a href="#search-container" class="screen-reader-text">Search</a>
            </div>

            <nav id="primary-navigation" class="site-navigation primary-navigation" role="navigation">
                <h1 class="menu-toggle">Primary Menu</h1>
                <a class="screen-reader-text skip-link" href="#content">Skip to content</a>
                <div class="nav-menu"><ul><li class="page_item page-item-2"><a href="http://wp-themes.com/?page_id=2">About</a></li><li class="page_item page-item-46 page_item_has_children"><a href="http://wp-themes.com/?page_id=46">Parent Page</a><ul class="children"><li class="page_item page-item-49"><a href="http://wp-themes.com/?page_id=49">Sub-page</a></li></ul></li></ul></div>
            </nav>
        </div>

In an attempt to hide all elements except those with .page-item-2, I implemented the following CSS:

.header-main .nav-menu li:not(.page-item-2) {
    display:none;
}

This solution effectively hides unwanted elements, but I also wish to exclude .page-item-46 from the selector. My modified CSS rule looks like:

.header-main .nav-menu li:not(.page-item-2) :not(.page-item-46) {
    display:none;
}

Unfortunately, this adjustment does not produce the desired outcome.

Answer №1

When dealing with the element .page-item-46, it's important to note that it is not a descendant. As a result, you must remove the space between the :not pseudo classes:

.header-main .nav-menu li:not(.page-item-2):not(.page-item-46) {
    display:none;
}

Check out an example here


To illustrate this concept further, consider the following:

<ul>
    <li class="one">one..</li>
    <li class="two">two</li>
    <li class="three">three</li>
</ul>

In this case, using the following selector would exclude elements with classes .one and .two: (example)

ul li:not(.one):not(.two) {
    color:red;
}

The following approach does not achieve the desired outcome: (example)

ul li:not(.one) :not(.two) {
    color:red;
}

Similarly, this method also falls short: (example)

ul li:not(.one,.two) {
    color:red;
}

Lastly, an ineffective approach selects all elements because both selectors are not exclusive. (example)

ul li:not(.one),
ul li:not(.two) {
    color:red;
}

Answer №2

Even though you have received a solution, it's worth noting that if compatibility with older browsers is important to you, you can achieve the desired outcome by implementing the following code:

.header-main .nav-menu li.page-item-2, .header-main .nav-menu li.page-item-46 {
    display: list-item;
}
.header-main .nav-menu li {
    display: none;
}

Answer №3

apply the following CSS code

.header-main .nav-menu li:not(.page-item-2),.header-main .nav-menu li:not(.page-item-46) {
    visibility:hidden;
}

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

Looking for a way to locate an element in an ajax pop-up window using

Struggling to find a specific element in a pop-up window? Even after attempting to switch frames and windows, the desired element remains elusive. The original source code does not contain the sought-after element. Even when the pop-up is active, len(drive ...

Discovering hidden files within appsettings.json in ASP.NET Core 6 using Program.cs

Within Appsettings.json, the code resembles the following: { "Project": { "ConnectionString": "Data Source=(local)\SQLSERVER; Database=MyCompany; Persist Security Info=false; User ID='sa'; Password='sa ...

Stylish CSS round link positioned on a half-circle

As a beginner in web design, I'm struggling with CSS. However, I have been given the task of creating a button like the one shown below. I am unsure of how to create a circular link like this. Any assistance would be greatly appreciated. ...

Arranging several lists in columns with a customized header title

I am looking to have 7 lists displayed side by side, each with a unique styled header title. I want the lists to be neatly organized under their respective titles, including the bullets. I attempted to use text-indent for this purpose, but it seems that ...

Hovering over the <li> element reveals that it does not have an appropriate size

I'm currently utilizing bootstrap and have come across this code snippet: <ul class="dropdown-menu scrollable-menu-brands" style="width:210px;"> {foreach $arr as $id => $r} <li><a class="dropdown-i ...

Is there a way to automatically adjust the size of an input field after dynamic spans have been

I am looking to replicate the tagging functionality found on StackOverflow when posting a new question. In this case, spans are dynamically added using an input field located next to them. Here is an example: <div> <div id="multiple-span"&g ...

Blur transition on CSS-defined borders

Looking to create a large button with a blurred background image that unblurs on hover. I've implemented a container with overflow hidden and adjusted the margin on the background image for defined edges. However, during the transition from blurred t ...

Customizing a material-ui theme with withStyles() in conjunction with withTheme()

After developing a series of reusable components, I started styling them by utilizing classes prop to override the MUI classnames. To streamline the process and prevent repetition in more intricate components, I consolidated common styles into a theme. Eac ...

The collapsible menu in Bootstrap features a two-row design

I am currently in the process of developing a website called torgoborudovanie.com One issue I have encountered is with the collapsing navbar that I built using Bootstrap. When I attempt to resize the window, it ends up looking like this: https://i.sstatic ...

Vibrant progress bar design with CSS

Could someone assist me in coding a multicolor progress bar? I would like it to display red when the progress is less than 50, green when the progress is between 50 and 90, and blue when the progress is between 90 and 100. How can I achieve this? ...

Utilizing React for incorporating HTML5 canvas gradients

I'm currently working on an app that allows users to generate a background gradient with two distinct colors using React. The issue I'm facing is that while the first color appears correctly, the second color ends up looking more like a solid col ...

Preventing conflicts while toggling visibility with JQuery across various groups

My navigation system allows me to show/hide divs and toggle an 'active' class on the navigation items. However, I've encountered conflicts when trying to use similar sections on the same page (clicking a link in one section affects divs in o ...

Loading content onto a webpage using AJAX

I am currently facing a challenge while attempting to incorporate a file on my server using AJAX. The issue I am encountering is that it disrupts the functionality of my PHP code. The AJAX is implemented on the following page: <div id="content&quo ...

Reading Properties in VueJS with Firebase

<template> <div id="app"> <h1 id="title"gt;{{ quiz.title }}</h1> <div id="ques" v-for="(question, index) in quiz.questions" :key="question.text"> <div v-show="index = ...

Creating an Event on ASP.NET Websites

I'm attempting to add a button to my ASP.net website that, when clicked, will trigger a music player to start streaming music on the site. Although I have experience creating buttons and handling events, I am struggling to get my player to play the * ...

How to make your divs stand out using Bootstrap

Can anyone assist me in aligning my divs based on the example below? https://i.sstatic.net/OAyCj.jpg Here is what I have achieved so far: https://i.sstatic.net/KkOWg.jpg I am struggling to apply spacing between the two divs, and they are not of equal h ...

Guide to turning off the Facebook iframe page plugin

I am facing a challenge with embedding a facebook iframe on my website. I want to disable it once the screen width reaches a specific point, but I am struggling to make the iframe disappear properly. Here is how I have attempted to implement this: window.o ...

Updating the Hero Image

Scenario: In my project directory, under Product -> src -> static -> ostatic [ there are two folders named css and images ] -> images -> where all the images can be found. I am using bootstrap and attempting to set a background-image for j ...

Error: The context parameter should be in the form of a dictionary, not a set

I am currently working on integrating these two different perspectives. My goal is to merge MenuView with add_to_menu so that even if the if statement produces a negative result, the menuview section will still execute and display the menu on my HTML page. ...

Is there a way for me to choose the nearest input value when clicking on a href

Is there a way to efficiently select the nearest input value when clicking on a link? I attempted to do so like this, but it returns undefined: $(".change").click(function(e){ e.preventDefault(); var qty = $(this).closest('input').val(); ...