使用类或ID [英] Use Class or ID

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

问题描述

我知道id'只应该用在

页面上的唯一元素,但是否则使用class或id会有什么不同?


我一直在制作一些无表格的样本来学习css,但是

我已经养成了只使用课堂的习惯。在我制作的其中一个
页面中,我将唯一的div类更改为id'和

html文件大小仅减少了23个字节,并且css文件减少了

a几个字节。没什么区别。


这只是个人偏好还是更有效?


提前致谢,

-

Chet
ng ****** @ NOcharterSPAM.net (删除NO .....垃圾邮件)

I know that id''s should only be used for a unique element on a
page, but what is the difference of using class or id otherwise?

I''ve been making a few sample tableless pages to learn css, but
I''ve gotten into the habit of using only class. In one of the
pages I made, I changed the unique div classes to id''s and the
html file size only decreased 23 bytes and the css file decreased
a few bytes. Not much of a difference.

Is it just somewhat personal preference or something more valid?

Thanks in advance,
--
Chet
ng******@NOcharterSPAM.net (remove NO.....SPAM)

推荐答案

Chet写道:
我知道那个id'应该只用于
页面上的唯一元素,但是否则使用class或id有什么不同呢?
I know that id''s should only be used for a unique element on a
page, but what is the difference of using class or id otherwise?




在级联中使用id描述的属性比具有类的属性具有更多的重量
权重。所以如果你把它们都应用到同一个元素中,那么id

的属性就会否定它们的类别。

例如:


风格:

#content {color:black; }

..fancy {border:2px点缀橙色;颜色为橙色; }


html:

< div id =" content" class =" fancy">

< p>此文字仍为黑色,但它的橙色点缀为

border。< / p>

< / div>


您可以通过添加''!important''来重新覆盖id的属性。

对于类的属性。所以如果这个例子的类是

..fancy {border:2px点缀橙色;颜色:橙色!重要;文本

的paragraf也会呈现为橙色。


其次,在超链接中你可以通过添加''#id_here'来引用id '到

链接的结尾。 < a href =" foo / bar.html#id_here">使用该id跳转到视口右上角的元素的位置

主流grafical浏览器的
(即,如果有足够的内容

在该点下面填充视口的剩余高度)。

-


Barbara
http://home.wanadoo.nl/b .de.zoete / html / weblog.html
http://home.wanadoo.nl/b.de.zoete/html/webontwerp.html



In the cascade the properties that are described with an id have more
weight than the properties with a class. So the properties of the id
overrule those of the class if you apply them both to the same element.
For example:

style:
#content { color:black; }
..fancy { border:2px dotted orange; color:orange; }

html:
<div id="content" class="fancy">
<p>This text will still be black, but it does have a orange dotted
border.</p>
</div>

You can overrule the properties of the id again, by adding ''!important''
for the properties of the class. So if the class of the example had been
..fancy { border:2px dotted orange; color:orange !important; } the text
of the paragraf would get rendered as orange too.

Secondly, in hyperlinks you can refer to an id by adding ''#id_here'' to
the end of the link. <a href="foo/bar.html#id_here"> makes the position
of the element with that id jump to the top right corner of the viewport
of the mainstream grafical browsers (that is, if there is enough content
underneath that point to fill the remaining height of the viewport up).
--

Barbara

http://home.wanadoo.nl/b.de.zoete/html/weblog.html
http://home.wanadoo.nl/b.de.zoete/html/webontwerp.html


Chet写道:
Chet wrote:
我知道id'只应该用于
页面上的唯一元素,但是否则使用class或id有什么区别?
我一直在制作一些无表格的样本来学习CSS,但是我已经养成了只使用课堂的习惯。在我制作的其中一个页面中,我将唯一的div类更改为id',并且
html文件大小仅减少了23个字节,而css文件减少了几个字节。差别不大。
这只是个人偏好还是更有效?
提前致谢,
-
切特
ng ****** @ NOcharterSPAM.net (删除NO ..... SPAM)
I know that id''s should only be used for a unique element on a
page, but what is the difference of using class or id otherwise? I''ve been making a few sample tableless pages to learn css, but
I''ve gotten into the habit of using only class. In one of the
pages I made, I changed the unique div classes to id''s and the
html file size only decreased 23 bytes and the css file decreased
a few bytes. Not much of a difference. Is it just somewhat personal preference or something more valid? Thanks in advance,
--
Chet
ng******@NOcharterSPAM.net (remove NO.....SPAM)



一个圆圈说只使用一个ID。

我的circle说BULLSHIT。

两者都可以根据需要经常使用。

如果有使用限制,那么为什么页面可以使用多个?

这纯粹是一个偏好问题。

http://www.htmlgoodies.com/beyond/classid.html


这些ID怎么样?

ID命令的工作方式与CLASS命令完全相同。

存在的原因是允许您将这些样式表模型

合并到JavaScript或DHTML中。因此,除非您尝试将这些与

JavaScript一起使用,否则请坚持使用CLASS命令。您可以使用ID命令,但它不会比CLASS命令做任何不同或更好的事情。


参见: http://css-discuss.incutio.com/?page=ClassesVsIds


我认为这一切归结为你问的对象。


One circle says "use only one ID".
My circle says "BULLSHIT".
Both can be used as often as desired.
If there was a limit of use, then why does the page work with more than one?
It''s purely a matter of preference.

http://www.htmlgoodies.com/beyond/classid.html

What About Those IDs?
The ID command works exactly the same way as the CLASS command. The
reason it exists is to allow you to incorporate these Style Sheet models
into JavaScript or DHTML. So, unless you are attempting to use these with
JavaScript, stick with the CLASS command. You can use the ID command, but it
won''t do anything different or better than the CLASS command.

See also: http://css-discuss.incutio.com/?page=ClassesVsIds

I think it all boils down to whom you ask.


Richard写道:
Richard wrote:
Chet写道:
Chet wrote:
>我知道id'只应用于
>上的唯一元素。页面,但是使用class或id有什么不同呢?
> I know that id''s should only be used for a unique element on a
> page, but what is the difference of using class or id otherwise?



一个圈子说只使用一个ID。
我的圈子说BULLSHIT。



One circle says "use only one ID".
My circle says "BULLSHIT".




我不记得任何对规格的引用,说明你只能在页面上有一个id。我知道你应该在页面上只使用一次独特的

id。它毕竟是/标识符/并且如果它在同一页面中多次出现则很难识别任何东西。

所以:

- 您只能在页面上使用一次唯一ID;

- 您可以在页面上使用尽可能多的不同ID;

- 您可以在任意数量的页面上使用唯一的ID,一次在任何页面上。


参考:

< http://www.w3 .org / TR / 2002 / WD-CSS21-20020802 / selector.html#id-selectors>

-


Barbara

http://home.wanadoo.nl/ b.de.zoete / html / weblog.html
http://home.wanadoo.nl/b.de.zoete/html/webontwerp.html



I dont remember any reference to a specification that says you can have
only one id on a page. I do know that you are supposed to use a unique
id only once on a page. It is afterall an /identifier/ and it is hard to
identify anything if it occurs more than once in the same page.
So:
- you can use a unique id only once on a page;
- you can use as many different id''s on a page as you like;
- you can use a unique id on as many pages as you like, once on any of them.

Reference:
<http://www.w3.org/TR/2002/WD-CSS21-20020802/selector.html#id-selectors>
--

Barbara

http://home.wanadoo.nl/b.de.zoete/html/weblog.html
http://home.wanadoo.nl/b.de.zoete/html/webontwerp.html


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

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