如何防止 HTML 源格式影响输出?
HTML 代码中额外的换行符可能会在最终输出中添加不需要的空格。我一直认为无论我如何布局 HTML 代码,都不会影响渲染结果的外观。但这里有一个示例:
<h2>
<a href="#">Hello.</a>World
</h2>
将显示:“Hello.World” - 一切看起来都符合预期
<h2>
<a href="#">Hello.</a>
World
</h2>
将显示:“Hello.World” - 带有 额外的空格点之后!
有机会摆脱这种效果吗? 我希望我的代码位于单独的行上 - 并且不产生额外的空间。
It looks like extra line breaks in the HTML code may add unwanted spaces in the final output. I always thought that no matter how I layout my HTML code, it will not affect the way the rendered result looks like. But here is an example:
<h2>
<a href="#">Hello.</a>World
</h2>
Will display: "Hello.World" - all looking good as expected
<h2>
<a href="#">Hello.</a>
World
</h2>
Will display: "Hello. World" - with an extra space after the dot!
Is there a chance to get rid of this effect?
I want to have my code on separate lines - and not producing an extra space.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用注释:
或将空格放在标签内:
You could use comments:
or put the spaces inside the tag:
不,不存在,在这种情况下不存在。
在 HTML 中,所有空格都算作空格,但相邻的多个空格字符仅算作 1。因此,您的代码相当于:
删除与块元素相邻的空格,但不删除文本内的空格。由于锚标记是内联元素,因此它无法删除其旁边的空格,因为这会更改内容。因此,在删除可以的空格后,剩下的就是:
因此,您可以在任何您喜欢的地方添加额外的空格,只要它不是内容的一部分即可。这:
将相当于(删除不影响结果的空格后):
No, there isn't, not in this case.
In HTML all white space counts as spaces, but several white space characters after each other only counts as one. So, your code is equivalent to:
The spaces adjacent to block elements are removed, but not spaces inside text. As the anchor tag is an inline element, it can't remove the space next to it, as that would change the content. So, after removing the spaces it can, this is what's left:
So, you can have extra white space anywhere you like, as long as it's not part of the content. This:
Will be equivalent to (after removing spaces that doesn't affect the result):
新行总是被翻译成空格。当每个列表项元素都写入新行时,在 IE 上情况会变得更糟。悲伤但真实。
New line is always translated into a space. It even gets worse on IE when every list item element is written in new line. Sad but true.
恐怕你不能。任何一组空白都被视为一个空格。
另一种选择:
但是由于浮动元素,您将面临另一组问题需要解决。
I'm afraid you can't. Any group of white spaces is considered a space.
An alternative:
But you will have another set of problems to solve due to to the floating elements.
假设 HTML 按照您希望的方式工作。然后说我想要这些空间。你会让我如何创建它们?每次都添加一个特殊字符,例如
?
Let's say HTML worked the way you want it to work. Then say I want the spaces. How would you have me create them? Add a special character like
every time?