简体   繁体   English

任何不在HTML中删除空格的原因

[英]Any reason not to strip whitespace in HTML

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). 空白会导致这些神秘的空白,可以通过基本缩小HTML源代码来手动删除内联块之间的空白(并在此过程中完全弄乱源代码格式)来删除这些空白。

There's no reason not to, really. 真的没有理由不这样做。 It can be done very easily with something like htmlcompressor . 它可以通过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. 但是,假设您通过gzip提供所有html,css和js文件,那么从剥离空白中看到的实际带宽节省量将非常小。 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) 22232字节(未压缩)
  • 5276 bytes (gzip) 5276字节(gzip)

AFTER minification 缩小后

  • 19207 bytes (uncompressed) 19207字节(未压缩)
  • 5146 bytes (gzip) - 130 bytes saved 5146字节(gzip) - 保存130个字节

The uncompressed file is about 3 KB smaller after minification. 缩小后,未压缩文件缩小约3 KB。 But that's not really what matters. 但这并不重要。 The gzip compressed file is what is sent over the wire. gzip压缩文件是通过线路发送的。 And you can clearly see that gzip does a pretty good job even with the non-minified HTML. 你可以清楚地看到,即使使用非缩小的HTML,gzip也能做得很好。

I see the benefit of minifying js libraries, or things that aren't changing constantly. 我看到了缩小js库的好处,或者不断变化的东西。 But I don't think it's worth the trouble doing this to your HTML for a measly 130 bytes. 但我不认为这样做对你的HTML有130麻烦的麻烦是不值得的。

Let me give one reason why you shouldn't minify html: 让我给出一个你不应该缩小html的原因:

How html eventually gets rendered is strongly tie to the CSS applied up on it, but the minifiers usually work without expecting the influence of CSS. 最终如何呈现html与应用于它的CSS紧密相关,但是minifiers通常可以在不期待CSS的影响的情况下工作。 All minifiers you can get out there at the time of writing, they remove the spaces in html based on certain assumptions of your coding and CSS styling, if you don't code it the way they expected, the minified rendering result in browser will be different from before minification. 在编写本文时你可以得到所有缩小器,它们会根据编码和CSS样式的某些假设删除html中的空格,如果你不按照他们的预期编码,那么浏览器中的缩小渲染结果将是与缩小之前不同。

For example, some of minifiers assume the space between "block elements" (such as <div/> , <p/> ) can be removed, this is usually true, because spaces between them has no effect on rendering the final result. 例如,一些缩小器假设“块元素”之间的空间(例如<div/><p/> )可以被移除,这通常是正确的,因为它们之间的空格对渲染最终结果没有影响。 But what if in the CSS you set "display: inline" or "inline-block" for elements whose default display property is block? 但是,如果在CSS中为默认显示属性为块的元素设置“display:inline”或“inline-block”,该怎么办?

Will below html snippet still rendering as it should be if you remove the spaces between <div/> s ? 如果删除<div/> s之间的空格,html片段下面是否仍然会呈现?

<div style="display: inline">will</div> <div style="display: inline">this</div> <div style="display: inline">still</div> <div style="display: inline">work?</div>

You may argue that, we can reserve at least 1 space, and remove remaining consecutive spaces and that still save a lot bytes. 您可能会争辩说,我们可以保留至少1个空格,并删除剩余的连续空格,这仍然可以节省很多字节。 Then how about <pre> tag and white-space: pre ? 那么<pre> tag和white-space: pre

Try copy the html code snippet from below url and paste into your minifier, see if it produces result as before the minification: 尝试从下面的url复制html代码片段并粘贴到你的minifier中,看看它是否在缩小之前产生结果:

https://jsfiddle.net/normanzb/58rpazL2/ https://jsfiddle.net/normanzb/58rpazL2/

The only downside of stripping out whitespace from production pages is readability, and maintainability for the person that follows you in editing that/those page(s); 从生产页面中删除空白的唯一缺点是可编辑性,以及跟随您编辑那些/那些页面的人的可维护性; but if you maintain a 'properly'/'readable' whitespaced-version for editing, and then minify that post-editing to form the production pages then it doesn't really cause significant problems. 但是如果你保持一个'正确'/'可读'的空白版本进行编辑,然后缩小后期编辑以形成生产页面,那么它并不会真正造成重大问题。

I'm not sure how effective, or useful, the technique will be, but there's nothing to stop you trying it. 我不确定这种技术会有多么有效或有用,但没有什么可以阻止你尝试它。

Short answer: no reason whatsoever 简短的回答:没有任何理由

The only real purpose white space serves is to make the code more human-readable. 唯一真正用途的白色空间用于使代码更易于阅读。 You can, over time, save a lot of bandwidth by stripping all the unnecessary white space out of your documents and it should be considered good practice for production code. 随着时间的推移,您可以通过从文档中剥离所有不必要的空白来节省大量带宽,这应该被视为生产代码的良好实践。 If your compressing your content the saving will be less, but even 1% of 1GB is 10MB... If your doing 100GB in a month on a busy web site, cutting out 1% of the data might be the difference between two pricing tiers of hosting... 如果您压缩内容节省的费用会更少,但即使1GB的1GB也是10MB ......如果您在繁忙的网站上一个月内做100GB,那么削减1%的数据可能是两个定价层之间的差异托管...

As you say, some browsers (usually IE, grrrr....) will occasionally interpret the white space when they render the page, but usually when this happens it's in a way you'd rather it hadn't... 正如你所说,一些浏览器(通常是IE,grrrr ....)在呈现页面时偶尔会解释空白区域,但通常情况下会发生这种情况,你可能会认为它不是......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM