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

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

问题描述

这个问题已经困扰了我一段时间了.当编写一个 CSS 选择器来比较一个元素的属性时.

a[rel="nofollow"]

我从来不知道我应该用引号做什么.它们真的有必要吗?

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

所有这些都被认为是有效的吗?

a[rel="nofollow"]a[rel='nofollow']一个 [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 属性选择器:引号的规则(“,"还是没有?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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