How to find the unclosed div tag

148,242

Solution 1

As stated already, running your code through the W3C Validator is great but if your page is complex, you still may not know exactly where to find the open div.

I like using tabs to indent my code. It keeps it visually organized so that these issues are easier to find, children, siblings, parents, etc... they'll appear more obvious.

EDIT: Also, I'll use a few HTML comments to mark closing tags in the complex areas. I keep these to a minimum for neatness.

<body>

    <div>
        Main Content

        <div>
            Div #1 content

            <div>
               Child of div #1

               <div>
                   Child of child of div #1
               </div><!--// close of child of child of div #1 //-->
            </div><!--// close of child of div #1 //-->
        </div><!--// close of div #1 //-->

        <div>
            Div #2 content
        </div>

        <div>
            Div #3 content
        </div>

    </div><!--// close of Main Content div //-->

</body>

Solution 2

I made an online tool called Unclosed Tag Finder which will do what you need.

Paste in your HTML, and it will give you output like "Closing tag on line 188 does not match open tag on line 62."

Update: The new location of the Unclosed Tag Finder is https://jonaquino.blogspot.com/2013/05/unclosed-tag-finder.html

Solution 3

the World Wide Web Consortium HTML Validator is great at catching HTML errors.

Solution 4

Use notepad ++ . you can find them easily

http://notepad-plus-plus.org/download/

Or you can View source from FIREfox - Unclosed divs will be shown in RED

Solution 5

I know that there have already been some good answers, but I came across this question with a Google Search and I wish someone would have pointed out this online checking tool...

http://www.tormus.com/tools/div_checker

You just throw in a URL and it will show you the entire map of the page. Very useful for a quick debug like I needed.

Share:
148,242
Admin
Author by

Admin

Updated on July 08, 2022

Comments

  • Admin
    Admin almost 2 years

    A unclosed div problem almost make me crazy. It is very difficult to track especially when the page is long and complex.

    Any suggestions?

  • rockerest
    rockerest almost 13 years
    This is a good point - +1. Clean, maintainable code is 90% of finding problems. Also, I would give another +1 for using tabs instead of spaces ;)
  • Sparky
    Sparky almost 13 years
    It's really amazing how many cross-browser issues simply vanish when you reach "green" on the validator.
  • rockerest
    rockerest almost 13 years
    @Sparky672 I feel dirty if my code isn't green :) Standards compliance all the way!
  • Admin
    Admin almost 13 years
    Do you think this format is still possible when html mixed with hundreds lines of php codes?
  • Sparky
    Sparky almost 13 years
    @Don: It's worth a try. But you don't have to do every single line... you just have to identify the main chunks. Then clean up the sub-chunks, etc. Once you start cleaning up these chunks of code, the problem should reveal itself.
  • Jorre
    Jorre over 11 years
    I noticed that the W3 Validator does not detect unclosed tags, when using html5, or it just allows using crappy code?
  • rockerest
    rockerest over 11 years
    @jorre Unfortunately (in my opinion), HTML5 is standardized as HTML, not XHTML. XHTML requires that almost all tags be closed, everything is lower case, etc. Since HTML5 is purely HTML, those "pretty code" requirements are out the window, and it's now legal to leave unclosed tags, have incomplete tag attributes (e.g., "<option selected value="1">"), and more. It disappoints me, but it is what it is.
  • Admin
    Admin about 11 years
    Website blocked by Trend Micro Internet Security Opening this website may put your security at risk The website you wanted to see might transmit malicious software to your computer, or has done that before to someone else. It may also show signs of involvement in online scams or fraud. Address: jona.ca/blog/unclosed-tag-finder Rating: Dangerous
  • Jonathan Aquino
    Jonathan Aquino about 11 years
    Wonder why it's saying that about my site :(
  • Jonathan Aquino
    Jonathan Aquino about 11 years
    I have filed a Reclassify Request with Trend Micro - hopefully they can get my site off their blacklist.
  • Jonathan Aquino
    Jonathan Aquino about 11 years
    Trend Micro has reclassified my blog (jona.ca) as Safe: global.sitesafety.trendmicro.com - thanks for pointing it out to me.
  • Prasanth
    Prasanth almost 11 years
    Classy... what about this: <!-- </div> --> :D
  • desperateCoder
    desperateCoder over 9 years
    tabs instead of spaces? No never. Tabs cause numerous problems when formatting code. I always have my editor insert spaces for tabs.
  • Sparky
    Sparky over 9 years
    @TrevordeKoekkoek, what sorts of "numerous problems" exactly? I'm curious only because I've never seen a problem caused by a tab used in place of four spaces.
  • Andrew Koper
    Andrew Koper about 9 years
    A) This is a great question and shouldn't have been closed. B) This top answer isn't too great. Yes, you should indent your code and you can put comments by the closing tag, and this looks awesome if your file is 20 lines long, but the question is more nuanced than that - these two techniques aren't enough to tell where unclosed tag is when the file is 1,000 lines long.
  • Sparky
    Sparky about 9 years
    @AndrewKoper, a lot has changed regarding the off-topic closing reasons since 2011. However by today's rules & reasons, if not "too broad", then certainly "primarily opinion based". You can vote to re-open, flag it for a moderator to re-open, discuss this at the Meta SO site, and if/when re-opened post your own better answer. Otherwise, this is not really the purpose of comments.
  • Sparky
    Sparky about 9 years
    @AndrewKoper, also, what does the length of the file have to do with anything when it should have been written properly in the first place? I have files that are over a thousand lines long and properly follow standard formatting.
  • Sparky
    Sparky about 9 years
    The OP already knows he has an open tag. How does counting them identify which element was left open?
  • Qasim
    Qasim over 8 years
    They don't have an OSX version, btw..
  • Sablefoste
    Sablefoste about 8 years
    Hello Jonathan, I tried it right away, but I noticed if there are any comments, your tool thinks they are tags. E.g., <!-- html comment here -->.
  • Sablefoste
    Sablefoste about 8 years
    Also, I notice if a tag is spread across lines/ broken with a carriage return (such as when it has many pieces, such as an id, data, style, etc. your validator doesn't calls it "broken". Otherwise, a great tool!
  • SherylHohman
    SherylHohman about 7 years
    This is the most useful tool for checking div tags I've seen yet. Its major advantage over editor "matching tags" features, is that It cuts clutter of all other code, comments, and other tags, so you can easily focus just on the structure of the divs. It's a simple and pretty layout. Thanks. Bookmarked.
  • gurung
    gurung over 5 years
    Link is producing - 502 Bad Gateway. Please upload the code on github or somewhere it could stay for a few more years at least.
  • Sebastian Simon
    Sebastian Simon over 5 years
    Why is there a suggested edit by a user named Jonathan Aquino? Is this the same person with a different account? You, Jonathan Aquino, could directly approve or reject that pending suggested edit.
  • Abhishek Singh
    Abhishek Singh over 5 years
    How do you know which div do not have closing tag out of 100??
  • spume
    spume about 2 years
    The View Page Source from Firefox is really helpful: the red highlighting will still flag the problem when it's not what you though it was.
  • Ajmal PraveeN
    Ajmal PraveeN about 2 years
    @JonathanAquino Really Thank you, Most Reliable and Helpful Tool you made ❤