Removing Whitespace in HTML

I came across what I thought would be a pretty simple challenge, whereas I needed to quickly stripe white space from an HTML document, so I could stuff it into a database to be referenced by another job.  I assumed, somebody would have created a webp age that magically striped white space for me, and I’d be off and rolling with my proof of concept – nope.  10 minutes of Google turned up no such page, so after some experimenting this is what I came up with..

This is a simple and yet very powerful “trick”.  Almost any code editor should do the trick, I used Visual Studio.
Here’s how it works:

  1. Open a document,
  2. Click inside Code View,
  3. Create an empty line,
  4. Left-click in the margin on the left hand-side of that empty line (it should turn black on Windows, blue on Mac),
  5. Select Menu > Edit > Find and Replace (keyboard shortcut: Ctrl + F),
  6. Select “Replace All” and you’re done.

Be aware that “trimming” can do serious damage and break things apart.  In Dreamweaver, I understand even line breaks are removed and you end up with one tidy run-on string.  In VS however, I had to manually remove line breaks, which wasn’t too bad, considering the find/replace did most the work for me.

Hope it helps you if you find yourself quickly needing to stripe whitespace.


One response to “Removing Whitespace in HTML

  1. Notepad++ does this directly.

    open HTML file in Notepad++
    click Language / HTML to see it formatted in HTML
    click TextFX / TextFX Edit / Delete Blank Lines
    click File / Save As …
    You’re done!

    Notepad++ is free from

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s