如何在没有过多注释的情况下消除空格?
这是我页面中的代码片段:
<input value="Search" type="submit" /><!-- whitespace
--><span class="vdivider"></span><!-- whitespace
--></form><!-- whitespace
--><form action="login_action.php" method="post"><!-- whitespace
--><?php
这些空白注释是为了消除分隔线两侧的空白。这真的是唯一的方法吗?必须有一个更优雅的解决方案。
Here is a code snippet from my page:
<input value="Search" type="submit" /><!-- whitespace
--><span class="vdivider"></span><!-- whitespace
--></form><!-- whitespace
--><form action="login_action.php" method="post"><!-- whitespace
--><?php
Those whitespace comments are to get rid of the whitespace on each side of the divider. Is this really the only way of doing this? There has to be a more elegant solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
可以考虑的一个选择是——如果可以的话,使用模板引擎。例如,在 Smarty 中,有一个 {strip} 函数正是执行此操作:
输出:
One option to consider - use a templating engine if you can. For example, in Smarty, there's a {strip} function that does exactly this:
Output:
您可以使用
font-size:0
hack。基本上,您在父元素上设置font-size:0
,并在子元素上显式设置 font-size 。现场演示: http://jsfiddle.net/simevidas/mLZYW/1/
(没有破解的演示:http://jsfiddle.net/simevidas/mLZYW/)
You can use the
font-size:0
hack. Basically, you setfont-size:0
on the parent element, and set the font-size explicitly on the children.Live demo: http://jsfiddle.net/simevidas/mLZYW/1/
(Presentation without hack: http://jsfiddle.net/simevidas/mLZYW/)
空白仅在在内联元素周围或旁边时显示,因此至少对于您不需要的表单(如果您尚未将表单设置为
display:inline
... )。定位或浮动内容几乎总是会删除不需要的空白,因此,例如,如果您的
.vdivider
应该是垂直分隔符/换行符,则只需使用display:block
code> 放在其前面的input
上,并删除该元素及其周围的注释。White-space only shows when it is around or next to inline elements, so at least for the forms you don´t need it (if you haven´t set your forms to
display:inline
...).Positioning or floating things almost always removes the unwanted white-spaces, so for example if your
.vdivider
is supposed to be a vertical divider / new line, you can just usedisplay:block
on theinput
before it and remove that element and the comments around it.元素之间的空格(包括换行符和制表符)会导致浏览器在不应该有空格的地方插入空格。
我见过的解决此问题的最优雅的方法是将
>
放在下一行,而不是同一行。这样,它仍然是合法的 html,并且仍然可以保持美观。例如:
将变为:
Whitespace between elements (including newlines and tabs) cause browsers to insert spaces where there should be none.
The most elegant method that I've seen used to get around this issue is putting the
>
on the next line, instead of on the same line. This way, it's still legal html, and you can still keep it pretty.For example:
would become: