任何理由不剥夺HTML中的空格 [英] Any reason not to strip whitespace in HTML

查看:132
本文介绍了任何理由不剥夺HTML中的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题很自我解释。为什么我不应该剥离它?在我看来,大多数空白纯粹用于在文本编辑器中格式化,并且对最终页面没有影响。 更重要的是,当这些随机的空格 do 节点对最终页面有影响时,它通常是我不想要的影响,比如内联块之间的一个神秘的一个字符(在空白折叠后)间隔。



我可以很容易地去除所有这些空白文本节点。是否有什么理由我不应该这样做?



编辑:



主要用于奇怪的行为,而不是表现。一个例子是我想要使用inline-block而不是float来并排放置图像,同时防止包装到下一行并允许它们溢出父级。

空白引起这些神秘的空白,可以通过基本缩小HTML源代码来手动删除内嵌块之间的空白(并彻底搞乱了源代码在过程中进行格式化)。

解决方案

没有理由不这样做,真的。可以通过 htmlcompressor 之类的东西轻松完成。



然而,假设您通过gzip提供所有html,css和js文件,那么通过删除空白将实现的真实带宽节省量将非常小。问题就变成了,是否值得麻烦?

更新:

也许这会影响你的决定。我在网站的一个页面上执行了一个简单缩小,以查看它会产生什么样的差异。结果如下:

缩小前


  • 22232个字节(未压缩)

  • 5276字节(gzip)

    $ b

    缩小后


    • 19207字节(未压缩)
    • 5146字节(gzip) - 保存130字节
    • $ b


    缩小后的未压缩文件约小3 KB。但这并不重要。 gzip压缩文件是通过电线发送的。而且你可以清楚地看到gzip甚至在未缩小的HTML中也做得很好。



    我认为缩小js库的好处,或者是不会不断变化的东西。但我认为这样做对你的HTML来说只需130字节是不值得的。


    The question is pretty self explanatory. Why shouldn't I strip it? It seems to me that most of the whitespace is used purely for formatting in the text editor and has no impact on the final page.

    What's more, when these random nodes of whitespace do have an impact on the final page, it is usually an impact I do not want, such as a mysterious one character (after whitespace collapse) gap between inline-blocks.

    I can strip all these whitespace text nodes pretty easily. Is there any reason I shouldn't?

    edit:

    It's mainly for the strange behaviour where whitespace, rather than for performance. One example is me wanting to put images side by side using inline-block instead of float, while preventing wrapping to next line and allowing them to spill out of the parent.

    The whitespace causes these mysterious gaps, which can be removed by basically minifying the HTML source code to remove the whitespace between inline-blocks manually (and completely messing up your source code formatting in the process).

    解决方案

    There's no reason not to, really. It can be done very easily with something like htmlcompressor.

    However, assuming you're delivering all your html, css, and js files via gzip, then the amount of real-world bandwidth savings you'll see from stripping whitespace will be very small. The question then becomes, is it worth the trouble?

    UPDATE:

    Perhaps this will affect your decision. I performed a simple minification on a page of my website just to see what kind of difference it would make. Here are the results:

    BEFORE minification

    • 22232 bytes (uncompressed)
    • 5276 bytes (gzip)

    AFTER minification

    • 19207 bytes (uncompressed)
    • 5146 bytes (gzip) - 130 bytes saved

    The uncompressed file is about 3 KB smaller after minification. But that's not really what matters. The gzip compressed file is what is sent over the wire. And you can clearly see that gzip does a pretty good job even with the non-minified HTML.

    I see the benefit of minifying js libraries, or things that aren't changing constantly. But I don't think it's worth the trouble doing this to your HTML for a measly 130 bytes.

    这篇关于任何理由不剥夺HTML中的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