简体   繁体   English

`display:inline-block`-elements上的奇怪边距

[英]Strange margin on `display:inline-block`-elements

I have 6 DIV s with display:inline-block in one line. 我有6个DIV display:inline-block一行display:inline-block But they have a strange white space between each other, how can I get rid of that? 但是他们之间有一个奇怪的白色空间,我怎么能摆脱它呢? They should fit in the container in one line. 它们应该在容器中装入一行。

Fiddle: http://jsfiddle.net/y7L7q/ 小提琴: http//jsfiddle.net/y7L7q/

HTML: HTML:

<div id="container">
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
</div>

CSS: CSS:

#container{
    width:300px;
    border:1px solid black;
}
.box{
    display:inline-block;
    height:50px;
    width:50px;
    background-color:black;
    margin:0;
    padding:0;
}

Because they are set to inline block this means the whitespace between them is treated as inline whitespace, and therefore rendered as such. 因为它们被设置为内联块,这意味着它们之间的空白被视为内联空格,因此呈现为内联空格。 You can fix this by either by putting all the divs on one line in the html or wrapping the white space in comments: 您可以通过将所有div放在html中的一行或将空白包装在注释中来解决此问题:

<div class="box"></div><!--
--><div class="box"></div>

Write font-size:0; font-size:0; . Like this: 像这样:

#container{
    width:300px;
    border:1px solid black;
    font-size:0;
}

Check this http://jsfiddle.net/y7L7q/1/ 检查这个http://jsfiddle.net/y7L7q/1/

OR 要么

Write your mark like this: 写下这样的标记:

<div id="container">
    <div class="box"></div><div class="box"></div><div class="box"></div><div class="box"></div><div class="box"></div><div class="box"></div>
</div>

Check this http://jsfiddle.net/y7L7q/2/ 检查这个http://jsfiddle.net/y7L7q/2/

It's a common issue with display: inline-block . 这是display: inline-block的一个常见问题display: inline-block You have a few solution, delete new lines (or comment them : http://jsfiddle.net/eaqfk/ ), set font-size: 0 , set margin-right: -4px . 你有一些解决方案,删除新行(或评论它们: http//jsfiddle.net/eaqfk/ ),设置font-size: 0 ,设置margin-right: -4px

Everything is on this question : CSS: Spacing issue with dropdown 一切都在这个问题上: CSS:间距问题与下拉列表

Instead of display:inline-block, use float:left to remove the unwanted space. 而不是显示:inline-block,使用float:left来删除不需要的空间。 Check this out. 看一下这个。

http://jsfiddle.net/y7L7q/9/ http://jsfiddle.net/y7L7q/9/

Also note that font-size:0 is not the correct approach in my opinion as it will mess up the content inside those divs. 另请注意,font-size:0在我看来不是正确的方法,因为它会弄乱这些div中的内容。

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

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