The :not() exclusion in CSS property fails to exclude the class

I'm attempting to style all the links on my webpage in a particular way, except for those in the navigation bar. Despite trying to exclude the navbar links using a:not(.navbar), it seems that the styling still applies to all links, including Link 1 in the navbar:

html:

<body>
  <div class=".navbar">     
    <a href="#">Link 1</a>
  </div>      
  <a href="#">Link 2</a>
  <a href="#">Link 3</a>   
</body>

css:

body a:not(.navbar)  {
    font-size: 100%;
    color: black;       
    text-decoration: none !important;
    border-bottom: 6px solid red;   
}


body a:not(.navbar):hover  {
    border-bottom: none;
    background-color: #80b3ff;
    color: white;    
    text-decoration: none !important;
}

codepen

Answer №1

When styling the navbar, remember that the .navbar class should be applied to the container DIV, not the individual links. Therefore, your selectors should look like this:

div:not(.navbar) a { ... }

and

div:not(.navbar) a:hover

However, in order for this to work properly, you need a separate container div for the other links without a class. I have included an example below with the necessary adjustments. Additionally, it's important to note that the class attribute in HTML should be written as class="navbar" without the dot.

div:not(.navbar) a {
      font-size: 100%;
      color: black;
      text-decoration: none !important;
      border-bottom: 6px solid red;
    }

    div:not(.navbar) a:hover {
      border-bottom: none;
      background-color: #80b3ff;
      color: white;
      text-decoration: none !important;
    }
<body>
      <div class="navbar">
        <a href="#">Link 1</a>
      </div>
      <div>
        <a href="#">Link 2</a>
        <a href="#">Link 3</a>
      </div>
    </body>

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

ReactJS is unable to locate a valid DOM element within the target container

I recently embarked on my journey to learn ReactJS and I celebrated successfully writing my first code. However, when I encountered the same pattern with components, an error popped up stating _Invariant Violation: registerComponent(...): Target container ...

Dropdown with multiple selections organized in a hierarchical structure

I am in need of the following : Implement a drop-down menu that reflects hierarchical parent-child relationships. Include a checkbox for each node to allow for selection. If all child nodes are selected, their parent node should be automatically ch ...

HTML and CSS: Aligning Text on the Left and Image on the Right - A Guide to Positioning Elements

Just starting out with HTML and CSS, I've run into some issues while writing my code. My goal is to have Text (on the left middle part) and image (on the right middle part) displayed side by side A responsive design that stacks the text on top of t ...

Troubleshooting Cross-Origin Resource Sharing (CORS) problem in Jquery

When using AJAX post from jQuery to call two REST services on different domains for business logic, a CORS issue arises. By setting crossDomain: true in my AJAX call following Google references, it works fine without specifying headers. However, if I inc ...

Use PHP to dynamically adjust the number of form instances and then save them all as CSV files

Currently, I am developing a PHP script that will allow users to open an existing CSV file (referred to as the template) on a webpage. Users can then populate editable text fields with its data anywhere between 1 and 50 times, depending on their needs. Onc ...

In IOS 9.0, flex items do not wrap to new lines and result in overflow

While it functions properly in other browsers, the issue arises in Safari. I require the flex items to occupy the entire width on screens smaller than 768px. Currently, they are taking up the full width of their container but remaining in a single line, wh ...

Using display:inline-block will increase the vertical spacing

I am currently dealing with the following HTML and CSS setup: * { -webkit-box-sizing: border-box; -moz-box-sizing: border-box; box-sizing: border-box; } body { margin: 0; } .row { background-color: red; /* display: inline-block; */ } ul { ...

Bulma: Tips for creating equally sized buttons?

I am working with Bulma CSS and trying to ensure that all my buttons are the same size. Currently, each button appears to have a different size based on the text content. I have looked into using the "is-fullwidth" option, but it makes the buttons too la ...

Extracting data from the web using PHP

I'm facing an issue with my code that scrapes data from a website. The current output looks like this: Agriculture Food Apparel I want to only display the first/nth category, for example just "Agriculture". I attempted: echo $sub_title[1].&ap ...

Exploration of search box with highlighted fields

Seeking assistance to enhance the highlighting feature after entering a letter in the search box. Here is the current code snippet: <!DOCTYPE html> <html> <head> <script type="text/javascript" src="javascripts/jquery-1.11.0.min.js"&g ...

Ways to prevent onMouseDown from triggering when the element is exclusively clicked?

I'm currently working on developing a game where units (svg elements) are controlled using React. In this game, the units should be draggable, and clicking on a unit should open a form. However, when I click on the unit, only the mousedown event is t ...

Guide to parsing the HTML structure of a webpage from a different domain

Struggling with parsing the DOM of a remote webpage using AJAX. Can't find any examples or tutorials on this process. My goal is to navigate through the DOM of a remote page, locate a tag with a specific id/class, extract the content within that tag, ...

Ensure that the image inside a resizable container maintains the correct aspect ratio

I often encounter a situation where I need to showcase a series of thumbnails/images that adhere to a specific aspect ratio within a flexible container. My current approach involves using a transparent, relatively positioned image to enforce the correct as ...

When touch-triggered on IOS, HTML5 Web SQL Transactions were smoothly bypassed without any errors

I'm encountering issues with database transactions on IOS devices. When the user does not interact with the phone, everything functions as expected. However, if the user taps, scrolls, or touches the screen, some transactions bypass the actual transac ...

Generating HTML using a filter

I have been working on creating a filter that will render HTML tags. Here is the code for my filter: filters: { limitStrLength: function (value, maxLength) { if (value && value.length > maxLength) { let partialVal = value.substr(0, ...

Stack pictures on a slender DIV

I am trying to create a vertical line inside a DIV container. After that, I want to add an image on top of the vertical line (see attached picture for reference). This is what my current source code looks like: <div style="background-color:gray;width ...

What is the best way to ensure that the text within Span children is properly laid out to match the width of the parent div tag?

This particular instance involves setting the width of the div tag to 200px and organizing all the span children to occupy the entire width of the parent div tag. If the first row is filled, the span tags should then flow into a second row. My attempt at ...

Exploring WordPress's Query Page with Posts

My current code successfully pulls posts from a specified category, but I am struggling to also include pages in the output. I have tried various methods, but haven't been able to pull in both posts and pages at the same time. HTML/PHP <?php quer ...

How can I change the cursor of a button to a pointer in Bootstrap 4 when the button is not disabled?

I'm working with a bootstrap button <button class="btn btn-primary">Login</button> My goal is to change the cursor to a pointer (hand) on this button when it's not disabled, instead of the default arrow. The disabled property of th ...

Serving static files in Django

Currently diving into Django and encountering a hurdle with static files. I've followed the setup in both the settings and HTML, but unfortunately, they are not loading on the website. Seeking assistance to resolve this issue! **settings.py:** STATIC ...