为什么这个内联块元素被向下推? [英] Why is this inline-block element pushed downward?

查看:29
本文介绍了为什么这个内联块元素被向下推?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的代码,我想了解 为什么 #firstDiv 被所有浏览器向下推.我真的很想理解这个事实的内部运作,为什么它被向下推而不是以一种或另一种方式向上拉.(而且我知道如何对齐他们的上衣:))

而且我知道它的 overflow:hidden 导致它但不确定为什么它向下推动该 div.

body {宽度:350px;边距:0px 自动;}#容器 {边框:15px纯橙色;}#firstDiv {边框:10px纯棕色;显示:内联块;宽度:70px;溢出:隐藏;}#secondDiv {边框:10px纯天蓝色;向左飘浮;宽度:70px;}#thirdDiv {显示:内联块;边框:5px纯黄绿色;}

<div id="firstDiv">第一个</div><div id="secondDiv">SECOND</div><div id="thirdDiv">第三<br>更多内容<br>还有一些内容

http://jsfiddle.net/WGCyu/.

解决方案

基本上,您在代码中添加了更多混乱,这会造成更多混乱,因此首先我尝试消除妨碍理解真正问题的混乱.

首先我们必须确定真正的问题是什么?这就是inline-block"元素被向下推的原因.

现在我们开始了解它,首先去除杂乱.

1 -为什么不给所有三个 div 相同的边框宽度?让我们给它.

2 - 浮动元素与被向下推的内联块元素有什么联系吗?不,这与它无关.

所以,我们已经完全删除了那个 div.并且您正在目睹内联块元素被向下推的相同行为.

现在轮到一些文献来了解线框的概念以及它们如何排在同一行,尤其是请仔细阅读最后一段,因为这就是您问题的答案.

<块引用>

'inline-block' 的基线是它在正常流中最后一个 line box 的基线,除非它没有 in-flow line box 或者它的 'overflow' 属性的计算值不是 'visible',在这种情况下,基线是底部边距边缘.

如果您不确定基线,那么这里有简单的简要说明.

除'gjpqy'之外的所有字符都写在基线上,您可以将基线视为在这些随机字符"下方绘制一条与下划线相同的简单水平线,那么它将成为基线,但现在如果您写下任何一个'gjpqy' 字符位于同一行,那么这些字符的下部将落在该行下方.

所以,我们可以说除了'gjpqy'之外的所有字符都完全写在基线之上,而这些字符的一部分写在基线之下.

3 - 为什么不检查我们生产线的基线在哪里?我添加了几个显示我们线路基线的字符.

4 - 为什么不在我们的 div 中添加一些字符以在 div 中找到它们的基线?在这里,在 div 中添加了一些字符以阐明基线.

现在当您了解基线时,请阅读以下有关内联块基线的简化版本.

i) 如果有问题的内联块的溢出属性设置为可见(默认情况下,因此无需设置).那么它的基线将是该行包含块的基线.

ii) 如果有问题的内联块的溢出属性设置为 OTHER THAN 可见.那么它的下边距将在包含框的行的基线上.

  • 第一点细节

现在再看一遍,以阐明您的概念,即绿色 div 发生了什么.如果还有任何混淆,那么这里添加了更多靠近绿色 div 的字符以建立包含块的基线,绿色 div 基线是对齐.

好吧,我现在声称它们具有相同的基线?对吗?

5 - 那么为什么不将它们重叠起来,看看它们是否合适?所以,我带来了第三个 div -left: 35px;检查他们现在是否有相同的基线?

现在,我们已经证明了我们的第一点.

  • 第二点细节

好吧,在解释了第一点之后,第二点很容易理解,您会看到第一个将溢出属性设置为可见(隐藏)以外的 div 的底部边距位于该行的基线上.

现在,你可以做几个实验来进一步说明它.

  1. 设置第一个 div 溢出:可见(或完全删除).
  2. 设置第二个div溢出:不可见.
  3. 设置两个div溢出:不可见.

现在把你的杂物带回来,看看你是否觉得一切都很好.

  1. 带回浮动的div(当然需要
    增加一些身体的宽度)你看它没有效果.
  2. 带回相同的奇怪边距.
  3. 绿色 div 设置为溢出:在您的问题中设置可见(未对齐是由于从1px 到 5px 所以如果调整负向左你会发现没有问题)
  4. 现在删除我添加的其他字符以帮助理解.(当然,删除负左)
  5. 最后减小主体宽度,因为我们不再需要更宽的宽度.

