Can a CSS class inherit one or more other classes?

前端 未结 30 3161
挽巷
挽巷 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:37

    In Css file:

    p.Title 
    {
      font-family: Arial;
      font-size: 16px;
    }
    
    p.SubTitle p.Title
    {
       font-size: 12px;
    }
    
    0 讨论(0)
  • 2020-11-22 00:37

    Don't think of css classes as object oriented classes, think of them as merely a tool among other selectors to specify which attribute classes an html element is styled by. Think of everything between the braces as the attribute class, and selectors on the left-hand side tell the elements they select to inherit attributes from the attribute class. Example:

    .foo, .bar { font-weight : bold; font-size : 2em; /* attribute class A */}
    .foo { color : green; /* attribute class B */}
    

    When an element is given the attribute class="foo", it is useful to think of it not as inheriting attributes from class .foo, but from attribute class A and attribute class B. I.e., the inheritance graph is one level deep, with elements deriving from attribute classes, and the selectors specifying where the edges go, and determining precedence when there are competing attributes (similar to method resolution order).

    The practical implication for programming is this. Say you have the style sheet given above, and want to add a new class .baz, where it should have the same font-size as .foo. The naive solution would be this:

    .foo, .bar { font-weight : bold; font-size : 2em; /* attribute class A */}
    .foo { color : green; /* attribute class B */}
    .baz { font-size : 2em; /* attribute class C, hidden dependency! */}
    

    Any time I have to type something twice I get so mad! Not only do I have to write it twice, now I have no way of programatically indicating that .foo and .baz should have the same font-size, and I've created a hidden dependency! My above paradigm would suggest that I should abstract out the font-size attribute from attribute class A:

    .foo, .bar, .baz { font-size : 2em; /* attribute base class for A */}
    .foo, .bar { font-weight : bold; /* attribute class A */}
    .foo { color : green; /* attribute class B */}
    

    The main complaint here is that now I have to retype every selector from attribute class A again to specify that the elements they should select should also inherit attributes from attribute base class A. Still, the alternatives are to have to remember to edit every attribute class where there are hidden dependencies each time something changes, or to use a third party tool. The first option makes god laugh, the second makes me want to kill myself.

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

    As others have said, you can add multiple classes to an element.

    But that's not really the point. I get your question about inheritance. The real point is that inheritance in CSS is done not through classes, but through element hierarchies. So to model inherited traits you need to apply them to different levels of elements in the DOM.

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

    There are tools like LESS, which allow you to compose CSS at a higher level of abstraction similar to what you describe.

    Less calls these "Mixins"

    Instead of

    /* CSS */
    
    #header {
      -moz-border-radius: 8px;
      -webkit-border-radius: 8px;
      border-radius: 8px;
    }
    
    #footer {
      -moz-border-radius: 8px;
      -webkit-border-radius: 8px;
      border-radius: 8px;
    }
    

    You could say

    /* LESS */
    
    .rounded_corners {
      -moz-border-radius: 8px;
      -webkit-border-radius: 8px;
      border-radius: 8px;
    }
    
    #header {
      .rounded_corners;
    }
    
    #footer {
      .rounded_corners;
    }
    
    0 讨论(0)
  • Have a look at CSS compose: https://bambielli.com/til/2017-08-11-css-modules-composes/

    according to them:

    .serif-font {
        font-family: Georgia, serif;
    }
    
    .display {
        composes: serif-font;
        font-size: 30px;
        line-height: 35px;
    }
    

    I use it in my react project.

    0 讨论(0)
  • You can do is this

    CSS

    .car {
      font-weight: bold;
    }
    .benz {
      background-color: blue;
    }
    .toyota {
      background-color: white;
    }
    

    HTML

    <div class="car benz">
      <p>I'm bold and blue.</p>
    </div>
    <div class="car toyota">
      <p>I'm bold and white.</p>
    </div>
    
    0 讨论(0)
提交回复
热议问题