css - CSS 属性选择器值中的值是否需要用引号引起来?

标签 css css-selectors

<分区>

例如:

a[href="val"]

“val”是否需要用引号引起来?单人或双人都可以接受吗?整数呢?

最佳答案

TLDR:除非值满足 identifier specification for CSS2.1,否则需要引号

CSS 规范可能会说它们是可选的,但现实世界却呈现出不同的故事。在与 href 属性进行比较时,您需要使用引号(在我非常有限的测试中使用单引号或双引号 - 最新版本的 FF、IE、Chrome。)

有趣的是,@Pekka 引用的 css 规范链接恰好在其特定于 href 的示例周围使用了引号。

这不仅仅是由于非字母字符(如句点或斜杠)给这种独特的情况带来了引用要求——如果你只使用“domain.com”中的“域”

好吧,这里的每个答案都是错误的(包括我自己之前的答案。)CSS2 规范没有阐明选择器部分本身是否需要引号,但 CSS3 规范确实如此并将规则作为 CSS21 实现引用:

http://www.w3.org/TR/css3-selectors/

Attribute values must be CSS identifiers or strings. [CSS21] The case-sensitivity of attribute names and values in selectors depends on the document language.

这是标识符信息:

http://www.w3.org/TR/CSS21/syndata.html#value-def-identifier

In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A0 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit, two hyphens, or a hyphen followed by a digit. Identifiers can also contain escaped characters and any ISO 10646 character as a numeric code (see next item). For instance, the identifier "B&W?" may be written as "B\&W\?" or "B\26 W\3F".

我的回答似乎是正确的,但那是因为“~=”是一个空白选择器比较器,所以它永远不会匹配 href 值内的部分字符串。但是,“*=”比较器确实有效。像“domain”这样的部分字符串确实适用于匹配 href='www.domain.com'。但是检查完整的域名是行不通的,因为它违反了标识符规则。

关于css - CSS 属性选择器值中的值是否需要用引号引起来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3851091/

相关文章:

css - 相同的特异性,考虑到布局后,:first-letter always wins?

css - nth-child 可以在一页上工作,但不能在另一页上工作

c# - CSS 问题 : <hr> line not showing in non-compatibility view in IE 8

css - css中的.class1.class2和.class1.class2有什么区别?

CSS 并为具有特定 ID 的所有控件应用样式

python Selenium : How to wait and switch to a dynamic iframe?

html - 如何在 CSS 中旋转 div 中的文本以使我的数字看起来正确(我已经旋转了 div 本身只是想让数字旋转))

css - 使用下拉菜单停止页面内容移动

javascript - 如何在sessionStorage中存储崩溃?

javascript - 停止特定元素上的传播