Coding Clean and Semantic Templates 235
If you are the guy who uses <div> tag for everything, this post is for you. It focuses on how you can write clean HTML code by using semantic markups and minimize the use of <div> tag. Have you ever edited someone's templates, don't those messy tags drive you crazy? Not only writing clean templates can benefit yourself, but your team as well. It will save you time when you have to debug and edit (particularly the large projects).
1. Remove The Unnecessary <div> Tags
I've seen a lot of people wrap a <div> tag around the <form> or <ul> menu list. Why create an extra <div> tag that you don't need? You can achieve the same result by applying the CSS rules to the selector.
Example 1:
The example below shows how you can drop the <div> tag and declare the styling to the form selector.

Example 2:
Sometimes we wrap the content with an extra <div> tag strictly for spacing purposes. The example on the left uses a <div class="sidebox"> to create margin space in between the boxes. But if each box has a heading (ie. <h4>), we can simply apply the margin space to the h4 selector and drop the extra <div class="sidebox"> tag.

2. Use Semantic Markups
You should always use semantic markups to code HTML documents (ie. <h1> for headings, <p> for paragraph text, and <ul> for list items). So, even when the CSS is not presented nor supported, your document still makes sense.
Example:
The image below compares the rendering differences between <div> markups and semantic markups with no css supported.

3. Minimize The Use of <div> Tags
Have you seen the messy templates where <div> tags are everywhere and they drive you crazy? Have you ever missed a closing </div> tag or have an extra opening <div> tag messing up the entire layout? I'm sure most developers have experienced it before. So, you should always minimize the use of <div> tag if possible. It will make debugging and editing easier.
Example 1:
Instead of using <div> tag for breadcrumb navigation, it makes more sense to use <p> tag.

Example 2:
The following example shows how I can use CSS to cut down two <div> tags by replacing with one <span> tag. They both produce the same layout.

4. Format (Indent) Your Code
You should always format your source code (ie. indent your nested elements) so it is easier to read and debug. If you have Adobe Dreamweaver, you can easily format the code by using the Commands > Apply Source Formatting (from the application menu).

5. Comment The Closing </div> Tags
When coding for platform templates (ie. WordPress themes), the template is most likely splitted into several files: index.php, header.php, sidebar.php, and footer.php. Hence, you should always make a comment for the closing </div> tags so you won't get lost. For example, when I see </div><!-- /wrapper -->, I know it is the closing tag for <div id="wrapper">.
Example:
I usually insert a HTML comment tag right after the closing </div> tag. I use the forward slash to indicate it is the closing tag.

Conclusion
- Minimize the use of
<div>tags. - You should only use the
<div>tag for the main layout sections such as: header, content, sidebar, and footer. - The content should be in semantic HTML tags, not
<div>tags. - Format the source code and label the closing
</div>tags.
1. Decide upon the contents to be put on the page.
2. Put headings in h1,h2,h3.. according to the hierarchy of the website.
3. Put paragraphs in ‘p’ links in ‘a’ and so on following semantic markup.
4. Do the basic styling and typography common to the page with css(eg :base font size,color etc).
5. Group different blocks of data by using ‘div’ tags and naming them semantically by adding class names or unique ids.
6. Position the blocks according to the layout.
7. Add css for elements inside each block, ie. custom css for the block
This is how i do and i can avoid many unnecessary divs and gets clean markup.
Even i think the use of “p” tag replacing “div” is not a good idea for breadcrumb. use ‘p’ for paragraph, and ‘div’ to group elements into a block. Here i think we are grouping some elements into a block so i would suggest using a ‘div’. Another drawback – we might need to override the default margins and padding when using ‘p’.
Sorry if this has been mentioned previously but using a few CSS 2.1 selectors as opposed to assigning classes to different elements of the same type can also save a bit of excess code. I’ve found the adjacent and child selectors most commonly useful.
IE6 will no doubt have issues with it but depends how important IE6 is to your visitors.
Very helpful article. I also get the case of “DIV”itis as well. This will help cure that disease. :)
very nice article thank you
Thanks for the nice article!
Some good tips that are often overlooked!
excellent post. thanks
Very well written, you have the ability to come with new articles that are really useful. I’ll have it open next time I code a template. Keep on the good work.
Really useful, I wish I had come across this sooner. I have just finished redesigning my own blog today and it has been challenging to say the very least. If you get time please give it a visit and send me your honest feedback, I am always looking to improve and learn from some of the best. Thanks
I hope most guys that work with html markup know about these points… but im not agreeing about the replacement of the with tags… your just grouping links, so it doesnt make sense. Although its an article that beginners should know.
i ment div with p tags
Very good. Congratulations. Peace.
Nice post!! I generally override these things….thanks
Great article, and very well written mate!
Nice post – waiting new one’s
Perfect article. Thanks! I will take note of the redesign :).
Great, I really need this.
Excellent post! Will defiantly be implementing the tips in my next project!
Commenting at the is a great idea, I never thought about it, and it could save some serious headaches.
Thanks , man :)
I like you article :)