CSS:一个 HTML 元素可以拥有的类数量是否有限制?
CSS 允许一个 HTML 元素拥有多个类:
<div class="cat persian happy big"> Nibbles </div>
但是每个项目允许有多少个类有限制吗?
CSS allows an HTML element to have multiple classes:
<div class="cat persian happy big"> Nibbles </div>
But is there a limit on how many classes are allowed per item?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您仅受 (X)HTML 属性值的最大长度限制,这个答案。
浏览器通常非常宽容违反标准的行为,因此个别浏览器可能允许更长的类属性。此外,您还可以通过 JavaScript 将几乎无限数量的类添加到 DOM 元素,但受到浏览器可用内存量的限制。
对于所有意图和目的,没有限制。我假设你是出于好奇才问的;不用说,如果您非常担心会达到此限制,那么您就做错了。
You're only limited by the maximum length of an (X)HTML attribute's value, something covered well by this answer.
Browsers are often very forgiving of standards violations, so individual browsers may allow much longer class attributes. Additionally you are likely able to add a practically infinite number of classes to a DOM element via JavaScript, limited by the amount of memory available to the browser.
For all intents and purposes, there is no limit. I'm assuming you're asking out of curiosity; it goes without saying that if you're seriously worried about hitting this limit, you've done something very wrong.
不。我不认为,我曾经遇到过任何这样的限制/
编辑:抱歉随意的评论。
根据规范,没有任何限制,但有人试图达到这个限制,似乎是 Opera 的限制,Safari 支持超过 4000 个类,而 Firefox 至少支持 2000 个类!
来源:http://kilianvalkhof.com/ 2008/css-xhtml/每个元素支持的类的最大数量/
No. I don't think, I have ever come across any such limit/
EDIT: Sorry for the casual remark.
According to the specifications, there isn't any limit but someone has tried to reach this limit and it seems the limit for Opera, Safari supported well over 4000 classes, and Firefox at least 2000 classes!
Source: http://kilianvalkhof.com/2008/css-xhtml/maximum-number-of-supported-classes-per-element/
没有技术限制(除非浏览器可能消耗的内存量),但应该认真考虑在任何元素上加载类,因为浏览器必须解析所有类,应用这些样式并呈现页面。
此外,如果您需要在 DOM 中搜索特定类的元素,并且元素包含大量类,并且 JavaScript 解释器必须解析大量类,您可能会遇到性能问题。
There is no technical limit (barring the amount of memory the browser may be consuming), but one should heavily consider having loads of classes on any element as the browser will have to parse all the classes, apply those styles and render the page.
Also, if you need to search the DOM for elements of a particular class and elements contain loads of classes, you may likely see a performance issue if the JavaScript interpreter has to parse loads of classes.