Is quoting the value of url() really necessary?

后端 未结 7 2094
情话喂你
情话喂你 2020-11-22 09:40

Which of the following should I use in my stylesheets?

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


        
7条回答
  •  半阙折子戏
    2020-11-22 10:37

    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)

    A URL is a pointer to a resource and is a functional notation denoted by . The syntax of a is:
    = url( * )

    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.

    That means, the url(...) syntax is supposed to be a functional notation, which takes a string and a url-modifier as parameters. 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. 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.

提交回复
热议问题