如何在没有过多注释的情况下消除空格?

发布于 2024-10-23 20:03:53 字数 359 浏览 4 评论 0原文

这是我页面中的代码片段:

<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

最终幸福 2024-10-30 20:03:53

可以考虑的一个选择是——如果可以的话,使用模板引擎。例如,在 Smarty 中,有一个 {strip} 函数正是执行此操作:

{* the following will be all run into one line upon output *}
{strip}
<table border='0'>
 <tr>
  <td>
   <a href="{$url}">
    <font color="red">This is a test</font>
   </a>
  </td>
 </tr>
</table>
{/strip}

输出:

<table border='0'><tr><td><a href="http://. snipped...</a></td></tr></table>

One option to consider - use a templating engine if you can. For example, in Smarty, there's a {strip} function that does exactly this:

{* the following will be all run into one line upon output *}
{strip}
<table border='0'>
 <tr>
  <td>
   <a href="{$url}">
    <font color="red">This is a test</font>
   </a>
  </td>
 </tr>
</table>
{/strip}

Output:

<table border='0'><tr><td><a href="http://. snipped...</a></td></tr></table>
歌入人心 2024-10-30 20:03:53

您可以使用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 set font-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/)

不念旧人 2024-10-30 20:03:53

空白仅在在内联元素周围或旁边时显示,因此至少对于您不需要的表单(如果您尚未将表单设置为 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 use display:block on the input before it and remove that element and the comments around it.

碍人泪离人颜 2024-10-30 20:03:53

元素之间的空格(包括换行符和制表符)会导致浏览器在不应该有空格的地方插入空格。

我见过的解决此问题的最优雅的方法是将 > 放在下一行,而不是同一行。这样,它仍然是合法的 html,并且仍然可以保持美观。

例如:

<input value="Search" type="submit" />
    <span class="vdivider"></span>
    </form><form action="login_action.php" method="post">
    <?php>

将变为:

<input value="Search" type="submit" 
    /><span class="vdivider"></span
    ></form><form action="login_action.php" method="post"
    ><?php>

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:

<input value="Search" type="submit" />
    <span class="vdivider"></span>
    </form><form action="login_action.php" method="post">
    <?php>

would become:

<input value="Search" type="submit" 
    /><span class="vdivider"></span
    ></form><form action="login_action.php" method="post"
    ><?php>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文