: Global UTF-encoding, the right way I'm curious, as to what is the right way to have UTF-8 encoding on all web files All my files (incl. CSS and JS) are made and saved in UTF-8 encoding In
I'm curious, as to what is the right way to have UTF-8 encoding on all web files
All my files (incl. CSS and JS) are made and saved in UTF-8 encoding
In PHP, I set the char-set on top of the main page (this page includes all others) with:
header('Content-type: text/html; charset=utf-8');
In the same page I have this html meta tag:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Then I stubled upon an external css file that has this on first line:
@charset "UTF-8";
And now I wonder, should I set the charset INSIDE all my CSS/JS files too, like that?
And/or should I serve each file with charset=utf-8 in the meta tag?
UPDATE: Character encoding of the linked resource (charset="utf-8") is obsolete
More posts by @Cugini213
2 Comments
Sorted by latest first Latest Oldest Best
The best place for the character set declaration when served from your webserver is in the "Content-Type" header. You are doing it correctly when serving it from PHP. You could also set the header for CSS and JS. Assuming that your CSS and JS are not served through PHP, but as static files, you could add the following to your .htaccess file:
AddCharset utf-8 .js
AddCharset utf-8 .css
When linking to to your JavaScript files from your HTML you can also include the character set there:
<script type="text/javascript" src="/js/file.js" charset="utf-8"></script>
The in body charset declarations are most useful for when your files get downloaded and opened locally. In those cases meta information in the headers is lost.
The purpose of the meta element setting utf-8 is so user's browsers can properly read the page should they save it to their desktop where a server won't be serving the page and include the utf-8 header. In PHP, you set it in the header which actually makes including it in the page redundant (and repetitive, too :) ) but a good idea for the reason I gave.
I've forgotten what the @charset would be for but I've never used it which shows how important including that is.
btw, meta tags do not have a closing slash.
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.