Can a CSS class inherit one or more other classes?

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

    I realize this question is now very old but, here goes nothin!

    If the intent is to add a single class that implies the properties of multiple classes, as a native solution, I would recommend using JavaScript/jQuery (jQuery is really not necessary but certainly useful)

    If you have, for instance .umbrellaClass that "inherits" from .baseClass1 and .baseClass2 you could have some JavaScript that fires on ready.

    $(".umbrellaClass").addClass("baseClass1");
    $(".umbrellaClass").addClass("baseClass2");
    

    Now all elements of .umbrellaClass will have all the properties of both .baseClasss. Note that, like OOP inheritance, .umbrellaClass may or may not have its own properties.

    The only caveat here is to consider whether there are elements being dynamically created that won't exist when this code fires, but there are simple ways around that as well.

    Sucks css doesn't have native inheritance, though.

提交回复
热议问题