Can a CSS class inherit one or more other classes?

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 than what I think is the answer (which is that it's not possible).

My question is whether it is possible to make a CSS class that "inherits" from another CSS class (or more than one).

For example, say we had:

.something { display:inline }
.else      { background:red }

What I'd like to do is something like this:

.composite 
{
   .something;
   .else
}

where the ".composite" class would both display inline and have a red background


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;
}

You can add multiple classes to a single DOM element, eg

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

Inheritance is not part of the CSS standard.


是的,但不完全符合该语法。

.composite,
.something { display:inline }

.composite,
.else      { background:red }
链接地址: http://www.djcxy.com/p/22842.html

上一篇: css选择器匹配没有属性x的元素

下一篇: CSS类可以继承一个或多个其他类吗?