Top 10 best practices for front-end web developers

Posted: April 15, 2010 in Design
Tags: , , , ,

Explain which div you’re closing

Most of the time when I’m viewing a website source, I see, at the very bottom of the page, an almost endless list of closing </div> tags. In fact, many beginners think they just have to use divs instead of tables to produce quality code. Divs are cleaners than tables, but without proper code organization, it can be as (or even sometimes more) messy as table based code.

Using indentation is a good start. But a tip that can definitely make you save lot of time is to comment every div tag you’re closing, as shown in the example below:

1.<div id="header">
2. <div id="sub" class="first left">
3. ...
4. </div><!-- #sub.first.left -->
5.</div><!-- #header -->
****************************************************

Use a CSS reset

Unless you’re a beginner or if you were on vacation on a desert island for the last 6 years, you might already know how useful a CSS reset it. Because by default, browsers don’t apply the same default styling to HTML elements, a CSS reset will ensure that all element have no particular style so you can define your own without the risk of many cross-browser rendering issues.

01.html, body, div, span, applet, object, iframe,
02.h1, h2, h3, h4, h5, h6, p, blockquote, pre,
03.a, abbr, acronym, address, big, cite, code,
04.del, dfn, em, font, img, ins, kbd, q, s, samp,
05.small, strike, strong, sub, sup, tt, var,
06.b, u, i, center,
07.dl, dt, dd, ol, ul, li,
08.fieldset, form, label, legend,
09.table, caption, tbody, tfoot, thead, tr, th, td {
10. margin: 0;
11. padding: 0;
12. border: 0;
13. outline: 0;
14. font-size: 100%;
15. vertical-align: baseline;
16. background: transparent;
17.}
18.body {
19. line-height: 1;
20.}
21.ol, ul {
22. list-style: none;
23.}
24.blockquote, q {
25. quotes: none;
26.}
27.blockquote:before, blockquote:after,
28.q:before, q:after {
29. content: '';
30. content: none;
31.}
32.
33./* remember to define focus styles! */
34.:focus {
35. outline: 0;
36.}
37.
38./* remember to highlight inserts somehow! */
39.ins {
40. text-decoration: none;
41.}
42.del {
43. text-decoration: line-through;
44.}
45.
46./* tables still need 'cellspacing="0"' in the markup */
47.table {
48. border-collapse: collapse;
49. border-spacing: 0;
50.}

Source: http://meyerweb.com/eric/tools/css/reset/index.html

Don’t use @import

CSS files can be included using the @import directive. This can be useful when, for example, you want to include a stylesheet into another. Another common practice is to include CSS file in html documents using the following:

1.<style type="text/css>
2. @import url('a.css');
3. @import url('b.css');
4.</style>

While it works, the @import directive is much slower than the other way to include stylesheets into a html document:

1.<link rel='stylesheet' type='text/css' href='a.css'>
2.<link rel='stylesheet' type='text/css' href='proxy.css'>

It will not make a difference on low traffic websites, but if you have the chance to own a popular website, don’t waste your visitor’s time using @import.
Source: http://www.stevesouders.com/blog/2009/04/09/dont-use-import/

“Smush” your images

Being a developer, I always found that optimizing my images for the web wasn’t easy. I tried the good old “Save for web” Photoshop command, but most of the time, I ended up with images that were either too big or without a sufficient quality.
As a result, I had the bad habit of using unoptimized images on my websites. This isn’t a problem when you don’t have to care about your site’s bandwidth, but after my recent switch on my vps.net virtual private server, I had to be careful with image sizes.

At this time, I found a very cool tool named Smush It: You enter your unoptimized image url, and Smush It will create a perfectly optimized image for you. You can save up to 70% of the file size, while keeping the original quality. As an example, all the images from my list of online code editors have been “smushed”.

Don’t mix CSS with HTML

As a markup language, the right use of HTML is to organize documents by defining a header, a footer, lists, blockquotes, etc. Some time ago, front-end web developers often used now deprecated HTML attributes to style a particular element.
Nowadays, the style attribute allows developers to insert CSS directly into a html document. This is very useful for testing or when you’re in a hurry. But the style attribute is bad practice, that goes completely against the CSS philosophy.

