Can a CSS class inherit one or more other classes?

前端 未结 30 3029
挽巷
挽巷 2020-11-22 00:01

I feel dumb for having been a web programmer for so long and not knowing the answer to this question, I actually hope it\'s possible and I just didn\'t know about rather tha

相关标签:
30条回答
  • 2020-11-22 00:32

    Actually what you're asking for exists - however it's done as add-on modules. Check out this question on Better CSS in .NET for examples.

    Check out Larsenal's answer on using LESS to get an idea of what these add-ons do.

    0 讨论(0)
  • 2020-11-22 00:33

    No you can't do something like

    .composite 
    {
       .something;
       .else
    }
    

    This are no "class" names in the OO sense. .something and .else are just selectors nothing more.

    But you can either specify two classes on an element

    <div class="something else">...</div>
    

    or you might look into another form of inheritance

    .foo {
      background-color: white;
      color: black;
    }
    
    .bar {
      background-color: inherit;
      color: inherit;
      font-weight: normal;
    }
    
    <div class="foo">
      <p class="bar">Hello, world</p>
    </div>
    

    Where the paragraphs backgroundcolor and color are inherited from the settings in the enclosing div which is .foo styled. You might have to check the exact W3C specification. inherit is default for most properties anyway but not for all.

    0 讨论(0)
  • 2020-11-22 00:33

    Unfortunately, CSS does not provide 'inheritance' in the way that programming languages like C++, C# or Java do. You can't declare a CSS class an then extend it with another CSS class.

    However, you can apply more than a single class to an tag in your markup ... in which case there is a sophisticated set of rules that determine which actual styles will get applied by the browser.

    <span class="styleA styleB"> ... </span>
    

    CSS will look for all the styles that can be applied based on what your markup, and combine the CSS styles from those multiple rules together.

    Typically, the styles are merged, but when conflicts arise, the later declared style will generally win (unless the !important attribute is specified on one of the styles, in which case that wins). Also, styles applied directly to an HTML element take precedence over CSS class styles.

    0 讨论(0)
  • 2020-11-22 00:34

    You can add multiple classes to a single DOM element, e.g.

    <div class="firstClass secondClass thirdclass fourthclass"></div>
    

    Rules given in later classes (or which are more specific) override. So the fourthclass in that example kind of prevails.

    Inheritance is not part of the CSS standard.

    0 讨论(0)
  • 2020-11-22 00:34

    Yes, but not exactly with that syntax.

    .composite,
    .something { display:inline }
    
    .composite,
    .else      { background:red }
    
    0 讨论(0)
  • 2020-11-22 00:34

    Less and Sass are CSS pre-processors which extend CSS language in valuable ways. Just one of many improvements they offer is just the option you're looking for. There are some very good answers with Less and I will add Sass solution.

    Sass has extend option which allows one class to be fully extended to another one. More about extend you can read in this article

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