Many people learned HTML, CSS, and JavaScript by viewing the source of pages and then copying and pasting them, without considering whether or not the original site was well implemented. This means that they have perpetuated coding practices that might have been necessary in the past, but are irrelevant now. This article tries to list older coding practices that over time have become unnecessary or bad practices.
Doctype
There are about ten (X)HTML doctypes. Differences are subtle (and sometimes nonexistent). It is recommended that you use the HTML5 doctype:
<!DOCTYPE html>
which triggers standards mode in every browser (even Internet Explorer 6).
<meta>
element and charset
attribute
It is not uncommon to find source code with the following line:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
However, all Web browsers (even Internet Explorer 6) will act the same if you reduce it to:
<meta charset="UTF-8" />
This knowledge has been acquired by some amazing reverse engineering and pragmatism. Use it.
Nonexistent <meta>
elements
Numerous deprecated values or non-standard values are still copied from one site to the other, perpetuating their use; although use of these elements is widespread, they are actually do not follow the specifications and are invalid. Especially, don't use:
<meta name="MSSmartTagsPreventParsing" content="true">
which was useful only in one beta version of Internet Explorer 6. That version is not used anymore and the feature, smart tags, has been removed and won't come back.<meta name="robots" content="all">
. Althoughname="robots"
is perfectly legitimate, you should only use corresponding defined values for content;all
is not defined forname="robots"
. The default iscontent="index, follow"
which is basically what the nonexistantall
is supposed to do. Just remove the whole<meta>
element.<meta name="copyright" content="…">
. This meta attribute doesn't exist. Remove it and create a copyright page or div, or link to it using the<link>
HTML element with therel="copyright"
value.<meta name="rating" content="…">
. This meta attribute doesn't exist. Just remove the whole<meta>
element.
HTML comments in scripts
There was once a time in which some browsers understood the <script>
tag and others didn't. This sometimes led to browsers rendering as text what should be interpreted as script. A natural idea was to put scripts as HTML comments. This way, browsers executing scripts would execute them and those who did not understand scripts would just ignore them.
From this era, we inherit things like:
<script> //<!-- var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "https://www."); document.write(unescape("%3Cscript src='" + gaJsHost + "bla.com/ga.js' type='text/javascript'%3E%3C/script%3E")); //--> </script>
or:
<script type="text/javascript"> <!--//--><![CDATA[//><!-- Blabla.extend(MyFramework.settings, { "basePath": "/" }); //--><!]]> </script>
The only remaining use case for this pattern to assist in strict XML validation, in which case the comments avoid false positives for unquoted markup. Otherwise, it is completely useless nowadays. Even browsers that do not execute scripts just ignore <script>
tags. For simplicity, just write your scripts between the start and end <script>
tags. Better, insert your scripts as separate files with the src
attribute; while you're at it, consider trying the HTML5 async
and defer
attributes.
Elements that should not be used anymore
font, basefont
These tags should not be used any more. CSS is preferred to control typography appearance on the elements, targeted by element, or id
or class
attributes.
b, i, u
These tend to be a bit more controversial, but as much as possible, try to use respectively <strong>
, <em>
or <span>
and CSS (text-decoration:underline
) when relevant.
Exercise discretion when choosing which of these elements to use. Some development-oriented pages advise simply replacing <b>
with <strong>
and <i>
with <em>
. It is a bad idea to follow this advice. <strong>
is for statements of strong importance, while <em>
is for otherwise emphasized text. For example, it's a bad idea to use <em>
simply to achieve italicization; italic, non-emphasized text can be achieved by using font-style:italic
in your pages' CSS. Similarly, titles of books and works of art are traditionally styled with italic text, but using the <cite>
element for these items provides more semantic mark-up than <em>
or <i>
.
acronym
The acronym tag should not be used any more. <abbr>
is preferred to represent an abbreviation.
tt, xmp
Do not use these elements. To represent a fragment of computer code, use <code>
. To represent preformatted text, use <pre>
. If you only want to use a monospaced font, you can use font-family: monospace
in CSS.
applet
This element shouldn't be used any more. <object>
is preferred because it is more generic.