CSS属性选择器:引号上的规则("','或none?) [英] CSS attribute selectors: The rules on quotes (", ' or none?)

查看:288
本文介绍了CSS属性选择器:引号上的规则("','或none?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题已经扰乱我一段时间了。当写一个CSS选择器来比较元素的属性,像这样。

  a [rel =nofollow] 

我从来不知道我应该用什么引号。他们是真的有必要吗?



基本上,这是什么规格,因为我在网站上找不到它。



这些都是有效的吗?

  a [rel =nofollow] 
a [rel ='nofollow']
a [rel = nofollow]


解决方案

我在此处更详细地介绍了这个主题:



您通常可以省略引号,只要属性值为字母数字(但是,有一些例外 - 请参阅所有细节)。无论如何,我发现这是一个好的做法,无论如何,如果你需要它们,即 a [href ^ = http://] 不会工作, a [href ^ =http://] 将。



CSS规范中的章节。


This question has been bugging me for a while now. When writing a CSS selector that compares against an element's attribute like so.

a[rel="nofollow"]

I never know what I should be doing with the quotation marks. Are they really necessary?

Basically, what is the specification for this because I can't find it on the web site.

Are all of these considered valid?

a[rel="nofollow"]
a[rel='nofollow']
a[rel=nofollow]

解决方案

I’ve written more extensively on the subject here: Unquoted attribute values in HTML and CSS.

I’ve also created a tool to help you answer your question: http://mothereff.in/unquoted-attributes

You can usually omit the quotes as long as the attribute value is alphanumeric (however, there are some exceptions — see the linked article for all the details). Anyhow, I find it to be good practice to add the quotes anyway in case you need them, i.e. a[href^=http://] won’t work, but a[href^="http://"] will.

The article I mentioned links to the appropriate chapters in the CSS spec.

这篇关于CSS属性选择器:引号上的规则("','或none?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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