Remove White Space in HTML Output

by Michael Khalili on September 28, 2009

If you’ve looked at the HTML source of an ASP.NET page you know there’s a ton of extra white space that isn’t necessary. If you’re concerned about page load speed and bandwidth you’ll want to remove all that extra space. Here’s some code you can use to reduce page size by an average of over 10%. I put this in my MasterPage file.

Protected Overloads Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
    Dim swHtmlText As New HtmlTextWriter(New System.IO.StringWriter)

    MyBase.Render(swHtmlText)

    Dim strHtml As String = swHtmlText.InnerWriter.ToString

    strHtml = Regex.Replace(strHtml, "^\s+<", " <", RegexOptions.Multiline)
    strHtml = Regex.Replace(strHtml, ">\s+<", "> <", RegexOptions.Multiline)

    writer.Write(strHtml.Trim)

End Sub

The replacements I make are very conservative. You can get even more extreme by replacing all spaces after > and before < with the regex SingleLine option.

strHtml = Regex.Replace(strHtml, "\s+<", " <", RegexOptions.Singleline)
strHtml = Regex.Replace(strHtml, ">\s+", "> ", RegexOptions.Singleline)

That code could will break text in a Textfield so you have to make sure to test this more extreme version. You can also add a property in your code for the level of white space you want removed.

You should share this page:
  • email
  • HackerNews
  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Mixx
  • Sphinn
  • Yahoo! Buzz
  • Print
blog comments powered by Disqus

Previous post: Textbox and Dropdown combined

Next post: Use Bit.ly to Expose Click Traffic of Tweets