2014-09-19 58 views
0

这里的样本:如何引用另一个css风格?

.my-class { 
    font-size: 12px; 
} 

.my-another-class { 
    /* here I want to include .my-class style */ 
    .my-class; 
    border: 0; 
} 

我可以包括一个CSS类到另一个或没有?

+0

goo ..... SCSS LESS SASS? http://stackoverflow.com/questions/11084757/sass-scss-nesting-and-multiple-classes – 2014-09-19 13:49:00

+1

不,你不能在纯CSS中做到这一点。但是,您可以将多个类应用于任何元素。 – j08691 2014-09-19 13:49:10

+0

您可以设置多个选择器,如: .my-another-class, .my-class {} – TheFrozenOne 2014-09-19 13:50:48

回答

3

您可以将多个目标定义为.my-class规则,然后再指定规则只是.my-another-class

.my-class, 
.my-another-class { 
    font-size: 12px; 
} 

.my-another-class { 
    border: 0; 
} 

你甚至可以然后覆盖某些特性,例如

.my-class, 
.my-another-class { 
    color: red; 
    font-size: 12px; 
} 

.my-another-class { 
    border: 0; 
    color: blue; /* overrides color: red; on .my-another-class */ 
} 
+0

哦,谢谢!这是我想要的。你能告诉我,这是一个很好的做法吗? – omickron 2014-09-19 13:52:42

+0

这是完全可以接受的,每个人都会一直这样做,我从来没有听说过没有任何理由不这样做。 SCSS或LESS是**更好的选择,因为它们给你更多的权力,但它们也更加复杂。对于vanilla CSS解决方案,这样做没有问题 – Joe 2014-09-19 13:53:44

+0

谢谢@Joe! – omickron 2014-09-19 13:58:07

0

你不能在普通的CSS中使用这样的结构。

预处理器如LessSass支持此行为与mixins

0

你不能,但你可以这样做:

.my-class, .my-another-class{ 
    font-size: 12px; 
} 

.my-another-class { 
    border: 0; 
}