简体   繁体   English

无法消除包含div内的2个水平div之间的空间

[英]cannot eliminate space between 2 horizontal divs inside containing div

Should be easy, right? 应该很容易吧? Just set the outer containing div's padding to zero, and set the two side-by-side divs inside the outer div to have margin:0 but that's having no effect on the space between the 2 horizontal divs. 只需将外部包含div的填充设置为零,并将外部div内的两个并排div设置为margin:0即可,但这对2个水平div之间的空间没有影响。 What I need is the red-outlined left div to touch the green-outlined right-side div. 我需要的是红色轮廓的左侧div触摸绿色轮廓的右侧div。

Despite my effort using padding and margin, the space between the 2 divs will not go away. 尽管我努力使用padding和margin,但2个div之间的空间不会消失。

I have looked at many answers on SO but so far no one's broken it down to this simple example -- my fiddle shows this issue, at 我已经看过很多关于SO的答案,但到目前为止,还没有人将其分解为这个简单的示例-我的小提琴在

http://jsfiddle.net/Shomer/tLZrm/7/ http://jsfiddle.net/Shomer/tLZrm/7/

And here is the very simple code: 这是非常简单的代码:

<div style="border: 4px solid blue; white-space:nowrap; margin:0; padding:0; width:80%">

   <div style="display:inline-block; width:45%; overflow:hidden; margin:0; border: 1px solid red"> Flimmy-flammy
    </div>

    <div style="display:inline-block; width:50%; overflow:hidden; margin:0px; border: 1px solid green"> Hambone-Sammy
    </div>

</div>

The space rendered between your divs is the whitespace (represented as dots), collapsed, in: div之间呈现的空间是折叠的空格(表示为点):

    </div>.  
.................  
....<div>

Instead, try coding like this: 而是尝试像这样编码:

    </div><div>

and the gap will vanish. 差距将消失。

Use the float property. 使用float属性。

Example with div { float: left; } div { float: left; } div { float: left; } : http://jsfiddle.net/tLZrm/10/ . div { float: left; }http : //jsfiddle.net/tLZrm/10/

The whitespace in the source between inline blocks leads to a gap in the layout. 内联块之间源中的空白导致布局中的空白。 By removing the space (whether it's a single space or some line breaks doesn't matter) the elements will touch as intended. 通过删除空格(无论是单个空格还是换行都无关紧要),元素将按预期方式触摸。

Formatting of the code can be retained at a slight cost, either by commenting out the whitespace or by making the whitespace occur within tags. 通过注释掉空格或使空格出现在标签中,可以以少量的成本保留代码的格式。

Using comments: 使用注释:

<div>
   <div>Content</div><!--
   --><div>Content</div>
</div>

Placing linebreak inside tag: 在标签内放置换行符:

<div>
   <div>Content</div
   ><div>Content</div>
</div>

Try this: 尝试这个:

<div style="border: 4px solid blue; margin:0; padding:0; width:80%; height: 50px;">

   <div style="float:left; display:inline-block; width:45%; overflow:hidden; border: 1px solid red;"> Flimmy-flammy
    </div>

    <div style="float: left; display:inline-block; width:50%; overflow:hidden; border: 1px solid green;"> Hambone-Sammy
    </div>

</div>  

Like @Juan Lanus said in his answer, it is the whitespace causing your issue. 就像@Juan Lanus在回答中所说的那样,是空格导致了您的问题。

An additional solution is to set font-size: 0px on the containing div. 另一个解决方案是在包含的div上设置font-size: 0px

But you will need to then also set font-size: initial (or to a non-zero value) on the children div so you can still see your text. 但是您还需要在子div上设置font-size: initial (或设置为非零值),以便仍然可以看到您的文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM