简体   繁体   English

引用 url() 的值真的有必要吗?

[英]Is quoting the value of url() really necessary?

Which of the following should I use in my stylesheets?我应该在我的样式表中使用以下哪一项?

/* Example #1: */ background-image: url(image.png);
/* Example #2: */ background-image: url("image.png");
/* Example #3: */ background-image: url('image.png');

What does the W3C specify as the correct way ? W3C 指定的正确方法是什么?

The W3C says quotes are optional, all three of your ways are legal. W3C 说引号是可选的,所有三种方式都是合法的。

Opening and closing quote just need to be the same character.开始和结束引号只需要是相同的字符。

If you have special characters in your URL, you should use quotes or escape the characters (see below).如果 URL 中有特殊字符,则应使用引号或转义字符(见下文)。

Syntax and basic data types语法和基本数据类型

The format of a URI value is 'url(' followed by optional white space followed by an optional single quote (') or double quote (") character followed by the URI itself, followed by an optional single quote (') or double quote (") character followed by optional white space followed by ')'. URI 值的格式是 'url(' 后跟可选的空格,后跟可选的单引号 (') 或双引号 (") 字符,后跟 URI 本身,后跟可选的单引号 (') 或双引号(") 字符后跟可选的空格,后跟 ')'。 The two quote characters must be the same.两个引号字符必须相同。

Escaping special characters:转义特殊字符:

