<!DOCTYPE html>
<html>
<head>
<title>Width issue</title>
<style type="text/css">
body {
    margin: 0;
}
#left {
    width: 50%;
    background: lightblue;
    display: inline-block;
}
#right {
    width: 50%;
    background: orange;
    display: inline-block;
}
</style>
</head>
<body>
    <div id="left">Left</div>
    <div id="right">Right</div>
</body>
</html>

JSFdle:http://jsfiddle.net/5EcPK/

上面的代码试图将#Left div和#Right div并排放在一行中.但正如您在上面的JSFidleURL中看到的那样,情况并非如此.

我能够解决将其中一个div的宽度减少到49%的问题.请参见http://jsfiddle.net/mUKSC/.但是这不是一个理想的解决方案,因为在两个div之间出现了一个很小的差距.

另一种解决问题的方法是浮动两个div.见http://jsfiddle.net/VptQm/.这个很好用.

不过,我原来的问题仍然存在.当两个div都作为内联挡路元素保存时,为什么它们不能并排放在一起呢?

推荐答案

Update:因为是2021年,所以使用flexbox甚至更好-CSS grid layout而不是inline-block.


使用inline-block个元素时,始终会有whitespaceissue between those elements(该空间大约为~4px宽).

所以,你的两个divs,都有50%的宽度,加上whitespace(~4px)的宽度超过了divs%,所以它断裂了.你的问题的例子:

body{
  margin: 0; /* removing the default body margin */
}

div{
  display: inline-block;
  width: 50%;
}

.left{
  background-color: aqua;
}

.right{
  background-color: gold;
}
<div class="left">foo</div>
<div class="right">bar</div>


有几种方法可以解决这个问题:

1. No space between those elements

body{
  margin: 0; /* removing the default body margin */
}

div{
  display: inline-block;
  width: 50%;
}

.left{
  background-color: aqua;
}

.right{
  background-color: gold;
}
<div class="left">foo</div><div class="right">bar</div>

2. Using HTML comments

body{
  margin: 0; /* removing the default body margin */
}

div{
  display: inline-block;
  width: 50%;
}

.left{
  background-color: aqua;
}

.right{
  background-color: gold;
}
<div class="left">foo</div><!--
--><div class="right">bar</div>

3. Set the parents 100 to 101, and then adding some value to 102 elements

body{
  margin: 0; /* removing the default body margin */
}

.parent{
  font-size: 0;  /* parent value */
}

.parent > div{
  display: inline-block;
  width: 50%;
  font-size: 16px; /* some value */
}

.left{
  background-color: aqua;
}

.right{
  background-color: gold;
}
<div class="parent">
  <div class="left">foo</div>
  <div class="right">bar</div>
</div>

4. Using a negative margin between them (100)

body{
  margin: 0; /* removing the default body margin */
}

div{
  display: inline-block;
  width: 50%;
  margin-right: -4px; /* negative margin */
}

.left{
  background-color: aqua;
}

.right{
  background-color: gold;
}
<div class="left">foo</div>
<div class="right">bar</div>

5. Dropping closing angle

body{
  margin: 0; /* removing the default body margin */
}

div{
  display: inline-block;
  width: 50%;
}

.left{
  background-color: aqua;
}

.right{
  background-color: gold;
}
<div class="left">foo</div
><div class="right">bar</div>

<hr>

<div class="left">foo</div><div class="right">
bar</div>

6. Skipping 100 HTML closing tags(感谢@Thirtydot for the reference)

body{
  margin: 0; /* removing the default body margin */
}

ul{
  margin: 0; /* removing the default ul margin */
  padding: 0; /* removing the default ul padding */
}

li{
  display: inline-block;
  width: 50%;
}

.left{
  background-color: aqua;
}

.right{
  background-color: gold;
}
<ul>
  <li class="left">foo
  <li class="right">bar
</ul>


参考文献:

  1. Fighting the Space Between Inline Block Elements on CSS Tricks
  2. Remove Whitespace Between Inline-Block Elements by David Walsh
  3. How to remove the space between inline-block elements?

作为@MarcosPérezGude saidbest的方法是使用rem,并在html标签上向font-size添加一些默认值(如HTML5Boilerplate).例子:

html{
    font-size: 1em;
}

.ib-parent{             /* ib -> inline-block */
    font-size: 0;
}

.ib-child{
    display: inline-block;
    font-size: 1rem;
}

Html相关问答推荐

z—index总是 destruct 我的头,我该如何修复它?

HTML表行悬停仅适用于偶数行

为什么XPath返回的元素比我预期的要多?

R-markdown中的非顺序列表

附加点的列表样式

有没有一种方法可以很容易地将这个div从底部:0转换到顶部:0?

如何设计缠绕锚点元素的样式?

如何从通过 find-each 方法(在 Rails 应用程序中)生成的列表创建下拉菜单?

当浏览器宽度减小时字体大小变得太大

为什么 html 元素的 max-width 和 width 一起工作?

使用shinyjqui拖放到网格表中

在DIV上应用梯度模糊未如预期工作

从 Vue 应用程序的容器元素渲染 HTML

CSS 中的 fingerprint scanner 动画

如何使我的下拉菜单响应?

为四开本网站主页添加背景图片

使用 TailwindCSS 将徽标放在左上角,将菜单放在右上角

嵌套固定定位的 Div 滚动条渗透到前景中的 Div

在批处理文件中显示特定的 HTML 文本