Subscribe to RSS

How you can better organize your HTML with comments

There are a lot of different ways to organize your HTML to ensure that others, and yourself, are able to easily navigate their ways around your code. One of the best methods for helping to order your code for others is using comment tags to show where certain tags end. The best tag to use this for are divs, at the end of each div assigning a <!-- divName --!> to show that it is ending here can help others to understand what is going on and where it is ending within code. This becomes even more helpful when you are dealing with dynamic sites (as most of us strictly deal with), especially those in content management systems.

It can be difficult when approaching someone else’s code, especially if that person isn’t available for questioning. I find it frustrating when coming upon open source systems and finding a mess of divs and programming that are difficult to sort through. It can be a daunting task when you need to remove and customize various aspects of others programming when you can’t tell where one div ends and another begins.

It also helps when you are visually searching through the code and simply looking for one section of the programming or markup to see where it ends easily.

We’re all perfect CSS designers, But…

We all make mistakes. It’s true, even I make a mistake every century or so. When looking for stray markup that may be breaking your site, knowing where a div ends (or if it ends) can be impossibly helpful. It can save you a ton of time that could be spent on finishing a project.

What really helps me narrow down broken site’s issues is using the HTML Validator plugin for Firefox with this. You can easily check to see if there are any errors in the page that may be breaking the site. The HTML Validator will typically tell you where a tag starts, but there is no way for it to know where the tag is supposed to end. Having comments at the end of your divs can help you to determine what divs are closed and which are not.

This methodology for divs can be expanded, just keep in mind it renders the same as any other portion of your code. The more you use it the bigger your file will get. I am a strong believer that sacrificing a little file size for readability (and findability[sic]) is well worth it.

I typically won’t use a comment ending tag at the end of every div, some divs just don’t need it because they are extremely short markup collections. Mostly I stick with divs that are part of the structure of the web site, where divs are most commonly used (or at least should be most commonly used).

Not everyone is a programmer, but we can adapt to their methodologies

Any programmer worth anything will tell you that code that isn’t commented is messy and unprofessional. They will complain about it endlessly, trust me. So taking the way that programmers will typically comment their code, we can use that towards our markup. Not as excessively as some would— we’re not writing a novel, but we should still comment a little liberally.

You can use this practice even when it comes to commenting your CSS. Only in reverse. It is also helpful to others to add comment headings in your CSS to identify various generalized areas of the CSS. This means having more organization of where you put CSS selectors in your files.

What other methods do you find help to organize your HTML and CSS files (with or without commenting)?

Popularity: 10%

Whats Next?

  • Save to Delicious! Save the page

9 Responses to “How you can better organize your HTML with comments”

  1. suraj naik Says:

    check out the way how I manage my css.. http://www.cssaglobal.com/demos/html/cssdemo/layout.css

  2. paul Says:

    Great article i never thoguht about doing this, it would probably help my cleanliness a lot. thanks!

  3. Mike Says:

    “Any programmer worth anything will tell you that code that isn’t commented is messy and unprofessional. ”

    I disagree with this 100%. When programming, code should be readable and therefore require no comments. Naming a method updateArticle() is better than naming it u() or something pointless.

    The same goes with web programming. Using indenting and also good id/class values helps. I’ve seen a design where there was . That doesn’t tell me anything! Something like makes more sense and helps me navigate code.

    The way I and others work is, if it needs a comment, refactor.

  4. Funzig Web Design Blackburn Says:

    Good points Dustin. I know exactly what you mean about the open source issue. One of the reasons I like using MODx (opensource CMS) is that it gives you ultimate control over things like code commenting in it’s templating system. I also (mostly) use div commenting on top level structural elements for the reasons you mention.
    As for the programming issue, every software development and programming course I’ve ever been on has stressed the importance of commenting. Yes, code should be readable but why not make life easier for yourself and others by commenting?

  5. Wakish Says:

    I definetly agree with what Mike said! Self-commented variable is better than actually commenting it.

    But, viewing things as Dustin is writing, CSS and HTML are not really programming languages and hence needs a different approach. In this context comments are definetly useful! ;) (Though ids and classes should definetly be self-commented :) )

    Yours,
    - Wakish -

  6. Simon Tsui Says:

    When I’m working with PHP, instead of HTML comments with <! —  — >, I stick with the PHP comments /* */. The upside to is that these comments “disappear” (PHP comments aren’t outputted) and aren’t seen by anyone else.

  7. موقع Says:

    entirely safe and fun Very helpful!

  8. Louis Says:

    I find it’s extremely rare that developers comment the end of their DIV tags. I thought I was the only one that did this, until I found this article! I actually blogged about this same topic on my own site. Check out the article Do you comment your DIVs?. Thanks.

  9. professional Says:

    Hello. I think you are eactly thinking like Sukrat. I really loved the post.

Leave a Reply


Recent comments

CSS fix for the double margin float bug in IE6

On July 24, 2008, katalyst wrote:

omg finally bigtime thank you! i’ve been looking for a solution and yours works for me.

Understanding color in design

On July 23, 2008, Christy wrote:

i am what is referred to as a “traditional artist”- i am a fine art major & have taken a ton of color theory courses. I get confused when reading articles about color...

The best free web development add-ons for IE 6 and 7

On July 23, 2008, Valentinka wrote:

Сайт, да нет это целый портал - с фишкой. Буду теперь постоянно посещать…. и всем...

CSS fix for the double margin float bug in IE6

On July 23, 2008, agencja modelek wrote:

finally found what I sought! thx for this fix! regards, AM

Test IE5 or IE6 on your PC with IE7 installed

On July 21, 2008, Ken Hamric wrote:

CrossBrowserTesting.com allows you to check your site ‘live’ via connection via VNC to several configurations of OS and browsers. The browsers available...