HTML - Is it necessary to enclose a call to the cs

2019-09-09 17:37发布

问题:

This question already has an answer here:

  • HTML attribute with/without quotes 6 answers

I have a .html file and ist .css file. I call the .css trough the instruction

<link rel= stylesheet href= mystyle.css>

But if I use

<link rel= "stylesheet" href= "mystyle.css">

then nothing changes in my page. Is there some difference between using the quotes or not?

回答1:

I believe the spec defines it to have quotes, but some browsers go beyond and will know what you meant.

EDIT: I was wrong! From the spec

Attributes are placed inside the start tag, and consist of a name and a value, separated by an "=" character. The attribute value can remain unquoted if it doesn't contain space characters or any of " ' ` = < or >. Otherwise, it has to be quoted using either single or double quotes. The value, along with the "=" character, can be omitted altogether if the value is the empty string.

http://www.w3.org/html/wg/drafts/html/master/introduction.html#restrictions-on-content-models-and-on-attribute-values



回答2:

In HTML5 you don't need to use quotes for any attributes. But, sometimes you have to because of knowing where a string ends. In HTML4.1 and XHTML you have to use quotes.

It's a good practice to use quotes anyway, even if you don't need them.