在CSS中使用HTML标记名称,类或ID? [英] Use HTML tag names, classes or IDs in CSS?

查看:141
本文介绍了在CSS中使用HTML标记名称,类或ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在设计页面的HTML和CSS时,应该何时使用

In designing the HTML and CSS for a page, when should I use


img.className

img.className


.className

对比


#idName

是否有指南或建议?

感谢所有回答者 - 这里有一些优秀的东西!

Thank you to all answerers - there is some excellent stuff here!

  • make CSS as specific as possible
  • use an OO approach
  • order: #id, tag, tag.className, .className
  • when to use each selector, also class/ID comparison
  • give selectors names based on purpose, not what they look like
  • use advanced selectors for smaller code, leave CSS classes for exceptions/overrides only
  • manage ASP.NET munging ID

推荐答案

一般来说,您应该与项目需求相同

没有一般规则,这取决于所讨论的风格。

There is no general rule, it depends on the style in question.

很多人会推荐你保持最低的特殊性与理论,这允许最大级联重用,但这在现实世界的情况下是绝对有毒的,你有多个开发人员使用略有不同的版本.foo可能是什么样子。从你继承的污染,你不想导致大规模的膨胀试图撤消本地或重构时间损失。

A lot of people will recommend you keep to the lowest specificity with the theory that this allows the maximum cascading reuse but this is absolutely toxic in real world situations where you have multiple developers all working on slightly different versions of what a .foo might look like. Pollution from inheritance you did not want leads to massive bloat in trying to undo that locally or time-loss in refactoring.

我总是提供的最好的指导是试着和想的CSS在OO术语:类slectors映射到接口或多或少,标签映射到类和ID选择器映射到实例。非常确定您要应用的样式是否真的适用于该内容,所有内容或任何需要它的内容。

The best guideline I always offer is to try and think of CSS in OO terms: class slectors map to interfaces more or less, tags map to classes, and ID selectors map to instances. Consxequently decide if the style you want to apply really applies to that thing, all things like it, or anything which wants it.

我也强烈建议您使用高级id在包装器元素上,所以你可以编写选择器在一个命名空间像时尚(即#foo .bar,#foo .baz其中#foo是一个页面或页面设计的唯一),这允许你一个水平的特异性,减少了交叉设计的污染和一般的水平,使您可以充分利用级联CSS重用。

I also strongly encourage you to make use of high level id's on wrapper elements so you can write selectors in a namespace like fashion (i.e. #foo .bar, #foo .baz where #foo is unique to a page or set of page designs) which allows you both a level of specificity which reduces cross-design pollution and a level of generality which lets you make the most of cascading CSS reuse.

两者皆有。

这篇关于在CSS中使用HTML标记名称,类或ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