简体   繁体   English

为相同标签但行为不同的CSS类选择器命名的最佳实践

[英]Best Practice on Naming CSS class selector for a same tag, but different behavior

Let's say I have a <td> html tag like this: 假设我有一个<td> html标签,如下所示:

....
<td>Sample Item 1 - Common Item</td>
<td>Sample Item 2 - Important Item</td>
<td>Sample Item 3 - Common Item</td>
....

And then for the Important Item I need to (for example) color it in red. 然后,对于重要项目,我需要(例如)将其涂成红色。 Currently Common Item don't need any style and I don't know whether in the future it will need any style or not. 目前,Common Item不需要任何样式,我也不知道将来是否需要任何样式。 If I want my .Less file follows the best practice on writing css class, should I create the css class in .Less like this: 如果我希望我的.Less文件遵循编写css类的最佳实践,则应该在.Less中创建css类,如下所示:

.item {
    &.item-important {
        color: red;
    }
}

and use it like this: 并像这样使用它:

....
<td class="item">Sample Item 1 - Common Item</td>
<td class="item item-important">Sample Item 2 - Important Item</td>
<td class="item">Sample Item 3 - Common Item</td>
....

OR should I only create the css class in .Less like this: 或者我应该只在.s中创建css类,像这样:

.important-item {
    color: red;
}

and use it like this: 并像这样使用它:

....
<td>Sample Item 1 - Common Item</td>
<td class="important-item">Sample Item 2 - Important Item</td>
<td>Sample Item 3 - Common Item</td>
....

I'm a bit confused since in Bootstrap I saw a lot like btn btn-default , or like glyphicon glyphicon-asterisk . 我有点困惑,因为在Bootstrap中我看到了很多类似btn btn-defaultglyphicon glyphicon-asterisk Why they don't just put like btn-default or glyphicon-asterisk ? 为什么它们不像btn-defaultglyphicon-asterisk那样放? I can't seem to find a good post explaining the best practice and reasoning for this naming style for css classes. 我似乎找不到很好的文章来解释css类的这种命名方式的最佳实践和推理。 Any insight would be much appreciated! 任何见解将不胜感激!

---------------------------- Update ---------------------------- ----------------------------更新--------------------- -------

So I was then reading this documentation http://getbem.com/naming/ , and found this: 因此,我当时在阅读此文档http://getbem.com/naming/ ,发现了这一点: 在此处输入图片说明 Now I'm sure that if I have the style for item base class, the best practice is putting: 现在,我可以确定,如果我具有item基类的样式,则最佳做法是:

....
<td class="item">Sample Item 1 - Common Item</td>
<td class="item item--important">Sample Item 2 - Important Item</td>
<td class="item">Sample Item 3 - Common Item</td>
....

But if I don't have the style for item yet, is <td class="item--important">...</td> still a bad exercise? 但是,如果我没有对风格item又是<td class="item--important">...</td>仍然是一个坏的锻炼? I'm also considering YAGNI here.. 我也在这里考虑YAGNI。

The concept of naming the CSS classes as such as known as the BEM nomenclature/method , which is an acronym for b lock- e lement- m odifier. 命名CSS类作为诸如称为概念BEM命名法/方法 ,它是对于b锁相Ëlement- odifier的缩写。 What BEM does is that it advocates for a clear and standardised way of naming your CSS classes, so that you do not get easily confused. BEM所做的是提倡一种清晰,标准化的命名CSS类的方式,以免引起混乱。

The BEM method is what that inspires the CSS classes you come across. BEM方法激发了您遇到的CSS类。 In your question, you mentioned btn and btn-default . 在您的问题中,您提到了btnbtn-default In strict BEM sense, that would be btn and btn--default , since "default" is a modifier/state. 在严格的BEM意义上,这将是btnbtn--default ,因为“默认”是修饰语/状态。 However, different authors and frameworks have different means of separating these terms, so btn-default is just as legitimate as btn--default —as long as you are consistent throughout your stylesheet. 然而,不同的作者和框架必须分离这些术语的不同手段,使btn-default是一样合法的btn--default -只要你是在你的样式保持一致。

In this case, btn is the base class of all button-like elements. 在这种情况下, btn是所有类似按钮的元素的基类 It likely contains some base styles (like padding, line-height, positioning). 它可能包含一些基本样式(如填充,行高,位置)。 btn-default is an extension of the btn class, perhaps containing colors for the "default look" of the button (I can imagine authors having a standard call-to-action color for buttons). btn-defaultbtn类的扩展,可能包含按钮的“默认外观”的颜色(我可以想象作者具有按钮的标准号召性用语)。 Declaring btn-default itself does not make sense, because it extends on or modifies the btn class, which means it lacks the base styles of what is intended to be a button. 声明btn-default本身是没有意义的,因为它在btn类上扩展修改btn类,这意味着它缺少打算用作按钮的基本样式。

Based on this logic, even when item does not have any styles explicitly tied to it, you should still include it in your markup, along side with item--important , for example. 基于此逻辑,即使item没有任何明确关联的样式,您仍应将其包括在标记中,例如item--important Using item--important itself has no meaning. 使用item--important本身没有任何意义。

If you would indulge me in a rather more verbose example, let's say you have the following layout: you want a <div> that spans the full width of a container, and sometimes you wanted it to expand beyond the container, and etc...: 如果让我沉迷于一个更为冗长的示例,则可以假设您具有以下布局:您想要一个<div>跨越容器的整个宽度,有时您希望它扩展到容器之外,等等。 ::

 body { background-color: #ccc; margin: 0; padding: 0; } section { background-color: #fff; margin: 1.5rem 3rem; position: relative; } /* Base styles for .content */ .content { background-color: #eee; margin-bottom: 1.5rem; padding: 1rem; } /** * Modifier: expand * Now, we want to increase the width */ .content--expand { position: relative; left: -3rem; width: calc(100% + 6rem); } /** * Modifier: important * Now, we want to draw attention to this content */ .content--important { background-color: #b13131; color: #fff; } 
 <section> <div class="content">I am content, with default styles</div> <div class="content content--expand">I am content, with expanded width. Note that I inherit base styles from my `.content` block</div> <div class="content--expand">I am expanded width content without using `.content`. Look that I am messed up.</div> <div class="content content--important">I am a very important content.</div> </section> 

Note that the appearance of one of the <div> element is off: that is because it does not have the class .content , and therefore fails to inherit the "default" look of the .content containers ;) 请注意, <div>元素之一的外观已关闭:这是因为它没有类.content ,因此无法继承.content容器的“默认”外观;)

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

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