Welcome to WebmasterWorld Guest from 54.159.120.168

Forum Moderators: incrediBILL

Meta charset

     
4:10 am on Jan 19, 2017 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Mar 15, 2013
posts: 336
votes: 29


I have this listed in my HTML, but W3 validator says:

A document must not include both a meta element with an http-equiv attribute whose value is content-type, and a meta element with a charset attribute.

So, which <meta> tag should I remove? I'm guessing the second, but I'm not 100% sure.

<!DOCTYPE html>
...
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="UTF-8">
4:22 am on Jan 19, 2017 (gmt 0)

Moderator from US 

WebmasterWorld Administrator keyplyr is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Sept 26, 2001
posts:8035
votes: 286


Yup, it's superfluous to use both.

If the page is HTML then use:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
It also identifies the type of file.

- or -

If you have an Apache server, just use this in your htaccess for the entire site (then the meta tag is not needed.)
AddDefaultCharset utf-8
AddLanguage en .html .htm .php
W3C may give a warning (not an error) that the user may need the charset identifier if they download the file from your server, so if you allow downloading of your pages, then stick to the meta tag method.
5:19 am on Jan 19, 2017 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13610
votes: 416


So, which <meta> tag should I remove? I'm guessing the second, but I'm not 100% sure.

For your HTML 5 DTD, remove the first (the longer version, suitable for HTML 4). All you need is meta charset.
5:32 am on Jan 19, 2017 (gmt 0)

Moderator from US 

WebmasterWorld Administrator keyplyr is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Sept 26, 2001
posts:8035
votes: 286


For your HTML 5 DTD
Guess I missed that <!DOCTYPE html> part :)
8:15 am on Jan 19, 2017 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Mar 15, 2013
posts: 336
votes: 29


Awesome, thanks!

Similar but different question... does it matter where this <meta> tag comes in the layout? Before or after <title>, before or after other <meta> tags, etc? I'm guessing not, but I figure since we're on the subject then I might as well ask.
8:37 am on Jan 19, 2017 (gmt 0)

Moderator from US 

WebmasterWorld Administrator keyplyr is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Sept 26, 2001
posts:8035
votes: 286


No, just somewhere in the HEAD.

Most people put TITLE and DESCRIPTION first, then the other tags but it really doesn't matter.
8:55 am on Jan 19, 2017 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Mar 15, 2013
posts: 336
votes: 29


Good to know, thanks!
9:02 am on Jan 19, 2017 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10813
votes: 53


i normally define the charset before any markup that contains displayable characters.
10:21 am on Jan 19, 2017 (gmt 0)

Senior Member from GB 

WebmasterWorld Senior Member 10+ Year Member

joined:Oct 2, 2003
posts: 986
votes: 30



No, just somewhere in the HEAD.
Most people put TITLE and DESCRIPTION first,
then the other tags but it really doesn't matter.

It should matter.
The advocation or encouragement of sloppy
coding methodology is, unfortunately, all to
prevalent.

Here is the recommended order...


<!DOCTYPE html>
<html lang="en">
<head>

<meta charset="utf-8">
<meta name="viewport" content="width=device-width,height=device-height,initial-scale=1">

<title>untitled document</title>

<link rel="stylesheet" href="screen.css" media="screen">

</head>


birdbrain
11:34 am on Jan 19, 2017 (gmt 0)

Moderator from US 

WebmasterWorld Administrator keyplyr is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Sept 26, 2001
posts:8035
votes: 286


The order of tags in the HEAD is absolutely unimportant. The only relevant factor is what makes sense to the person doing the coding, nothing more. There is nothing "sloppy" about it and no "recommended order."
12:00 pm on Jan 19, 2017 (gmt 0)

Full Member

Top Contributors Of The Month

joined:Nov 13, 2016
posts:348
votes: 49


The order has no impact, however, it's more coherent to define the charset first. To correctly interpret meta tags values, the rendering engine needs to know which charset is used. This means that when the rendering engine reads the meta tags, as long as it doesn't know the charset, it will skip them (put them into a waiting queue of tags to be processed). Then once the charset is detected , it will goes after waiting queue to interpret the meta tags. In practice it doesn't change anything to the result, and it certainly has no significant impact over the rendering speed, but it's more coherent to start with the charset. Exactly as if someone speaks to you, it's more convenient to know in which language "before", the discussion begins.
2:28 pm on Jan 19, 2017 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10813
votes: 53


https://www.w3.org/TR/html5/document-metadata.html#character-encoding-declaration
The element containing the character encoding declaration must be serialized completely within the first 1024 bytes of the document.
7:11 pm on Jan 19, 2017 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13610
votes: 416


Darn, phranque, where do you find this stuff? ;) 1024 characters. Who knew?

:: business with calculator ::