现在我们又回到了起点.

希望我已经回答了您的问题.

Following is my code and I want to understand that why #firstDiv is being pushed downward by all browsers. I really want to understand the inner workings of the fact that why its being pushed downward rather than pulling it upward by one way or another. (and I know how to align their tops :))

And I know that its overflow:hidden which is causing it but not sure that why its pushing that div downward.

body {
  width: 350px;
  margin: 0px auto;
}

#container {
  border: 15px solid orange;
}

#firstDiv {
  border: 10px solid brown;
  display: inline-block;
  width: 70px;
  overflow: hidden;
}

#secondDiv {
  border: 10px solid skyblue;
  float: left;
  width: 70px;
}

#thirdDiv {
  display: inline-block;
  border: 5px solid yellowgreen;
}

<div id="container">
  <div id="firstDiv">FIRST</div>
  <div id="secondDiv">SECOND</div>
  <div id="thirdDiv">THIRD
    <br>some more content<br> some more content
  </div>
</div>

http://jsfiddle.net/WGCyu/.

解决方案

Basically you have added more clutter in your code which is creating more confusion so first I try to remove clutter which hinders understanding the real issue.

First of all we have to establish that what's the real question? Its that why "inline-block" element is pushed downward.

Now we start to understand it and remove the clutter first.

1 - Why not give all three divs same border width? Let's give it.

2 - Does floating element has any connection with inline-block element being pushed downward? No, it has nothing to do with it.

So, we have removed that div altogether. And you are witnessing same behavior of inline-block element being pushed downward.

Here comes the turn of some literature to grasp the idea of line boxes and how they are lined in the same line esp read last paragraph carefully because there lies the answer of your question.

The baseline of an 'inline-block' is the baseline of its last line box in the normal flow, unless it has either no in-flow line boxes or if its 'overflow' property has a computed value other than 'visible', in which case the baseline is the bottom margin edge.

If you are not sure about baseline then here is brief explanation in simple words.

All characters except 'gjpqy' are written on the baseline you can think of baseline as if you draw a simple horizontal line same as underlining right below these "random characters" then it will be the baseline but now if you write any of 'gjpqy' character(s) on the same line then lower part of these characters would fall below the line.

So, we can say that all characters except 'gjpqy' are written completely above the baseline while some part of these characters are written below the baseline.

3 - Why not check where is the baseline of our line? I have added few characters which show the baseline of our line.

4 - Why not add some characters in our divs too to find their baselines in the div? Here, some characters added in divs to clarify baseline.

Now when you understand about baseline, read the following simplified version about baseline of inline-blocks.

i) If inline-block in question has its overflow property set to visible (which is by default so no need to set though). Then its baseline would be the baseline of the containing block of the line.

ii) If inline-block in question has its overflow property set to OTHER THAN visible. Then its bottom margin would be on the baseline of the line of containing box.

  • First point in detail

Now look at this again to clarify your concept that what's happening with green div. If yet any confusion then here is added more characters close to green div to establish the baseline of the containing block and green div baseline is aligned.

Well, I am now claiming that they have same baseline? RIGHT?

5 - Then why not overlap them and see if they are fit right one on another? So, I bring third div -left: 35px; to check if they have same baseline now?

Now, we have got our first point proved.

  • Second point in detail

Well, after explanation of first point second point is easily digestible and you see that first div which has overflow property set to other than visible (hidden) has its bottom margin on the base line of the line.

Now, you can do couple of experiments to further illustrate it.

  1. Set first div overflow:visible (or remove it altogether).
  2. Set second div overflow: other than visible.
  3. Set both divs overflow: other than visible.

Now bring back your clutter and see if everything is looking to fine to you.

  1. Bring back your floated div (of course there is need to
    increase some width of body) You see it has no effect.
  2. Bring back same odd margins.
  3. Set green div to overflow: visible as you set in your question (that misalignment is due to increase of border width from 1px to 5px so if adjust negative left you'll see there is no issue)
  4. Now remove additional characters I added to aid in understanding. (and of course remove negative left)
  5. Finally reduce body width because we no longer need wider one.

And now we are back to where we started from.

Hopefully I have answered your question.

这篇关于为什么这个内联块元素被向下推?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