Why would font names need quotes?

后端 未结 5 485
青春惊慌失措
青春惊慌失措 2020-12-02 17:58

As far as I know, one needs to use double or single quotes for fonts if they contain spaces, like:

font-family: \"Times New Roman\", Times; 
font-family: \'T         


        
相关标签:
5条回答
  • 2020-12-02 18:35

    I've just learned myself that the quotes aren't ever necessary but rather recommended. We all learn something new every day.

    The other place you see them is in css properties that require a url

    background:url('hithere.jpg');
    background:url(hithere.jpg);
    

    Both those statements are going to work exactly the same. Same goes for the fonts, which type of quote you use is irrelevant in this case, just be consistent in how YOU do things and that is all that really matters.

    0 讨论(0)
  • 2020-12-02 18:45

    You can always put a specific font family name in quotes, double or single, so Arial, "Arial", and 'Arial' are equivalent. Only the CSS-defined generic font family names like sans-serif must be written without quotes.

    Contrary to popular belief, a font name consisting of space-separated names such as Times New Roman need not be quoted. However, the spec recommends “to quote font family names that contain white space, digits, or punctuation characters other than hyphens”

    0 讨论(0)
  • 2020-12-02 18:55

    When to quote font-family:

    Optional

    font-family: Times New Roman;         /* These are equivalent */
    font-family: 'Times New Roman';
    font-family: "Times New Roman";
    
    font-family: Unique Ode™                                                                     
    0 讨论(0)
  • 2020-12-02 18:56

    For two reasons;

    1. When an actual font-family name shares the same name as a generic family name, to avoid confusion with keywords with the same names e.g.

    p {font-family: 'Shift', sans-serif;}

    1. When there are more than one word in a font-family name e.g.

    p {font-family: 'Times New Roman', ..... , a generic family name here ;}

    0 讨论(0)
  • 2020-12-02 19:00

    You have to use quotes when there is a space in the font name. If there is no space in the font name, it's best practice to leave them off, although it won't hurt anything but your file size to have them still.

    Examples:

    font-family: "Times New Roman", Times; // ok, and best practice
    font-family: Times New Roman, Times; // incorrect, Browser won't be able to find Times New Roman
    font-family: "Times New Roman", "Times"; // ok, but has extraneous quotes around Times
    
    0 讨论(0)
提交回复
热议问题