HTML attribute with/without quotes
Is there any difference between
<iframe src="www.example.com" width=100%></iframe>
and
<iframe src="www.example.com" width="100%"></iframe>
I've tried both and both seem to work, but I'm asking just in case there's something I need to be careful with (like maybe units other than %, etc.)
It is all about the true validity of HTML markup. It's for what W3C (WWW Consortium) work for. Many things might work in HTML but they have to be validated in order to be more carefully recognized by the web browser. You can even omit the <html>
and </html>
tags at the start and end, but it is not recommended at all, nobody does it and it is considered as a 'bad code'.
Therefore, it is more valid to put them in quotes.
There is no practical difference except
Otherwise, the quotation marks are really needed only if the attribute value contains a space, a line break, an Ascii quotation mark ("), an Ascii apostrophe ('), a grave accent (`), an equals sign (=), a less than sign (<), or a greater than sign (>). So style = width:20em
would work (though it might be seen as somewhat obscure), whereas style = width: 20em
would not – due to the space, you would need to write style = "width: 20em"
.
Many people always write quotation marks around all attribute values, for simplicity. Others think that quotation marks make the code a bit messy, so they omit them when possible.
Quite independently of this, src="www.example.com"
means a relative URL reference, not what people expect to mean. You probably meant src="http://www.example.com"
.
According to the W3C there are four types of attribute syntax:
These really apply to HTML5, however when referring to < HTML5 the W3C says that quotes (single or double) are required based on the doctype (eg strict, transitional, etc.) used.
链接地址: http://www.djcxy.com/p/87964.html下一篇: 带/不带引号的HTML属性