-1

So I have a navbar with several links inside of it. If the width of the screen gets too small, all of those links disappear and a hamburger menu icon appears, which is also a link, but it has the class "hamburger". How would I make a selector in CSS that would select all of the links except from that hamburger menu link?

Here is my CSS:

@media only screen and (min-width: 601px) {
    /* hide the hamburger */
    .hamburger {
        display: none;
    }
    /* show all the nav-buttons */
    .navbar a {
        display: block;
    }
}


/* if browser window is small (max-width) */

@media only screen and (max-width: 600px) {
    /* show everything with the hamburger class */
    .hamburger {
        display: block;
    }
    /* hide all the nav-buttons */
    .navbar a {
        display: none;
    }
}

2 Answers2

2

You need to use the pseudo class not() as per the MDN docs not() CSS psuedo class

The not() pseudo class requires a comma-separated list of one or more selectors as its argument (i.e. what goes inside the brackets).

In your example where you want all of the links except the one with the class .hamburger it would be:

a:not(.hamburger)
paulo77
  • 174
  • 14
1

You can use a CSS pseudo-class, specifically the :not() selector, to select elements that do not match the hamburger class.

Assuming that .hamburger is a class nested of another called .navbar:

.navbar a:not(.hamburger) {
    /* your code */
}

This will select all <a> tags except those containing the .hamburger class.

Moh
  • 23
  • 1
  • 5