两个直线块元件,每个50%宽,不能并排安装在一排中

Lon*_*ner 83 html css

<!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>
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

但我原来的问题仍然存在.为什么当两个div保持为内联块元素时,它们并不适合并排?

Vuc*_*cko 129

使用inline-block元素时,whitespace 这些元素之间总会存在问题(该空间约为4像素宽).

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

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

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

.left{
  background-color: aqua;
}

.right{
  background-color: gold;
}
Run Code Online (Sandbox Code Playgroud)
<div class="left">foo</div>
<div class="right">bar</div>
Run Code Online (Sandbox Code Playgroud)


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

这些元素之间没有空格

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

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

.left{
  background-color: aqua;
}

.right{
  background-color: gold;
}
Run Code Online (Sandbox Code Playgroud)
<div class="left">foo</div><div class="right">bar</div>
Run Code Online (Sandbox Code Playgroud)

2.使用HTML注释

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

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

.left{
  background-color: aqua;
}

.right{
  background-color: gold;
}
Run Code Online (Sandbox Code Playgroud)
<div class="left">foo</div><!--
--><div class="right">bar</div>
Run Code Online (Sandbox Code Playgroud)

3.将父项设置font-size0,然后向inline-block元素添加一些值

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;
}
Run Code Online (Sandbox Code Playgroud)
<div class="parent">
  <div class="left">foo</div>
  <div class="right">bar</div>
</div>
Run Code Online (Sandbox Code Playgroud)

4.在它们之间使用负边距(不可取)

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;
}
Run Code Online (Sandbox Code Playgroud)
<div class="left">foo</div>
<div class="right">bar</div>
Run Code Online (Sandbox Code Playgroud)

5.降低闭合角度

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

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

.left{
  background-color: aqua;
}

.right{
  background-color: gold;
}
Run Code Online (Sandbox Code Playgroud)
<div class="left">foo</div
><div class="right">bar</div>

<hr>

<div class="left">foo</div><div class="right">
bar</div>
Run Code Online (Sandbox Code Playgroud)

6.跳过某些 HTML结束标记(感谢@thirtydot作为参考)

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;
}
Run Code Online (Sandbox Code Playgroud)
<ul>
  <li class="left">foo
  <li class="right">bar
</ul>
Run Code Online (Sandbox Code Playgroud)


参考文献:

  1. 在CSS技巧上战斗内联块元素之间的空间
  2. 删除David Walsh之间的内联块元素之间的空白
  3. 如何删除内联块元素之间的空格?

正如@MarcosPérezGude 所说,最好的方法是使用rem,并font-sizehtml标签上添加一些默认值(如在HTML5Boilerplate中).例:

html{
    font-size: 1em;
}

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

.ib-child{
    display: inline-block;
    font-size: 1rem;
}
Run Code Online (Sandbox Code Playgroud)

更新:因为它接近2018.使用flexbox甚至更好 - CSS网格布局.

  • 这是一种让程序员疯狂的无稽之谈. (16认同)
  • @hoosierEE那不是nosense.元素之间的空间很好,这是因为内联块元素将定位在文本行.如果你在一条线上放一个空格,就会有一个空格.所以行为是正确的,即使你认为这是一个问题(因为Vucko称之为错误). (3认同)
  • 我同意@MarcosPérezGude,这种行为是正确的.正如您在我的帖子中看到的,有几种方法可以删除该空格(我自己使用HTML注释来删除空格).但是如果你介意那个空格,你可以随时使用[`flexbox`](https://css-tricks.com/snippets/css/a-guide-to-flexbox/),[`table/table-row/table-cell`](http://colintoh.com/blog/display-table-anti-hero)或使用`float`. (2认同)
  • 我完美地使用内联块.我的常规方法是`parent {font-size:0; } child {font-size:1rem; }`.现在使用rems是最简单的 (2认同)

Mat*_*rix 21

css3中的好答案是:

white-space: nowrap;
Run Code Online (Sandbox Code Playgroud)

在父节点中,和:

white-space: normal;
vertical-align: top;
Run Code Online (Sandbox Code Playgroud)

在div(或其他)50%

例如:http://jsfiddle.net/YpTMh/19/

编辑:

还有另一种方式:

font-size: 0;
Run Code Online (Sandbox Code Playgroud)

对于父节点并在子节点中覆盖它

  • 最优雅的解决方案 (3认同)

Dan*_*Dan 6

这是因为你的两个div之间的空白被解释为一个空格.如果您将<div>标记排成一行,如下所示,则问题已得到纠正:

<div id="left"></div><div id="right"></div>
Run Code Online (Sandbox Code Playgroud)