Is it correct to use single quotes for HTML attributes?

前端 未结 15 2557
没有蜡笔的小新
没有蜡笔的小新 2020-12-01 05:57

Recently I\'ve been seeing a lot of this:


    

        
相关标签:
15条回答
  • 2020-12-01 06:38

    I find using single quotes is handy when dynamically generating HTML using a programming language that uses double quote string literals.

    e.g.

    String.Format("<a href='{0}'>{1}</a>", Url, Desc)
    
    0 讨论(0)
  • 2020-12-01 06:39

    When using PHP to generate HTML it can be easier to do something like:

    $html = "<img src='$url' />";
    

    than concatenating a string with a variable with a string, as PHP parses variables in double-quoted strings.

    0 讨论(0)
  • 2020-12-01 06:39

    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") %>' />
    
    0 讨论(0)
  • 2020-12-01 06:40

    If you want to the html to be valid html4 or xhtml then both single-quotes or double-quotes will work for attributes. HTML4 can be validated here: https://validator.w3.org/

    If you are supporting only modern browsers (ie10 and higher) then you can use the html5 syntax which allows single quotes, double quotes, and no quotes (as long as there are no special characters in the attributes' value). HTML5 can be validated here: https://validator.w3.org/nu

    0 讨论(0)
  • 2020-12-01 06:41

    What's against single quotes?

    You can have single/double quotes all over your html code without any problem, as long as you keep the same quoting style inside a current tags ( many browser won't complain even about this, and validation just want that if you start with a quote, end with the same, inside the same propriety )

    Free support to random quoting styles!!! (yay ;D )

    0 讨论(0)
  • 2020-12-01 06:42

    Single quotes generate a cleaner page with less clutter. You shouldn't be escaping them in HTML strings and it's your choice which to use... my preference is single quotes normally and if I need to include a single quote in the string (e.g. as delimiters for a string inside the string), I use double quotes for one & single for the other.

    0 讨论(0)
提交回复
热议问题