Can I write a CSS selector selecting elements NOT having a certain class or attribute?

前端 未结 10 515
不知归路
不知归路 2020-11-22 10:47

I would like to write a CSS selector rule that selects all elements that don\'t have a certain class. For example, given the following HTML:



        
相关标签:
10条回答
  • 2020-11-22 11:27

    Example

      [class*='section-']:not(.section-name) {
        @include opacity(0.6);
        // Write your css code here
      }
    

    // Opacity 0.6 all "section-" but not "section-name"

    0 讨论(0)
  • 2020-11-22 11:29

    You can use :not(.class) selector as mentioned before.

    If you care about Internet explorer compatibility I recommend you to use http://selectivizr.com/.

    But remember to run it under apache otherwise you won't see the effect.

    0 讨论(0)
  • 2020-11-22 11:36

    If you want a specific class menu to have a specific CSS if missing class logged-in:

    body:not(.logged-in) .menu  {
        display: none
    }
    
    0 讨论(0)
  • 2020-11-22 11:43

    Typically you add a class selector to the :not() pseudo-class like so:

    :not(.printable) {
        /* Styles */
    }
    
    :not([attribute]) {
        /* Styles */
    }
    

    But if you need better browser support (IE8 and older don't support :not()), you're probably better off creating style rules for elements that do have the "printable" class. If even that isn't feasible despite what you say about your actual markup, you may have to work your markup around that limitation.

    Keep in mind that, depending on the properties you're setting in this rule, some of them may either be inherited by descendants that are .printable, or otherwise affect them one way or another. For example, although display is not inherited, setting display: none on a :not(.printable) will prevent it and all of its descendants from displaying, since it removes the element and its subtree from layout completely. You can often get around this by using visibility: hidden instead which will allow visible descendants to show, but the hidden elements will still affect layout as they originally did. In short, just be careful.

    0 讨论(0)
  • 2020-11-22 11:45

    As others said, you simply put :not(.class). For CSS selectors, I recommend visiting this link, it's been very helpful throughout my journey: https://code.tutsplus.com/tutorials/the-30-css-selectors-you-must-memorize--net-16048

    div:not(.success) {
      color: red;
    }
    

    The negation pseudo class is particularly helpful. Let's say I want to select all divs, except for the one which has an id of container. The snippet above will handle that task perfectly.

    Or, if I wanted to select every single element (not advised) except for paragraph tags, we could do:

    *:not(p) {
      color: green;
    }
    
    0 讨论(0)
  • 2020-11-22 11:47

    Using the :not() pseudo class:

    For selecting everything but a certain element (or elements). We can use the :not() CSS pseudo class. The :not() pseudo class requires a CSS selector as its argument. The selector will apply the styles to all the elements except for the elements which are specified as an argument.

    Examples:

    /* This query selects All div elements except for   */
    div:not(.foo) {
      background-color: red;
    }
    
    
    /* Selects all hovered nav elements inside section element except
       for the nav elements which have the ID foo*/
    section nav:hover:not(#foo) {
      background-color: red;
    }
    
    
    /* selects all li elements inside an ul which are not odd */
    ul li:not(:nth-child(odd)) { 
      color: red;
    }
    <div>test</div>
    <div class="foo">test</div>
    
    <br>
    
    <section>
      <nav id="foo">test</nav>
      <nav>Hover me!!!</nav>
    </section>
    <nav></nav>
    
    <br>
    
    <ul>
      <li>1</li>
      <li>2</li>
      <li>3</li>
      <li>4</li>
      <li>5</li>
    </ul>

    We can already see the power of this pseudo class, it allows us to conveniently fine tune our selectors by excluding certain elements. Furthermore, this pseudo class increases the specificity of the selector. For example:

    /* This selector has a higher specificity than the #foo below */
    #foo:not(#bar) {
      color: red;
    }
    
    /* This selector is lower in the cascade but is overruled by the style above */
    #foo {
      color: green;
    }
    <div id="foo">"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor
      in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."</div>

    0 讨论(0)
提交回复
热议问题