Can a CSS class inherit one or more other classes?

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

    While direct inheritance isn't possible.

    It is possible to use a class (or id) for a parent tag and then use CSS combinators to alter child tag behaviour from it's heirarchy.

    p.test{background-color:rgba(55,55,55,0.1);}
    p.test > span{background-color:rgba(55,55,55,0.1);}
    p.test > span > span{background-color:rgba(55,55,55,0.1);}
    p.test > span > span > span{background-color:rgba(55,55,55,0.1);}
    p.test > span > span > span > span{background-color:rgba(55,55,55,0.1);}
    p.test > span > span > span > span > span{background-color:rgba(55,55,55,0.1);}
    p.test > span > span > span > span > span > span{background-color:rgba(55,55,55,0.1);}
    p.test > span > span > span > span > span > span > span{background-color:rgba(55,55,55,0.1);}
    p.test > span > span > span > span > span > span > span > span{background-color:rgba(55,55,55,0.1);}

    One possible solution is using multiple nested tags

    I wouldn't suggest using so many spans like the example, however it's just a proof of concept. There are still many bugs that can arise when trying to apply CSS in this manner. (For example altering text-decoration types).

提交回复
热议问题