How Do I Avoid Line-Break Padding?

≡放荡痞女 提交于 2019-12-05 22:23:00

问题


My biggest gripe with HTML is that line breaks add a tiny bit of space between elements. (jsFiddle.)

This can screw up layouts where child elements are sized to exactly fit their parents.

I read somewhere that you can remove this implicit padding - while still keeping the code somewhat legible - by using comments like this:

<!--
--><div>Foo</div><!--
--><div>Bar</div><!--
--><div>And so on...</div><!--
-->

This works, but I feel like there has to be a better solution. What other ways are there to work around the line-break padding?


回答1:


That isn't "a little bit of space", but literally a space character. You are using display: inline-block to align your elements horizonally, and that's how "inline" works.

If you want to use inline-block you need to remove the white space between the elements as you are doing it.

Otherwise you can use one of the other methods to horizontally align, for example floating or display: table-cell.




回答2:


A solution would be to use some HTML compressor before publishing your pages to remove unneeded space from your markup, like in this example.

From what I've seen though, they tend to leave always one space at least, because they don't know if you really wanted that space or not, and since browsers considers only the first space if there are more than one, compressors leave one space there.




回答3:


You should try font-size:0px; line-height:0px for outer div.

Something like this:

<div class="outer">
  <div class="inner">123</div>
  <div class="inner">34556</div>
</div>

<style>
.outer {
  font-size:0px;
  line-height:0px;
}

.inner {
  font-size:14px;
  line-height:16px;
  display:inline-block;
}
</style>



回答4:


This is because you use display: inline-block; for the div elements.

Block elements strip white space around them, inline elements don't.

Try float: left; instead.



来源:https://stackoverflow.com/questions/7185816/how-do-i-avoid-line-break-padding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!