简体   繁体   English

使用display:inline-block的CSS问题

[英]CSS issue with using display:inline-block

I have set up 4 divs all of width: 120px placed inside of a wrapper of 240px wide. 我设置了4个div宽度:120px放置在240px宽度的包装纸内。

JSFiddle here - http://jsfiddle.net/s7dxxro0/1/ JSFiddle在这里-http: //jsfiddle.net/s7dxxro0/1/

HTML: HTML:

<div id="wrapper">  
  <div class="primary-content-block" id="about">
  </div>
  <div class="primary-content-block" id="gallery">
  </div>
  <div class="primary-content-block" id="contact">
  </div>
  <div class="primary-content-block" id="news">
  </div>
</div>

CSS: CSS:

#wrapper {
margin:0 auto;
width: 240px;
}

.primary-content-block {
display:inline-block;
vertical-align:top;
width: 120px;
height: 120px;
}

#about {
background: radial-gradient(#5cc4ba, #00aa99);
}

#gallery {
background: radial-gradient(#5cc4ba, #00aa99);
}

#contact {
background: radial-gradient(#5cc4ba, #00aa99);
}

#news {
background: radial-gradient(#5cc4ba, #00aa99);
} 

However the elements to not display next to each other due to a slight margin being applied to my 4 blocks. 但是,由于对我的4个块应用了少量边距,因此元素不会彼此相邻显示。

Where does this what seems to be a margin come from? 这似乎是从哪里来的呢? How do I remove it? 如何删除它?

This works when I use float:left in place of display:inline-block but I would prefer not to use floats for many reasons 当我使用float:left代替display:inline-block时,此方法有效,但由于许多原因,我不希望使用float

This is because there are spaces between your inline blocks. 这是因为内联块之间存在空格。 The elements are separated by whitespace (new lines also count for this unfortunately), so the browser puts a space in between them as if they were words. 元素之间用空格分隔(不幸的是,换行符也是如此),因此浏览器在它们之间放置一个空格,就好像它们是单词一样。

There's several ways to battle this. 有几种方法可以解决这个问题。

Put the html tags side to side: 将html标记放到一边:

<div class="primary-content-block" id="about">
</div><div class="primary-content-block" id="gallery">
</div><div class="primary-content-block" id="contact">

Use a negative margin on the divs: 在div上使用负边距:

.primary-content-block {
    margin-right: -4px;
}

Set the font size to 0: 将字体大小设置为0:

#wrapper {
    font-size: 0;
}
#wrapper > div {
    font-size: 12px /* or whatever it was before */
}

Or in case of <p> elements (not divs, unfortunately) just leave out the closing tag: 或者,如果是<p>元素(不幸的是不是divs),则省略结束标记:

<p class="primary-content-block" id="about">
<p class="primary-content-block" id="gallery">

Source: css-tricks 资料来源: css-tricks

The issue was caused by linebreaks between my divs 问题是由我的div之间的换行引起的

The fix would be: 解决方法是:

<div id="wrapper">  
  <div class="primary-content-block" id="about"></div><div class="primary-content-block" id="gallery"></div><div class="primary-content-block" id="contact"></div><div class="primary-content-block" id="news"></div>
</div>

you can simple add float: left the div 您可以简单地添加浮点数:离开div

.primary-content-block {
    /* display:inline-block; */
    vertical-align:top;
    width: 120px;
    height: 120px;
    float: left;
}

like this http://jsfiddle.net/s7dxxro0/10/ 像这样http://jsfiddle.net/s7dxxro0/10/

This isn't a "bug" (I don't think). 这不是一个“ bug”(我不认为)。 It's just the way setting elements on a line works.The spaces between these blocks are just like spaces between words. 这就是设置行上元素的方式,这些块之间的空格就像单词之间的空格一样。 You can scoot the elements back into place with negative 4px of margin (may need to be adjusted based on font size of parent) 您可以以4px的负空白将元素放回原处(可能需要根据父字体大小进行调整)

.primary-content-block {
   display:inline-block;
   vertical-align:top;
   width: 120px;
   height: 120px;
}

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

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