CSS - 'Common Classes' 坏习惯与否?

标签 css

<分区>


想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它.

关闭 6 年前

我曾经在我的 CSS 中添加所谓的“公共(public)类”,例如:-

.text-color{color:#2da8d9;}
.text-white{color:#fff;}
.text-lgrey{color:#919D9D !important;}
.text-dgrey{color:#879798 !important;}
.text-dkgrey{color:#656d6e !important;}
.text-dblue{color:#15355C;}
.text-lblue{color:#1466B1;}
.nopadding{padding: 0 !important; margin: 0 !important;}
.margin0{margin:0px !important;}
.margint5{margin-top:5px !important;}
.margint10{margin-top:10px !important;}
.margint20{margin-top:20px !important;}
.margint30{margin-top:30px !important;}
.margint40{margin-top:40px !important;}
.margint50{margin-top:50px !important;}
.margint60{margin-top:60px !important;}
.margint70{margin-top:70px !important;}
.margint80{margin-top:80px !important;}
.margint90{margin-top:90px !important;}
.margint100{margin-top:100px !important;}
.margint120{margin-top:120px !important;}
.marginb10{margin-bottom:10px !important;}
.marginb20{margin-bottom:20px !important;}
.marginb30{margin-bottom:30px !important;} 
.marginb40{margin-bottom:40px !important;}
.marginb60{margin-bottom:60px !important;}
.marginb80{margin-bottom:80px !important;}
.marginb90{margin-bottom:90px !important;}
.marginb100{margin-bottom:100px !important;}
.marginb120{margin-bottom:120px !important;}
.pleftnone{padding-left:0 !important;}
.padrl20{padding:0 20px;}
.padrl50{padding:0 50px;}
.marginl20{margin-left:20px;}
.padt20{padding-top:20px;}
.padt40{padding-top:40px;}
.padt80{padding-top:80px;}
.padt60{padding-top:60px;}
.padb30{padding-bottom:30px;}
.padb40{padding-bottom:40px;}
.padb60{padding-bottom:60px;}
.padb80{padding-bottom:80px;}
.padb100{padding-bottom:100px;}
.pad5{padding:5px;}
.pad10{padding:10px;}
.pad20{padding:20px;}
.pad30{padding:30px;}
.text-center{text-align:center}
.text-right{text-align:right}
.text-left{text-align:left}
.relative-position{position:relative !important}

然后在我需要使用它们时才添加这些类,直到有人告诉我这样做是非常糟糕的做法?

我发现当在子元素上使用绝对定位等时,我确实在很多元素上添加了 position: relative;,对我来说,添加一个类似乎很容易,而不是将它添加到CSS 每次。我可以理解它会随着您添加更多类而增加 HTML 大小,但它会减少 CSS,因为您没有为每个类添加相同的样式?

只是想对最佳实践进行一些澄清,如果这里不是讨论这个问题的地方,请提前致歉。

最佳答案

不好。想象一下具有类 text-white margint5 marginb10 padrl20 padb40 text-center relative-position 的元素。它很丑。 如果你需要 margin 32px 怎么办?你会添加新的类margint32吗?你会为所有的 css 属性添加类吗? border-radius, font-size, font-weight 等。

阅读 BEM .

关于CSS - 'Common Classes' 坏习惯与否?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38633236/

上一篇:html - 绝对对齐问题

下一篇:css - 如何在视频标签上制作背景?

相关文章:

css - 如何影响类中的所有元素?

html - 如何使单个图像在 CSS 中具有动画效果?

javascript - 如何放大 jQuery?

css - 应用于多个选择器的样式不适用于两者

css - 是否可以根据不断变化的类为 an::after 设置动画?

html - 为什么我的 header 默认显示为 : block?

javascript - 如何动态改变 ion-content 位置?是否可以?

html - 为什么当我尝试应用 clear fix 方法时我的样式会被破坏?

css - 字体内的图标是否适合网站?

css - 背景位置不适用于 IE