Nov 18
Coding Clean and Semantic Templates
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.
5 Simple, But Useful CSS Properties iPhone Gift Ribbon
Comments
Pages: 19 18 17 16 15 14 13 12 11 10 9 … 1 » Show All
There are 190 comments (+Add)
Pages: 19 18 17 16 15 14 13 12 11 10 9 … 1 » Show All


190 KejeN http://fajnanuta.pl
March 6th, 2010 at 7:47 am
Thanks a lot for this post - it’s very eyes opening for me.
189 sbobet http://www.sbobet-th.com
March 4th, 2010 at 12:15 am
Thanks for this tips
188 J.F. Herrera http://www.templatejuice.com
March 2nd, 2010 at 1:08 am
Great information. I especially agree with commenting your code. Not for others sake, but for your own when you back to update it.
Thanks!
187 Steph http://www.gotmedia.org/
February 27th, 2010 at 12:05 am
Good to know I’m doing it right
186 Satyendra Chauhan http://www.Duninfo.com
February 24th, 2010 at 7:23 am
This is a really good technique and will definitel..
185 capsiplex http://www.capsiplexzayiflamahapi.net/
February 23rd, 2010 at 5:34 pm
very useful. some of my Wordpress site are with div tags instead of p tags.
184 hotfixes http://hotfixes.edibra.com
February 23rd, 2010 at 5:08 pm
Thank you, this post has been very helpful 2 me
183 niliya http://www.websitereckon.com
February 23rd, 2010 at 6:04 am
hi friends
check your website real value
websitereckon.com
WebsiteReckon contains a collection of useful features such as checking Google Page Rank (PR), Compete and Alexa Traffic Rank, Backlinks Checker, Server IP and Location on Google Map, DMOZ and Yahoo Directory Listings and more. You can use it for various purposes include doing research before buying or selling website; want to find out how popular the website is; use it as SEO analytic tool to improve your site performance; or just about curiosity.
182 Matsomoto San
February 9th, 2010 at 8:15 pm
Yes! That is’ what i think. Less is more…
181 Alain Gauthier http://www.albertainsuranceexam.com
February 8th, 2010 at 6:34 pm
very useful. some of my Wordpress site are with div tags instead of p tags. I think it is the TinyMCE that does it but I am not sure yet why.
However your article help me understand that p tags are indeed better to use than div tags in my posts.