HTML5 Compliant - Trailing Space in Class Attribute

那年仲夏 提交于 2019-12-01 01:59:09

问题


I know that technically HTML5 is a 'living spec' but I'm wondering if it's compliant to have trailing spaces inside of a class name. I didn't see any reference to this scenario in the spec, but one of my teammates said it was invalid. Maybe I missed something?

It'd be a pain to trim those spaces (I'm working inside of a large Java ecom application) and I want to know if it's worth doing for SEO, validation or any other purpose. I get the feeling it's not... haha


回答1:


As we can see in the link below, there is no restriction on what the developer can use in the class attribute.

http://www.w3.org/html/wg/drafts/html/master/dom.html#classes

In fact, after saying what the classes are and that they're used spliting in the spaces, the author(s) says:

There are no additional restrictions on the tokens authors can use in the class attribute, but authors are encouraged to use values that describe the nature of the content, rather than values that describe the desired presentation of the content.

Our colleagues here have tested and it successfully passes W3C validation, so I can't guess why your friend thought it was invalid.




回答2:


Yes, it is compliant.

From http://www.w3.org/html/wg/drafts/html/master/dom.html#classes:

The attribute, if specified, must have a value that is a set of space-separated tokens representing the various classes that the element belongs to.

From http://www.w3.org/html/wg/drafts/html/master/infrastructure.html#set-of-space-separated-tokens:

A string containing a set of space-separated tokens may have leading or trailing space characters.




回答3:


According to http://validator.w3.org/ under a <!DOCTYPE html> the following validates successfully.

<div class=" name1 name2 "></div>

Leaving a trailing and leading space may be acceptable, but it's not pretty and some people do not consider it to be best a practice.




回答4:


Anything that knows how to handle class attributes should be fine with this. It is possible to give a tag multiple classes by separating them with spaces so anything which reads them had better know how to deal with spaces.



来源:https://stackoverflow.com/questions/21439859/html5-compliant-trailing-space-in-class-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!