3
votes

In Google Fonts documentation, it suggests that loading multiple Google Fonts can be done via pipe character (|), for example:

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

With HTML validator, it said:

Bad value http://fonts.googleapis.com/css?family=PT+Sans:400,700|Amaranth:400italic for attribute href on element link: Illegal character in query: | is not allowed.

Which website should I trust? Of course I can separate two fonts in two requests as follow, but the load time will be doubled.

<link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700" />
<link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Amaranth:400italic" />

p.s. the DOCTYPE is HTML5.

2
... or you can just URL encode it.BoltClock
Thanks for posting this and sorry the checker message isn’t more helpful here. But going forward I hope the small improvement I just made to the error message helps others figure out what they need to change in order to avoid that error being raised by the checker.sideshowbarker

2 Answers

3
votes

As @BoltClock suggests, escaping the pipe character to %7C is the best choice.

<link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=PT+Sans:400,700%7CAmaranth:400italic" />

Both fonts loaded correctly, and verified by the network response of the above link.

0
votes

Updated: rephrased to properly communicate the position with the degree of nuance intended (earlier phrasing may have misrepresented my position as "never use validators" when, in fact, my intention was "choose browser behavior over validators in the event of disagreement").

In this particular case, the validator is correct; the '|' symbol needs to be percent-encoded. However, you should keep in mind that validators are not perfect and that many of them are overly strict in their processing; what really matters is how the website loads in the browsers that your users use, not what a particular validator happens to think. If there is a disagreement between a validator and what experimentation in browsers shows is the case, the browser is always "correct" from the standpoint of website development (it may not be correct vis-a-vis what is captured in W3C standards, but "the web platform" is dictated by de-facto "standards" arising from typical browser behavior... W3C and other standards committees are often well behind browser changes and only fully, formally standardize aspects of the web platform well after it has already been widely adopted).

As @slideshowbarker notes in the comments below, validators and linters can be helpful in the development process to catch errors early on in development (and on this I agree). However, you should be aware that validators are just a tool (and not always correct or sometimes overly noisy); it's important not to waste a significant amount of time making validators happy on code that browsers perfectly accept. And you certainly shouldn't litter your website with validation icons/buttons that simply confuse users who visit the website and don't know or care about the details of HTML.

Because browsers, not validators, are the true "source of truth" for whether a page is behaving properly, in addition to some of these other validator tools, you may wish to add WebDriver tests to your tool arsenal. WebDriver allows you to create integration tests that render your website in various different browsers, which allows you to simulate and verify your website's behavior in various browsers in a much more accurate fashion than standalone validators do. The checks performed by your WebDriver tests are therefore also likely to be more important / less noisy / less ignorable than errors raised by a validator (which may sometimes be overly stringent in their complaints).