At my text editor's settings, that's 14 full onscreen lines. The only way I could pass that number in a typical <head> is if I put the whole CSS before the charset meta, which strikes me as inane.
i normally define the charset before any markup that contains displayable characters.
Same here. Obviously it only matters if your page title contains non-ASCII characters--and even then, only if you don't also have a Charset line in your config/htaccess. I guess it also makes a difference in the rare case where a document's stylesheet includes non-ASCII characters, most likely in "content:".

Here is the recommended order...
:: further detour to look at the nearest random page ::

I'll be darned. That's exactly the order mine's in, though I'm morally certain it isn't because I've seen that recommendation anywhere. It just seemed the most logical--I mean, how else would you do it? I've also got a meta description, after the other metas but before I start in on stylesheets. That’s working on a general principle of “shorter stuff before longer stuff”.
1:25 am on Jan 20, 2017 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10813
votes: 53


Darn, phranque, where do you find this stuff? ;) 1024 characters. Who knew?

exactly!
every question here is an opportunity to learn.
i have an aversion to publicly expressing an opinion when i know there's an authoritative answer available.
since i can't pretend to have memorized all the standards and specification, i've made a habit of referring to them early and often.

even in the HTML 4.01 Specification (17 years ago), there was a "should" (vs "must") statement on the location of <meta> elements in general:
https://www.w3.org/TR/html4/charset.html#h-5.2.2
META declarations should appear as early as possible in the HEAD element.
2:07 am on Jan 20, 2017 (gmt 0)

Moderator from US 

WebmasterWorld Administrator keyplyr is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Sept 26, 2001
posts:8035
votes: 286


i normally define the charset before any markup that contains displayable characters.
Most do, however it ends up at the bottom in the response header:
Date: Fri, 20 Jan 2017 02:04:56 GMT
Server: Apache
Accept-Ranges: bytes
Cache-Control: max-age=2592000, no-transform, public
p3p: CP="ALL DSP COR NID TAIa OUR NOR STA"
X-Robots-Tag: notranslate, noarchive
X-Frame-Options: deny
Content-Length: 26607
Keep-Alive: timeout=2, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=utf-8
Content-Language: en
200 OK

I don't use a meta tag to set charset or language, I do it at the server level, but it still ends up at the bottom of the response header.
4:05 am on Jan 20, 2017 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:13610
votes: 416


but it still ends up at the bottom of the response header.
Does it matter? (Cue for phranque to locate yet another arcane information source.) A header, by definition, arrives before any of a page's visible (or even invisible) content.

I don't use a meta tag to set charset or language, I do it at the server level
Does this work reliably if not all pages are the same language? The one drawback to setting a charset globally, as with an AddCharset declaration, is that it overrides anything set within the document itself. (This seems backward to me.) Now, it's not likely you'd use different charsets within the same site--especially if your default is UTF-8--but using different languages can be exceedingly common.

:: detour to look up <html(?! lang = "en") on one site ::

Oh, heck yeah. Lots of 'em. Also a whole slew of pages with no language set at all, which I should fix forthwith to avoid Unintended Consequences.
4:44 am on Jan 20, 2017 (gmt 0)

Moderator from US 

WebmasterWorld Administrator keyplyr is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Sept 26, 2001
posts:8035
votes: 286


Does it matter?
It would appear not.
I don't use a meta tag to set charset or language, I do it at the server level
Does this work reliably if not all pages are the same language?
I think you answered this pretty well.

I have never used more than one language on a web site, but I can see it done in some scenarios.
7:25 am on Jan 20, 2017 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10813
votes: 53


Obviously it only matters if your page title contains non-ASCII characters--and even then, only if you don't also have a Charset line in your config/htaccess.

I don't use a meta tag to set charset or language, I do it at the server level, but it still ends up at the bottom of the response header.

A header, by definition, arrives before any of a page's visible (or even invisible) content.

since the document may be rendered in some cases without the server headers being provided it's better to also declare the character set in the document.

Does this work reliably if not all pages are the same language?

you can specify the lang attribute for several HTML elements.
a bilingual or multilingual site certainly must have the capability of specifying several languages in a single document with one overriding language where none is specified.

much of the charset issue discussed above is covered in the previously mentioned reference.
it's worth it to read the whole section:
https://www.w3.org/TR/html5/document-metadata.html#character-encoding-declaration
8:09 am on Jan 20, 2017 (gmt 0)

Moderator from US 

WebmasterWorld Administrator keyplyr is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Sept 26, 2001
posts:8035
votes: 286


since the document may be rendered in some cases without the server headers being provided it's better to also declare the character set in the document
Yes, for many sites that allow proxies to serve their pages or if the site allows users to download the pages to their local machine, having the charset/language at document level is a must.

I allow neither so I don't wish to aid in that endeavor :)
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members