简体   繁体   English

CSS:我应该使用mixin还是类?

[英]CSS: Should I use a mixin or a class?

I am using the boostrap CSS framework for one of my personal projects. 我正在为我的一个个人项目使用boostrap CSS框架。 By default all the col- classes have a padding on both sides. 默认情况下,所有colclass的两边都有填充。 Which is good for some components. 这对某些组件很有用。 But there are places where I don't want this extra padding. 但是有些地方我不需要这种多余的填充。 So I usually just take a unique class (say containerName ) being used to style that element and then- 所以我通常只是采用一个唯一的类(例如containerName )来设置该元素的样式,然后-

.containerName {
 padding-right: 0;
 padding-left: 0;
 //and whatever other css is required
}

I don't want to keep writing this again and again . 我不想让一次又一次写这个。 So I decided to write a simple mixin just for that. 因此,我决定为此编写一个简单的mixin。

%removePadding { 
padding-right: 0; 
padding-left: 0;
}

and then use extend on .containerName 然后在.containerName上使用extend

.containerName {
 @extend %removePadding;
 //and whatever other css is required
}

Is this an overkill? 这是一个过度杀伤力吗? Should I just add another class to those elements which has these two rules? 我是否应该向具有这两个规则的元素添加另一个类?

What is the best way to handle this? 处理此问题的最佳方法是什么?

I think you should use ".removePadding" as a class, once. 我认为您应该一次使用“ .removePadding”作为一个类。 Because in any case, you need to check which divs you want the padding, and which divs you don't. 因为无论如何,您都需要检查要填充的div,而不要填充的div。 So if you just add ".removePadding" class to your div, right after your col-... class, you will have the same control, and more important than that, you won't be expanding your CSS file. 因此,如果仅在col -...类之后将“ .removePadding”类添加到div中,您将拥有相同的控件,并且比这更重要的是,您将不会扩展CSS文件。 And on the other hand, you can unterstand what is going on just by looking at your HTML file. 另一方面,仅查看HTML文件,您就无法理解正在发生的事情。 Like common ".clearfix" classes. 类似于常见的“ .clearfix”类。

Add this CSS rule (.removePadding class) after the bootstrap styles, to override them. 在引导样式之后添加此CSS规则(.removePadding类),以覆盖它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM