• 之间有换行符
  • 发布于 2024-08-12 19:57:03 字数 435 浏览 8 评论 0原文

    我有一个由带有 display: inline

  • 元素组成的水平菜单。
  • 这些元素应该无缝地彼此相邻。

    在源代码中,我希望将每个 li 放在一行上,以便于调试:

    <li class="item1 first"> ... </li>
    <li class="item2"> ... </li>
    ...
    

    但是,如果我在每个元素后面添加 \n ,则菜单项之间会有间隙。我认为这是预期的行为,但是有什么方法可以使用巧妙的“空白”设置或其他方式将其关闭吗?

    编辑:我无法使用浮动,这是在 CMS 中,可以选择将列表项居中。

    I have a horizontal menu consisting of <li> elements with display: inline.

    The elements are supposed to be next to each other seamlessly.

    In the source code, I would like to have each li on one line for easier debugging:

    <li class="item1 first"> ... </li>
    <li class="item2"> ... </li>
    ...
    

    However, if I add a \n after each element, the menu items have a gap between each other. I gather this is the intended behaviour, but is there any way to turn it off using a clever "white-space" setting or something?

    Edit: I can't use float, this is in a CMS with the option to center the list items.

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

    发布评论

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

    评论(4

    泡沫很甜 2024-08-19 19:57:03

    您可以避免渲染问题,但保持代码可维护,如下所示:

    <ul
      ><li>item 1</li
      ><li>item 2</li
      ><li>item 3</li
    ></ul>
    

    它删除了空格,但在文本编辑器中编辑项目仍然很容易,前提是您的 CMS 不会干扰您输入的标记!

    You can avoid the rendering issues but keep the code maintainable like this:

    <ul
      ><li>item 1</li
      ><li>item 2</li
      ><li>item 3</li
    ></ul>
    

    It removes the whitespace but it's still easy to edit the items in a text editor, provided your CMS doesn't mess with the markup you enter!

    终陌 2024-08-19 19:57:03

    你有能力编辑CSS吗?如果是这样,您可以尝试调整

  • 上的填充/边距。元素。看起来确实在可读性上下了很大的功夫。
  • 根据您使用的浏览器,您可以获得 HTML Tidy http://users.skynet.be/ mgueury/mozilla/,它为您提供了整理源代码的选项,这对于调试可能足够有用

    Do you have the ability to edit the CSS? If so, you could try adjusting the padding/margins on the <li> element. It does seem to be a lot of effort of readability.

    Depending on what browser you are using you can get the HTML Tidy http://users.skynet.be/mgueury/mozilla/, which gives you the option to Tidy up your source, which might be useful enough for debugging

    烟花易冷人易散 2024-08-19 19:57:03

    CSS+float 是你的朋友。

    CSS+float is your friend.

    浪荡不羁 2024-08-19 19:57:03

    HTML 与空白无关 - 因此添加换行符应该没有效果。然而,某些浏览器的渲染引擎并不能完全正确地做到这一点。

    您可能想要做的是将

    float: left;

    添加到 li 标记以使它们彼此相邻。

    HTML is whitespace independent - so adding line breaks should have no effect. Some browser's rendering engines don't quite get this right however.

    What you probably want to do is add

    float: left;

    to your li tags to get them next to each other.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文