I want to use font-family CSS
<div style="font-family:"Times New Roman", Times, serif"> Hello world! </div>
For the font-family "Times New Roman", should I escape the quotes? Or can I use it without quotes?
Quotes are required around font-family names when they are not valid CSS identifiers. For example, a font family name requires quotes around it if it contains $ , ! or / , but does not require quotes just because it contains spaces or a (non-initial) number or underscore.
The name of a font family. For example, "Times" and "Helvetica" are font families. Font family names containing whitespace should be quoted. For example: "Comic Sans MS".
No escaping is used with single quotes. Use a double backslash as the escape character for backslash.
Use single quotes:
<div style="font-family: 'Times New Roman', Times, serif"> Hello world! </div>
or alternatively (Thanks @Matt):
<div style='font-family: "Times New Roman", Times, serif'> Hello world! </div>
Within an attribute value in HTML, you cannot use delimiter of the value inside the value as such, for rather obvious reasons: in style="font-family:"Times New Roman", Times, serif"
, the second quote terminates the value, so that there is just the attribute style="font-family:"
followed by something that constitutes a syntax error.
There are several ways around this.
1) The delimiter can be written using an entity reference like "
, e.g.
style="font-family:"Times New Roman", Times, serif"
2) You can use a different delimiter, namely a single quote, if the value contains a double quote:
style='font-family:"Times New Roman", Times, serif'
3) Depending on the nature of the attribute value, it might be possible to use another character inside the value. In this case, since the value is CSS code and CSS accepts single quotes too, you can write:
style="font-family:'Times New Roman', Times, serif"
4) Depending on the nature of the attribute value, it might be possible to omit the character inside the value. In this case, since the quoted string in the value is a font name in CSS and since the quotes are not required (contrary to popular belief) in a simple case like this, you can write:
style="font-family:Times New Roman, Times, serif"
Finally, you can avoid this problem by using an external style sheet or at least a style
element instead of writing CSS code in an attribute.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With