The following example illustrates how dirty and hard to read a simple line of code can become, with the style attribute:

1.<a href="http://www.catswhocode.com" style="background:#069;padding:3px;font-weight:bold;color:#fff;">Cats Who Code</a>

Don’t mix Javascript with HTML

Just like mixing your html code with css is bad practice, you shouldn’t use any Javascript in your html documents. The following bad practice illustrates an onclick event:

1.<a id="cwc" href="http://www.catswhocode.com" onclick="alert('I love this site!');">Cats Who Code</a>

The same result can be achieved using unobstructed Javascript. In this example, I’m using the popular jQuery framework:

1.$(document).ready(function() {
2. $('#cwc').click(function() {
3. alert('I love this website');
4. });
5.});

This may seems a bit harder at first, especially for beginners; but it is definitely not, and it will keep your html document clean.

Use conditional comments

You know it, IE sucks, and some clients suck even more by requiring you to create webpages which are compatible with this obsolete browser. To target specific versions of IE, you can use the well known IE hacks, as shown below:

1.height: 200px; /* normal browsers */
2._height: 300px; /* IE6 */
3..height: 250px; /* IE7 */
4.*height: 350px; /* All IEs */

Those hacks are extremely useful sometimes, but they are not the best way to target a specific version of IE, and it will cause your CSS validation to fail.

Instead, you should use the conditional comment shown below to target IE6.

1.<link href="style.css" rel="stylesheet" type="text/css" />
2.
3.<!--[if lte IE 6]>
4. <link href="ie.css" rel="stylesheet" type="text/css" />
5.<![endif]-->

Place Javascript file at the bottom

A popular practice of the late 90’s/early 2000’s was to place Javascript files within the <head> and </head> tags. The problem is that your javascript files will be loaded first, and consequently your content will be loaded after.

By placing Javascript files at the bottom of your documents, you’ll ensure that JS files will be loaded only when the content has been properly displayed.

1. ...
2. <script type='text/javascript' src='jquery.js?ver=1.3.2'></script>
3. </body>
4.</html>

Use HTML semantically

HTML is not a programming language. It is a markup language, used to create structured documents by denoting structural semantics for text such as headings, paragraphs, lists, and more.
If you started to create websites in the good old 90’s or in the beginning of the century, you know how dirty the markup was at the time. But happilly, it has evolved.
Among other things, it is important to use html element semantically. As an example, a navigation menu should always be an unordered list:

1.<ul>
2. <li><a href="#">Home</a></li>
3. <li><a href="#">About</a></li>
4. <li><a href="#">Contact</a></li>
5. <li><a href="#">Blog</a></li>
6.</ul>

Test WHILE you build to avoid cross-browser issues

One of the biggest mistake I ever made when developing html, CSS, and javascript, was not to test my pages on multiple browser while I was writing them. Instead, I used to write all my code and just view in Firefox to see how it was rendered.
In theory, this should be good. But as you know, cross-browser issues are a major problem for front-end developers, especially due to IE. If you test your documents on Firefox/IE/Chrome while your writing it, cross-browser rendering problems will be much easier to fix. I have lost hours not doing it, so I hope this final tip will help you saving your precious time. To test on multiple versions of IE, I use this very handy tool. Happy coding ;)

About these ads
Comments
  1. descarca says:

    This is good. I wanted pictures to add in my school project and the images you have posted are good. Thanks for them buddy! I guess teacher will love my project due to these images and of course the information I provided.

  2. trilulilu says:

    Your blog is good! Generally when I visit blogs, I just come across shit, but this time I was really surprised when I got your blog containing wonderful information. Thanks mate and keep this effort up.

  3. snowalker says:

    really interesting article. I have the feeling I might had been reading something similar though. The only thing I didn’t do it till now is to “Smush” the images. I regularly use “save for web” PS method. Good job, bookmarked!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s