html/css 的命名约定 + MVC?
有没有关于命名综合命名约定的好文章?
我正在寻找清理一些代码......从资产到模型(Codeigniter)到 HTML 和 CSS 的所有内容。
我也很好奇如何构建CSS......是给所有东西一个独特的类(即search-bar-icon)更好,还是重复像.search span.icon {这样的类更好?
谢谢, 沃克
Are there any good articles on naming comprehensive naming conventions?
I'm looking to clean up some code... everything from assets to models (Codeigniter) to HTML and CSS.
I'm also curious how to structure the CSS... is it better to give everything a unique class ie search-bar-icon or is it better to repeat classes like .search span.icon {?
Thanks,
Walker
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在 HTML/CSS 中,id 和 class 选择器并不等同。 id 的权重更大,因此应谨慎使用。将其用于页面中具有后代选择器的部分,这些选择器的类名与其他部分相同,但您希望它们的样式不同。可以将 id 想象为穷人的页面区域命名空间。
In HTML/CSS, the id and class selectors are not equivalent. The id carries more weight, so it should be used sparingly. Use it for sections of a page where you have descendant selectors whose class names are the same as other sections but you wish them to be styled differently. Think of the id like a poor man's namespacing for page regions.
为每个事物赋予唯一的 id 可以使您的选择器速度最快,但会使您的标记变得臃肿,并且可能会变得难以使用。使用独特的类是没有意义的(类用于对象组)。
您的第二个选择是更清晰的代码,但选择器通常要慢得多。
Giving each thing a unique id makes your selectors fastest, but bloats your markup and can become a bog to work with. Using unique classes kind of doesn't make sense (classes are used for groups of objects).
Your second option is the cleaner code wise but the selectors are usually significantly slower.
在 CSS 中为所有内容赋予一个独特的类违背了“级联”样式表的目的。有效的 CSS 利用级联,以便您重复尽可能少的样式工作。
请记住,大多数 HTML 元素都可以直接设置样式。我不需要使用
因为我可以在不使用 span 的情况下设置标签本身的样式。大多数人使用的 div 和 span 远多于他们真正需要的。
您还可以通过推理来设计样式。例如,我可能有一个
接下来是搜索结果的 UL,我希望其样式与页面上的其他 UL 不同。我可以使用以下规则,而不是给 UL 一个特定的类(例如“searchResultsList”):
H3.searchResults + ul {some style...;}
或者
H3.searchResults + div > > * {一些样式...;}
至于CSS组织,我发现按元素类别组织我的文件很有帮助,从最简单和最普遍的情况开始,例如a,p等,然后处理稍后会出现更复杂的元素,例如表格。我按特殊性对所有内容进行分组,因为这是级联规则的一部分。首先按照元素在文件中出现的顺序处理元素,然后按照影响它的规则的具体程度进行处理。我将所有单实例和实用程序类放在最后(.iconWhichAppearsOnceEver、.noBordersTable 等)
Giving literally everything a unique class in your CSS defeats the purpose of "Cascading" style sheets. Effective CSS leverages the cascade so that you're repeating as little styling effort as possible.
Bear in mind that most HTML elements can be styled directly. I don't need to use
<span class="something"><label>...
because I can style the label itself without using a span. Most people use far more divs and spans than they really need to.You can also style by inference. For example, I might have an <H3 class="searchResults"> followed by a UL of search results that I want to style uniquely from other ULs on the page. Instead of giving the UL a specific class (of, say, "searchResultsList") I could just use the following rule:
H3.searchResults + ul {some styling...;}
or
H3.searchResults + div > * {some styling...;}
As for CSS organization, I find it helpful to organize my files by categories of elements, starting with the simplest and most ubiquitous cases, like a, p, etc. and then handle more complex elements like tables later. I group everything by specificity, because that's part of the cascade rules. An element is handled first in its order of appearance in the file, and then by how specific a rule affecting it is. I place all my one-instance and utility classes last (.iconWhichAppearsOnceEver, .noBordersTable, etc.)
这完全取决于您认为最好的。惯例不是规则,而是可以让您的生活更轻松的指南。由于 Codeigniter 加载所有必需类的方式,它具有相对严格的命名约定。
例如,控制器的文件名是小写的,但类名是大写的,并且应该与文件名匹配。例如,test.php 将生成一个名为 Test 的类。
HTML 类/ID 的命名并不是标准化的,完全取决于您认为最好的命名方式。我通常尝试给我的 ID 和类起一个有意义的名称。例如,包含徽标的 DIV 将被命名为“site_logo”。包含博客文章的 DIV 将被命名为“文章”,依此类推。
另外,不要让整个“ID/类速度较慢”的事情欺骗您,因为速度增加非常小。如果您想优化您的网站,最好优化您的 PHP 或 CSS 代码,而不是删除 HTML ID 或类。
It fully depends on what you think is best. Conventions aren't rules, they're guidelines that can make your life easier. Codeigniter has relatively strict naming conventions due to the way it loads all the required classes.
For example, the filename of a controller is lowercase but the classname is capitalized and should match the filename. E.g. test.php would results in a class named Test.
Naming HTML classes/IDs is something that isn't standardised and fully depends on what you think is best. I usually try to give my IDs and classes a name that makes sense. For example, a DIV containing a logo will be named "site_logo". A DIV containing a blog article will be named "article", and so on.
Also, don't let the whole "IDs/classes are slower" thing fool you as the speed increase is very small. If you want to optimize your website you'd be better off optimizing your PHP or CSS code than removing HTML IDs or classes.
请记住,您可以堆叠和继承 CSS 类,如下所示:
我之前使用过的一个很好的技术是设置多个 CSS 类(在本例中,用于显示审核日志):
这是定义一系列通用样式的好方法(对于带有图标、工具栏按钮、头像等的链接),然后您可以在特定实例中覆盖它们(将其视为类继承)。
我对 CSS 中的命名有点奇怪,但我坚持使用
UpperCase
表示 ID,lowerCamel
表示类的约定。它只是帮助我区分。我遵循的 CSS 命名禅宗是:
#Header a.icon.person
用于标题中的个人资料链接最重要的是,保持简洁。 CSS 越少越好 - 使用通用、可重用的样式,您将受益于 a) UI 的一致性,以及 b) 更少的页面膨胀。
华泰
Remember that you can stack and inherit CSS classes, as follows:
A nice technique I've used before is setting multiple CSS classes (in this case, for displaying an audit log):
This is a great way to define a series of generic styles (for links with icons, toolbar buttons, avatars, etc), which you can then override in specific instances (think of it as class inheritance).
I'm a bit weird about naming in CSS, but I stick to the convention that
UpperCase
is for IDs, andlowerCamel
for classes. It just helps me to differentiate.The zen of CSS naming that I follow is:
#Header a.icon.person
for a profile link in the headerMost importantly, keep it lean. The less CSS, the better - use generic, re-usable styles, and you will benefit from a) consistency in your UI, and b) less page bloat.
HTH
使用类的全部目的是让您可以多次使用它们。对于CSS,特别是对于某个元素,您应该使用id。如前所述,CSS 代码越少越好。
The entire point of working with classes is so you can use them several times. For CSS specificly for a certain element you should use id's. As said, the less CSS code the better.