Google fonts URL break HTML5 Validation on w3.org
Asked Answered
S

5

197

I load 3 fonts in different sizes using this HTML tag:

<link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Open+Sans:400,600,300,800,700,400italic|PT+Serif:400,400italic|Bree+Serif">

Till ~1/2 weeks ago this was supported by w3.org validator for HTML5; now it gives this error:

 Line 14, Column 163: Bad value http://fonts.googleapis.com/css?family=Open+Sans:400,600,300,800,700,400italic|PT+Serif:400,400italic|Bree+Serif for attribute href on element link: Illegal character in query: not a URL code point.

What's the things the W3C Markup Validator does not like now?

Survive answered 17/3, 2014 at 22:40 Comment(0)
S
358

URL encode the | (pipe characters) in the href attribute (%7C):

<link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Open+Sans:400,600,300,800,700,400italic%7CPT+Serif:400,400italic%7CBree+Serif">
Splenius answered 18/3, 2014 at 2:20 Comment(4)
Is this an issue with Google generated URL or an issue with w3 validator? Does any spec actually require pipe character to be encoded in HTML attribute?Eckmann
@MikkoRantalainen, RFC 1738 notes that the pipe character is unsafe: Other characters are unsafe because gateways and other transport agents are known to sometimes modify such characters. These characters are "{", "}", "|", "\", "^", "~", "[", "]", and "`".Splenius
I would expect raw UTF-8 to be valid in UTF-8 encoded HTML without encoding other characters but those used for HTML e.g. &, " and '. And those special characters would need to be encoded by HTML rules (e.g. &amp etc). The user agent is then expected to follow RFC 3987 and convert the IRI to percent encoded UTF-8 before submitting it over HTTP (tools.ietf.org/html/rfc3987).Eckmann
Interestingly, my Google Font link to Open Sans didn't contain a pipe, but rather a blank between "Open" and "Sans". Replacing the blank by a "+" solved the problem for me.Densimeter
S
10

There are 2 ways to fix this validation problem:

  1. URL encode the | (vertical bar/line) character in the href attribute of the link element (as %7C) :

    <link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Open+Sans:400,600,300,800,700,400italic%7CPT+Serif:400,400italic%7CBree+Serif">
    
  2. Separate fonts inclusion with multiple link elements :

    <link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Open+Sans:400,600,300,800,700,400italic">
    <link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=PT+Serif:400,400italic">
    <link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Bree+Serif">
    
Shortcut answered 24/5, 2015 at 14:48 Comment(2)
I know this is older post, but does anyone know if there is any performance (dis)advantage to separating <link> tags? Does Google compress if multiple fonts are in one call?Birthwort
@PatrickMoore 2 things: a) your server upload/response speed vs your end-user download speed b) time to create another connection (server response time vs end-user response time), theoretically, if you are loading 2 "heavy" fonts and your page is otherwise loading fast, loading them separately (in a paralel way) could result in faster load. But it really depends on a) & b)Urdar
C
6

http://www.utf8-chartable.de/

You must replace the character | by its corresponding UTF-8 character, which gives

href="http://fonts.googleapis.com/css?family=Cookie%7cAmaranth%7cKaushan+Script%7cCousine%7cBilbo+Swash+Caps%7cRancho&amp;effect=shadow-multiple"
Cupriferous answered 25/3, 2014 at 9:5 Comment(0)
E
-3

My case was crazy line break character. Please see the attached image.enter image description here

Earflap answered 26/5, 2018 at 13:35 Comment(0)
G
-4

I scape & with "& amp;" and works fine, example:

<link href='http://fonts.googleapis.com/css?family=Open+Sans+Condensed:300,700&amp;subset=latin,latin-ext' rel='stylesheet' type='text/css' />
Gabriel answered 15/5, 2015 at 8:59 Comment(1)
This question is about | character, not & ;-)Urdar

© 2022 - 2024 — McMap. All rights reserved.