Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escape quote with font family

Tags:

html

css

fonts

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?

like image 833
Mika H. Avatar asked May 11 '13 03:05

Mika H.


People also ask

Do you need quotes around font family?

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.

What is the example of font family?

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".

How do you escape a single quote?

No escaping is used with single quotes. Use a double backslash as the escape character for backslash.


2 Answers

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>
like image 73
hjpotter92 Avatar answered Oct 06 '22 19:10

hjpotter92


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 &quot;, e.g.

style="font-family:&quot;Times New Roman&quot;, 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.

like image 31
Jukka K. Korpela Avatar answered Oct 06 '22 18:10

Jukka K. Korpela