2014-09-27 101 views
4

是否可以动态地将类分配给现有类?我试图给第一个孩子分配一个样式,像这样:从CSS中动态分配CSS类

.active { 
    color: red; 
} 

item:first-child .item_text { 
    background: rgba(255, 255, 255, 1); 
    /* make it active class */ 
} 

回答

2

不,这根本就不可能用CSS。

最好的你可以这样做:

.active, 
item:first-child .item_text { 
    color: red; 
} 

item:first-child .item_text { 
    background: rgba(255, 255, 255, 1); 
} 

,如果你使用一个CSS预处理器一样都不能少或有可能SASS,这其中包括扩展CSS与像包括/扩展类的功能。

在更短的

item:first-child .item_text { 
    background: rgba(255, 255, 255, 1); 
    .active; 
} 

这会从字面上替换为color: red;行类名。

在SASS(从第3版,它使用 “SCSS” 语法):

item:first-child .item_text { 
    background: rgba(255, 255, 255, 1); 
    @extend .active; 
} 

这将使得输出如上述我的CSS实施例相同。