`doctype` vs `charset` in HTML 5 - html

I am curious about the relationship between DOCTYPE and charset in an HTML 5 file.
Using the default HTML template in VSCode produces the following markup:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
<script src="main.js"></script>
</head>
<body>
</body>
</html>
My question is, do we need both DOCTYPE and charset?
If the doctype is set to html (which refers to HTML 5), and we know that utf-8 is the default charset for HTML 5, do we have to specify the charset using the meta tag or does the browser know?

My question is, do we need both DOCTYPE and charset?
Yes
If the doctype is set to html (which refers to HTML 5), and we know that utf-8 is the default charset for that doctype
It isn't. Browsers perform complex sniffing to determine the character encoding. Specifying it explicitly is more reliable.

It is a good rule to prepare your website for most browsers and platforms.
The use of both will garantee that most technologies can read all they need to represente your site.
So yes... rule of thumb.... always add default preferences in your code, just in case.
Best regards.

The <!DOCTYPE html> declaration is used to inform a website visitor's browser that the document being rendered is an HTML document.
<!DOCTYPE html> is doctype of html 5.
#charset specifies the character encoding used in the style sheet, and <meta charset="utf-8" /> specifies the encoding for the HTML document

Related

Explanation of html:5, html:xml, html5-boilerplate

Can anyone explain the difference between the different html options show in the vscode screenshot below? When to use each one. Thanks!
For me, html and html:xml only generate the opening and closing html tags.
html generates <html></html>
html:xml generates <html xmlns="http://www.w3.org/1999/xhtml"></html>
But html:5 generates a basic boilerplate for any html5 compatible html file. It contains some basic stuff like, doctype, head, body, title tags etc.
html: create only <html></html> tag
html:5 : create a basic html 5 template as below
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
</body>
</html>
html:xml : Usually a <!DOCTYPE> declaration is used to distinguish between versions of HTMLish languages (in this case, HTML or XHTML). More detail about it

Quotation marks inside <pre> element showing as “ or similar character

I'm just trying to post a simple html file consisting mainly of some prose I wrote inside of <pre> elements.
Interestingly, when I view the file on my computer with my browser, the quotation marks display fine. But when I upload it to my website, quotation marks are rendered as something like “ or â€. I have looked around the web for solutions but they were few and in between.
I tried to use the meta tag and included
<meta http-equiv="Content-Type" content="text/html; charset="utf-8" />
to my header but to no avail. Any ideas on how to solve this? It just wouldn't make sense to go back to the content inside the elements and code it into html as the prose is a draft and will go through many changes in the future.
The <!doctype html> tag indicates the file is HTML5 - so the browser will render it as such. lang="en" should be set to the language you are working with. Be sure to use the <meta charset="utf-8"> tag to set the character set in the <head>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Template</title>
</head>
<body>
<pre>This is my stuff</pre>
</body>
</html>
Check your code with the browser's View Source and use the Validator at https://validator.w3.org/ to check the page.
Here what I tried.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<pre>Einstein said,"Once you stop learning, you start dying"</pre>
</body>
</html>
I also tried only this
<body>
<pre>Einstein said,"Once you stop learning, you start dying"</pre>
</body>
Still working

HTML5: Meta charset before title?

I often see this:
<head>
<meta charset="utf-8">
<title>Title</title>
...
</head>
Is it important to put the chartset definition BEFORE the the title tag?
Thanks!
yes it is, see https://code.google.com/p/doctype-mirror/wiki/MetaCharsetAttribute
In order for all browsers to recognize a <meta charset> declaration,
it must be
Within the <head> element,
Before any elements that contain text, such as the <title> element, AND
Within the first 512 bytes of your document, including DOCTYPE and whitespace

how can i display a "é" using <h2>

I want to display a special caracter but when i used <h2></h2> this caracter does not appear on the web-site page.
<h2>Aéroport Tunis Carthage </h2>
on the web-site the text is: Arport Tunis Carthage.
<h2>Aéroport Tunis Carthage</h2> will do it.
Handy info on encoding can be found here and info on html character entities here.
There are two ways for displaying special characters in HTML:
1) Using HTML entities. For example:
Aéroport
2) Encoding the source HTML file appropriately (e.g. using UTF-8) and then indicating such encoding in the HTML head:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<h2>Aéroport Tunis Carthage </h2>
</body>
</html>
Of course, you have to use a text editor capable of writing files in the desired encoding. Otherwise you should indicate in the <head> the encoding used by the editor.
Try adding this to your meta tags:
HTML4:
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
HTML5:
<meta charset="UTF-8">
Source:
http://www.w3schools.com/tags/att_meta_http_equiv.asp
EXAMPLE:
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Meta Tag -->
<meta charset="UTF-8"> <!-- HTML5 -->
<title> Your title here </title>
</head>

Where should I specify my charset if not on a meta element?

I'm getting an HTML validation error for the following line, I'm not sure where I should specify my charset if I don't do it within the meta tag.
Line 5, Column 70: Attribute charset not allowed on element meta at this point.
<meta http-equiv="Content-Type" content="text/html" charset="utf-8"/>
This is because you put it in a Content-type meta tag, this is not allowed.
simply make a seperate meta tag, e.g.
<meta charset="utf-8" />
this should validate for you.
Yes. You should use as this is recommended in html5 and it is a new recommendation.
Below is a simple html5 layout:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Title of the document</title>
</head>
<body>
Content of the document......
</body>
</html>