.mac{margin-left:auto; margin-right:auto;}
.table{.mac; color:red;}
Run Code Online (Sandbox Code Playgroud)
我希望能够做到这一点,对我来说没有意义,为什么我需要这样做:
.table{margin-left:auto; margin-right:auto; color:red;}
Run Code Online (Sandbox Code Playgroud)
我知道我能做的表:
<table class="table mac">
Run Code Online (Sandbox Code Playgroud)
或者只是输入.table{margin-left:auto; margin-right; color;}
上面的内容,但这似乎是多余的.
有没有办法在我的第一个声明中做我想要完成的事情,是否有一些我不知道的特殊语法使这个工作?
您可以使用逗号在同一行上组合多个CSS选择器:
.mac, .table
{
margin-left:auto;
margin-right:auto;
}
.table
{
color:red;
}
Run Code Online (Sandbox Code Playgroud)
简短的回答是否定的,你不能"包括"其他CSS类或"继承"它们可以这么说.
但是,有一些工具可以用来为你抽象那种东西.很少有人想到.我认为"mixin"正是你所寻找的.
以下是LESS首页的代码示例:
.rounded_corners (@radius: 5px) {
-moz-border-radius: @radius;
-webkit-border-radius: @radius;
border-radius: @radius;
}
#header {
.rounded_corners;
}
#footer {
.rounded_corners(10px);
}
Run Code Online (Sandbox Code Playgroud)