Removes spaces between html tags?

会有一股神秘感。 提交于 2019-12-11 11:52:20

问题


To take away some page loading time, where can I find something to remove spaces between html tags? Without me having to go through each one and remove them myself

Like so:

<body>
  <p>Lot's of space</p>
</body>

<body><p>No space</p></body>

I found this site. But it leaves one space between tags. But I don't want any.


回答1:


Be careful that you have some idea of what is happening or you will corrupt the integrity of your documents. A fully minified code sample removes all comments and all white space characters not necessary for syntactical purposes.

In other words this example of HTML:

<p>Some content
    <strong>is strong</strong>
    and
    <em>emphasized</em>
    in this paragraph.</p>

When fully minified becomes:

<p>Somecontent<strong>isstrong</strong>and<em>emphasized</em>inthisparagraph.</p>

In that case the corruption to the content is obvious as all the words are colliding into each other. What is not obvious is the space buffering content and tags and the spaces between tags not adjacent to other content. All white space characters outside of tags in a HTML document are text nodes in the DOM and removing DOM nodes without careful consideration is possibly harmful.

Furthermore, you also have to ensure that your HTML minifier is not corrupting any inline JavaScript or CSS code. Investigate these conditions carefully when looking at the different options available.

Here is one that I wrote which may be helpful to you as it minifies markup tags in a way that is fully recursive to a beautified state using an automated pretty-print application.

http://prettydiff.com/?m=minify&html




回答2:


What you need it´s minify your html. Here you can read a discussion about this

Also, you can minify your CSS and your JS.




回答3:


Any other HTML minifier without this rule will work.

Google listed me this one:
http://www.willpeavy.com/minifier/




回答4:


I'm developing web application with Smarty template engine. It has function {strip}, which replaces all new lines, tabs, spaces... in the template. So you can write your code with many new lines and spaces. But output will be in single line.



来源:https://stackoverflow.com/questions/8458227/removes-spaces-between-html-tags

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!