Skip to content

Instantly share code, notes, and snippets.

@matthewcopeland
Created October 10, 2012 19:56
Show Gist options
  • Save matthewcopeland/3868007 to your computer and use it in GitHub Desktop.
Save matthewcopeland/3868007 to your computer and use it in GitHub Desktop.
Eliminate html whitespace with this little trick. Plus the simplified version in haml. Putting the closing-tag and next opening-tag on the same line kills the whitespace.
<div>
<p>These divs have white space between them.</p>
</div>
<div>
<p>Because html naturally creates whitespace on the line-breaks.</p>
</div>
<!-- with comments -->
<div>
<p>These divs have white space between them.</p>
</div><!-- html inserts whitespace between me -->
<div><!-- and me -->
<p>Because html naturally creates whitespace on the line-breaks.</p>
</div>
<div>
<p>These divs DO NOT have white space between them.</p>
</div><div>
<p>Because no line-breaks exist between them.</p>
</div>
<!-- Again. This time with comments -->
<div>
<p>These divs DO NOT have white space between them.</p>
</div><div><!-- html can't come between us. aww. -->
<p>Because no line-breaks exist between them.</p>
</div>
%div
%p This is normal haml and will out the same html-markup as the above file: 01_has_whitespace.html
%div
%p Because html naturally creates whitespace on the line-breaks.
/ By using the alligator, '>'
/ this haml will output the same html-markup as shown in 02_doesnt_have_whitespace.html
/ For more details, see
/ http://haml.info/docs/yardoc/file.REFERENCE.html#whitespace_removal__and_
%div>
%p These divs do not have any whitespace between them.
%div>
%p These divs do not have any whitespace between them.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment