简体   繁体   English

删除div之间的空白

[英]Delete white space between divs

I'm getting some strange whitespace between two divs I have.我在两个 div 之间出现了一些奇怪的空白。

Each div has the css property display: inline-block and each have a set height and width.每个 div 都有 css 属性display: inline-block并且每个 div 都有一个设置的高度和宽度。

I cannot find where the whitespace is.我找不到空格在哪里。

Here is a Fiddle这是一个小提琴

You get whitespace there because you have whitespace inbetween the divs.你在那里得到空白是因为你在 div 之间有空白。 Whitespace between inline elements is interpreted as a space.内联元素之间的空白被解释为空格。

You have:你有:

<div id="left_side">
    <div id="plan">
        <h1>div 1</h1>
    </div>
</div>
<div id="right_side">
    <div id="news">
        <h1>div 2</h1>
    </div>
</div>

Change for:为。。改变:

<div id="left_side">
    <div id="plan">
        <h1>div 1</h1>
    </div>
</div><div id="right_side">
    <div id="news">
        <h1>div 2</h1>
    </div>
</div>

However, this is a bad way to do what you want to do.但是,这是做您想做的事情的糟糕方法。

You should float the elements if thats what you want to do.如果那是你想要做的,你应该浮动元素。

Use:用:

float:left;
clear:none;  

In both div在两个div

如果您想保留您的编码布局,请避免浮动并将每个 div 完全保留在自己的行上...

<div id="leftSide">Some content here</div><!-- --><div id="rightSide">Some more content here</div>

Only add this to your CSS仅将此添加到您的 CSS

h1 {
    padding:0;
    margin:0;
   }

Space between div is only due to h1 Margin and Padding div 之间的空间只是由于 h1 Margin 和 Padding

This does the trick:这是诀窍:

<div id="left_side">
    ...
</div><div id="right_side">
    ...
</div>

Notice how the right-side div starts immediately after the closing tag of the left-side div.请注意右侧 div 如何在左侧 div 的结束标记之后立即开始。 This works because any space between the elements, since they are now inline, would become a space in the layout itself.这是有效的,因为元素之间的任何空间,因为它们现在是内联的,将成为布局本身的空间。 You can mirror this behavior with two span elements.您可以使用两个跨度元素来反映此行为。

Demo .演示

You can also add display: flex;您还可以添加display: flex; to the divs' parent container (in this case, body ).到 div 的父容器(在本例中为body )。 Fiddle .小提琴

最好的方法是将父元素的字体大小设置为 0,然后将正常字体大小设置为该父元素内的子元素(否则从父元素继承零)

Floated both of the elements left, also made the 30% width into 40% to fill all the space, but this isn't necessary.将两个元素都向左浮动,同时将 30% 的宽度变成 40% 以填充所有空间,但这不是必需的。 Please be aware, "inline-block" isn't supported by IE7 but can be fixed with a workaround.请注意,IE7 不支持“inline-block”,但可以通过一种变通方法进行修复。

http://jsfiddle.net/RVAQp/3/ http://jsfiddle.net/RVAQp/3/

将这些语句移到同一行:

</div><div id="right_side">

Tried using float instead of "inline-block", no problems.尝试使用 float 而不是“inline-block”,没问题。 Just changed the display:inline-block to:刚刚将 display:inline-block 更改为:

#left_side {float: left;}

and

#right_side {float: right; margin-right: 10%}

No apparent problems.没有明显的问题。 Could be wrong.可能是错的。

Don't know why but I resolved this problem by adding border: 1px solid red;不知道为什么,但我通过添加border: 1px solid red;解决了这个问题border: 1px solid red; (vertical) and float: left; (垂直)和float: left; (horizontal) to related DIV style statement and white-spaces removed. (水平)到相关的 DIV 样式声明和空格被删除。

父 div 设置为 font-size: 0px 和 childs 设置为想要的大小,如 17px :)

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

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