• Instead of crafting a plugin of my own, I decided to use this one in hopes to remove the whitespace between my html tags.

    Why would I want to do this?

    Well, besides compressing my HTML even further, I wanted to get rid of the space between inline-block elements on my page which end up adding a visual gap between elements.

    Now, I know that there are css workarounds to get rid of this and none of them are truly bulletproof, including negative margins, etc. But, really the root of the problem was the space character, and getting rid of it fixes the problem absolutely. And, in my circumstance, I only wanted to clean up the HTML; I didn’t need to clean up my inline scripts, styles or comments.

    I noticed that the readme states that it attempts to preserve *rendered* whitespace, but for me this wasn’t necessary.

    After a couple minutes of looking at the innards of the plugin, I simply added one small line of code to the html-minify.php file after the entire foreach ($matches as $token) { ... } statement, just before the return $html; line.

    This is what I added

    $html = preg_replace('/>\s+</', '><', $html);

    RegExp Explained
    What it does is it looks for the end of a tag at the “>” character, including as many as possibly whitespace characters, up to the beginning of the next tag at the “<” character. Then it just replaces any instances of that search with a collapsed no-space version: “><“.

    This works out perfectly for me, I hope this helps anyone else as well.

    https://wordpress.org/plugins/wp-html-compression/

  • The topic ‘Spaces between tags’ is closed to new replies.