Is it correct to use single quotes for HTML attrib

2019-01-10 21:38发布

Recently I've been seeing a lot of this:

<a href='http://widget-site-example.com/example.html'>
    <img src='http://widget-site-example.com/ross.jpg' alt='Ross&#39;s Widget' />
</a>

Is it valid to use single quotes in HTML? As I've highlighted above it's also problematic because you have to escape apostrophes.

15条回答
干净又极端
2楼-- · 2019-01-10 22:41

It's easier when you want to embed double quotes.

查看更多
Evening l夕情丶
3楼-- · 2019-01-10 22:41

In ASP.NET, it's easier to use single quotes if you're using data-binding expressions in attributes:

<asp:TextBox runat="server" Text='<%# Bind("Name") %>' />
查看更多
一夜七次
4楼-- · 2019-01-10 22:42

You should avoid quotes altogether.

In your example only one quoted attribute actually needed quotes.

<!-- best form -->
<a href=http://widget-site-example.com/example.html>
  <img src=http://widget-site-example.com/ross.jpg alt='Ross&#39;s Widget' />
</a>

If you do use quotes, there is no hard and fast rule, but I've seen most commonly single quotes, with double quotes on the inside if necessary.

Using double quotes won't pass some validators.

查看更多
登录 后发表回答