hurriedly% 2011-02-22 12:41 采纳率: 100%
浏览 502
已采纳

如何删除行内块元素之间的空格?

Given this HTML and CSS:

span {
    display:inline-block;
    width:100px;
    background-color:palevioletred;
}
<p>
    <span> Foo </span>
    <span> Bar </span>
</p>

As a result, there will be a 4 pixel wide space between the SPAN elements.

Demo: http://jsfiddle.net/dGHFV/

I understand why this happens, and I also know that I could get rid of that space by removing the white-space between the SPAN elements in the HTML source code, like so:

<p>
    <span> Foo </span><span> Bar </span>
</p>

However, I was hoping for a CSS solution that doesn't require the HTML source code to be tampered with.

I know how to solve this with JavaScript - by removing the text nodes from the container element (the paragraph), like so:

// jQuery
$('p').contents().filter(function() { return this.nodeType === 3; }).remove();

Demo: http://jsfiddle.net/dGHFV/1/

But can this issue be solved with CSS alone?

</div>

转载于:https://stackoverflow.com/questions/5078239/how-do-i-remove-the-space-between-inline-block-elements

  • 写回答

29条回答 默认 最新

  • 零零乙 2011-02-22 12:47
    关注

    Since this answer has become rather popular, I'm rewriting it significantly.

    Let's not forget the actual question that was asked:

    How to remove the space between inline-block elements? I was hoping for a CSS solution that doesn't require the HTML source code to be tampered with. Can this issue be solved with CSS alone?

    It is possible to solve this problem with CSS alone, but there are no completely robust CSS fixes.

    The solution I had in my initial answer was to add font-size: 0 to the parent element, and then declare a sensible font-size on the children.

    http://jsfiddle.net/thirtydot/dGHFV/1361/

    This works in recent versions of all modern browsers. It works in IE8. It does not work in Safari 5, but it does work in Safari 6. Safari 5 is nearly a dead browser (0.33%, August 2015).

    Most of the possible issues with relative font sizes are not complicated to fix.

    However, while this is a reasonable solution if you specifically need a CSS only fix, it's not what I recommend if you're free to change your HTML (as most of us are).


    This is what I, as a reasonably experienced web developer, actually do to solve this problem:

    <p>
        <span>Foo</span><span>Bar</span>
    </p>
    

    Yes, that's right. I remove the whitespace in the HTML between the inline-block elements.

    It's easy. It's simple. It works everywhere. It's the pragmatic solution.

    You do sometimes have to carefully consider where whitespace will come from. Will appending another element with JavaScript add whitespace? No, not if you do it properly.

    Let's go on a magical journey of different ways to remove the whitespace, with some new HTML:

    <ul>
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
    </ul>
    
    • You can do this, as I usually do:

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

      http://jsfiddle.net/thirtydot/dGHFV/1362/

    • Or, this:

      <ul>
          <li>Item 1</li
          ><li>Item 2</li
          ><li>Item 3</li>
      </ul>
      
    • Or, use comments:

      <ul>
          <li>Item 1</li><!--
          --><li>Item 2</li><!--
          --><li>Item 3</li>
      </ul>
      
    • Or, you can even skip certain closing tags entirely (all browsers are fine with this):

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

    Now that I've gone and bored you to death with "one thousand different ways to remove whitespace, by thirtydot", hopefully you've forgotten all about font-size: 0.


    Alternatively, you can now use flexbox to achieve many of the layouts that you may previously have used inline-block for: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(28条)

报告相同问题?

悬赏问题

  • ¥15 求指导ADS低噪放设计
  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存