Some characters appearing in an unquoted URI, such as parentheses, white space characters, single quotes (') and double quotes ("), must be escaped with a backslash so that the resulting URI value is a URI token: '\(', '\)'.出现在不带引号的 URI 中的某些字符,例如括号、空格字符、单引号 (') 和双引号 ("),必须使用反斜杠进行转义,以便生成的 URI 值是 URI 标记:'\(', '\)'。

Better use quotes because it's recommended by the newest standard and there're fewer edge cases.更好地使用引号,因为它是最新标准推荐的,并且边缘情况更少。

According to the newest Editor's Draft of CSS Values and Modules Level 3 (18 December 2015)根据最新的CSS Values and Modules Level 3 编辑草案(2015 年 12 月 18 日)

A URL is a pointer to a resource and is a functional notation denoted by <url> . URL 是指向资源的指针,是由<url>表示的功能符号。 The syntax of a <url> is: <url>的语法是:
<url> = url( <string> <url-modifier>* )

The unquoted version is only supported for legacy reasons and needs special parsing rules (for escape sequences, etc.), thus being cumbersome and not supporting url-modifiers.不带引号的版本仅出于遗留原因而受支持,并且需要特殊的解析规则(用于转义序列等),因此很麻烦并且不支持 url-modifiers。

That means, the url(...) syntax is supposed to be a functional notation, which takes a string and a url-modifier as parameters.这意味着, url(...)语法应该是一个函数式表示法,它接受一个字符串和一个 url 修饰符作为参数。 Use the quote notation (which produces a string token) would be more standard-compliant and introduce less complexity.使用引号表示法(它产生一个字符串标记)将更符合标准并引入更少的复杂性。

@SimonMourier's comment in the top answer is wrong, because he looked for the wrong spec. @SimonMourier 在最佳答案中的评论是错误的,因为他查找了错误的规范。 The url-token type is only introduced for the legacy special parsing rules, so that's why it does not have anything to do with quotes. url-token类型只为遗留的特殊解析规则引入,所以它与引号没有任何关系。

Here is what the W3 CSS 2.1 specification says:以下是 W3 CSS 2.1 规范所说的:

The format of a URI value is 'url(' followed by optional white space followed by an optional single quote (') or double quote (") character followed by the URI itself, followed by an optional single quote (') or double quote (") character followed by optional white space followed by ')'. URI 值的格式是 'url(' 后跟可选的空格,后跟可选的单引号 (') 或双引号 (") 字符,后跟 URI 本身,后跟可选的单引号 (') 或双引号(") 字符后跟可选的空格,后跟 ')'。 The two quote characters must be the same.两个引号字符必须相同。

Source: http://www.w3.org/TR/CSS21/syndata.html#uri来源: http ://www.w3.org/TR/CSS21/syndata.html#uri

So all of the 3 examples you proposed are correct, but the one that I would choose is the first one because you use less characters and hence the resulting CSS file will be smaller, resulting in less bandwidth usage.因此,您提出的所有 3 个示例都是正确的,但我会选择第一个示例,因为您使用的字符更少,因此生成的 CSS 文件会更小,从而减少带宽使用。

This might feel like that is not important, but high traffic websites prefer to save bandwidth and over lots of css files, and url references in them it make sense to choose the option that make the file smaller... Even because there is no advantage in not doing so .这可能感觉不重要,但是高流量网站更喜欢节省带宽和大量 css 文件,并且其中的 url 引用选择使文件更小的选项是有意义的......即使没有优势在不这样做

Note: you might have to escape characters if urls contain parentheses, commas, white space characters, single quotes or double quotes.注意:如果 url 包含括号、逗号、空格字符、单引号或双引号,您可能需要转义字符。 This might make the url longer than just using quotes (which need less escaping).这可能会使 url 比仅使用引号(需要更少的转义)更长。 Hence you might want to serve a Css file with urls with no quotes only when the overhead of escaping does not make the url longer than just using quotes (which is very rare).因此,只有在转义开销不会使 url 比仅使用引号(这非常罕见)时,您可能希望使用不带引号的 url 提供 Css 文件。

However I would not expect any human being to even consider these edge cases... A Css optimizer would handle this for you... (but sure you need to know about all of this if you are actually writing a css optimizer :P)但是我不希望任何人甚至考虑这些边缘情况......一个Css优化器会为你处理这个......(但如果你真的在写一个css优化器,你肯定需要知道所有这些:P)

Three ways are legal according to the W3C.根据 W3C,三种方式是合法的。 If you have special characters in the name (as space) you should use the second or the third.如果名称中有特殊字符(如空格),则应使用第二个或第三个。

Update for 2021 (most answers are from 2015 and earlier.) 2021 年更新(大多数答案来自 2015 年及更早的时间。)

Quotes are optional, however some characters (if used) will need to be escaped.引号是可选的,但是某些字符(如果使用)需要转义。

From MDN:来自 MDN:

A URL, which is a relative or absolute address, or pointer, to the web resource to be included, or a data URI, optionally in single or double quotes. URL,它是要包含的 Web 资源的相对或绝对地址或指针,或数据 URI,可选用单引号或双引号。 Quotes are required if the URL includes parentheses, whitespace, or quotes, unless these characters are escaped, or if the address includes control characters above 0x7e.如果 URL 包含括号、空格或引号,则需要引号,除非这些字符被转义,或者如果地址包含 0x7e 以上的控制字符。 Double quotes cannot occur inside double quotes and single quotes cannot occur inside single quotes unless escaped.双引号不能出现在双引号内,单引号不能出现在单引号内,除非转义。 The following are all valid and equivalent:以下都是有效和等效的:

<css_property>: url("https://example.com/image.png")
<css_property>: url('https://example.com/image.png')
<css_property>: url(https://example.com/image.png)

If you choose to write the URL without quotes, use a backslash () before any parentheses, whitespace characters, single quotes ( ' ) and double quotes ( " ) that are part of the URL.如果您选择编写不带引号的 URL,请在作为 URL 一部分的任何括号、空格字符、单引号 ( ' ) 和双引号 ( " ) 之前使用反斜杠 ()。

Source: https://developer.mozilla.org/en-US/docs/Web/CSS/url()来源: https://developer.mozilla.org/en-US/docs/Web/CSS/url()

Example 2 or 3 are best:示例 2 或 3 是最好的:

From W3C: The format of a URI value is 'url(' followed by optional white space followed by an optional single quote (') or double quote (") character followed by the URI itself, followed by an optional single quote (') or double quote (") character followed by optional white space followed by ')'.来自 W3C: URI 值的格式是 'url(' 后跟可选的空格,后跟可选的单引号 (') 或双引号 (") 字符,后跟 URI 本身,后跟可选的单引号 (')或双引号 (") 字符后跟可选的空格,后跟 ')'。 The two quote characters must be the same.两个引号字符必须相同。

Note from the same explanation, Example 1 is acceptable, if appropriate characters are escaped.从相同的解释中注意,如果适当的字符被转义,示例 1 是可以接受的。

According to Google CSS Coding Style根据谷歌 CSS 编码风格

Do not use quotation marks in URI values (url()).不要在 URI 值 (url()) 中使用引号。

Exception: If you do need to use the @charset rule, use double quotation marks—single quotation marks are not permitted.例外:如果您确实需要使用@charset 规则,请使用双引号——不允许使用单引号。

I had:我有:

a.pic{
    background-image: url(images/img (1).jpg);
}

It took me a while to understand that the in-filename's closing parenthesis was breaking the rule.我花了一段时间才明白,文件名的右括号违反了规则。

So it is not mandatory but, even if quoting is not-so-well understood by older browsers, it could save you some headache in fairly complex dynamically generated pages.所以它不是强制性的,但是,即使旧浏览器不那么理解引用,它也可以在相当复杂的动态生成页面中为您省去一些麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM