逃脱报价与字体家族

Mik*_* H. 3 html css fonts

我想使用font-family CSS

<div style="font-family:"Times New Roman", Times, serif"> Hello world! </div>
Run Code Online (Sandbox Code Playgroud)

对于font-family"Times New Roman",我应该逃避报价吗?或者我可以不加引号使用它吗?

hjp*_*r92 8

使用单引号:

<div style="font-family: 'Times New Roman', Times, serif"> Hello world! </div>
Run Code Online (Sandbox Code Playgroud)

或者(谢谢@Matt):

<div style='font-family: "Times New Roman", Times, serif'> Hello world! </div>
Run Code Online (Sandbox Code Playgroud)


Juk*_*ela 6

在HTML中的属性值中,您不能使用值内部的值的分隔符,原因相当明显:在style="font-family:"Times New Roman", Times, serif"第二个引用中终止值,因此只有属性style="font-family:"后跟构成语法错误的内容.

有几种方法可以解决这个问题.

1)可以使用实体引用来编写分隔符&quot;,例如,

style="font-family:&quot;Times New Roman&quot;, Times, serif"
Run Code Online (Sandbox Code Playgroud)

2)如果值包含双引号,您可以使用不同的分隔符,即单引号:

style='font-family:"Times New Roman", Times, serif'
Run Code Online (Sandbox Code Playgroud)

3)根据属性值的性质,可以在值内使用另一个字符.在这种情况下,由于值是CSS代码而CSS也接受单引号,您可以编写:

style="font-family:'Times New Roman', Times, serif"
Run Code Online (Sandbox Code Playgroud)

4)根据属性值的性质,可以省略值内的字符.在这种情况下,由于值中引用的字符串是CSS中的字体名称,并且由于引号不是必需的(与普遍看法相反),在这种简单的情况下,您可以编写:

style="font-family:Times New Roman, Times, serif"
Run Code Online (Sandbox Code Playgroud)

最后,您可以通过使用外部样式表或至少一个style元素而不是在属性中编写CSS代码来避免此问题.