Can we include common css class in another css class?

前端 未结 3 1895
一个人的身影
一个人的身影 2020-12-14 14:36

I am a CSS newbie. I am just wondering, is that possible to include one common class into another class?

for example,

.center {align: center};
.conte         


        
3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-14 14:45

    Bizarrely, even though CSS talks about inheritance, classes can't "inherit" in this way. The best you can really do is this:

    .center, .content { align: center; }
    .content { /* ... */ }
    

    Also I'd strongly suggest you not do "naked" class selectors like this. Use ID or tag in addition to class where possible:

    div.center, div.content { align: center; }
    div.content { /* ... */ }
    

    I say this because if you do your selectors as broad as possible it ends up becoming unmanageable (in my experience) once you get large stylesheets. You end up with unintended selectors interacting with each other to the point where you create a new class (like .center2) because changing the original will affect all sorts of things you don't want.

提交回复
热议问题