简体   繁体   English

意外的CSS行为?

[英]Unexpected CSS Behavior?

While playing around with divider lines that take the full height of a container without a parents height, I stumbled across the below scenario. 在使用分隔线占据容器的整个高度而没有父母的高度时,我偶然发现了以下情况。 Can any one figure out why setting the dividers to position:absolute and display:inline-block does not cause them to float all the way to left of the parent container like expected? 谁能找出为什么将分隔符设置为position:absolutedisplay:inline-block不会导致它们像预期的那样一直浮动到父容器的左侧吗? Why are they actually inline? 为什么它们实际上是内联的?

HTML 的HTML

<div class="wrapper">
  <div class="box"></div>
  <div class="divider"></div>
  <div class="box"></div>
  <div class="divider"></div>
  <div class="box"></div>
</div>

CSS 的CSS

.wrapper{
  width:100%;
  text-align:center;
  position: relative;
}

.box{
  display: inline-block;
  width: 150px;
  height: 100px;
  background: red;
  margin: 0 0 0 5px;
}

.divider{
  display: inline-block;
  position: absolute;
  top: 0;
  bottom: 0;
  width: 1px;
  background: black;
}

JSFIDDLE JSFIDDLE

That's because both left and right are auto . 这是因为leftright都是auto

Then, according to §10.3.7 , 然后,根据§10.3.7

[If] 'left' and 'right' are 'auto' and 'width' is not 'auto', then if the 'direction' property of the element establishing the static-position] containing block is 'ltr' set 'left' to the static position , otherwise set 'right' to the static-position . [如果]“左”和“右”为“自动”,“宽度”不是“自动”,则如果包含块的静态位置的“方向”属性为“ ltr”,则设置为“左”设置为静态位置 ,否则将“正确”设置为静态位置 Then solve for 'left' (if 'direction is 'rtl') or 'right' (if 'direction' is 'ltr'). 然后求解“ left”(如果“ direction”为“ rtl”)或“ right”(如果“ direction”为“ ltr”)。

Where the static position is defined as 静态位置定义为

The static position for 'left' is the distance from the left edge of the containing block to the left margin edge of a hypothetical box that would have been the first box of the element if its 'position' property had been 'static' and 'float' had been 'none'. “左侧”的静态位置是从包含块的左侧边缘到假设框的左侧边缘的距离,如果该框的元素的“位置”属性为“静态”和“浮动”一直没有。

If you don't want that, specify a value: 如果您不想这样做,请指定一个值:

.divider {
  left: 0;
}

Can any one figure out why setting the dividers to position:absolute and display:inline-block does not cause them to float all the way to left of the parent container like expected? 谁能找出为什么将分隔符设置为position:absolutedisplay:inline-block不会导致它们像预期的那样一直浮动到父容器的左侧吗?

Because you did not specify a left value, so it is implicitly auto , and that means their left edge is positioned at the place they would have in normal flow, if they were not absolutely positioned. 因为您没有指定left值,所以它是隐式为auto ,这意味着如果未绝对定位,则它们的左边缘将位于正常流中的位置。

This behavior is described and specified here: http://www.w3.org/TR/CSS21/visudet.html#static-position 此处描述并指定了此行为: http : //www.w3.org/TR/CSS21/visudet.html#static-position


And btw., setting display: inline-block has no effect here, the calculated value of display will be block automatically, see http://www.w3.org/TR/CSS21/visuren.html#dis-pos-flo 顺便说一句,设置display: inline-block在这里无效, display的计算值将被自动block ,请参阅http://www.w3.org/TR/CSS21/visuren.html#dis-pos-flo

display: inline-block is necessary here to make the elements take their static position in between the boxes in the first place – so it is necessary for the observed effect to work. display: inline-block需要在这里做出的元素采取摆在首位的箱子之间的静态位置 -所以有必要对所观察到的效应来工作。 (Only “after” position:absolute is applied, the computed value will become block .) (仅在“之后” position:absolute应用position:absolute计算值变为 block 。)


And the technique you have chosen to position those dividers here might not be the best choice, at least if your layout is fluid – narrow the result window of your fiddle to a point where the three red boxes don't all fit onto one line, and you'll see what I mean … 而且,您选择的将那些分隔线放置在此处的方法可能不是最佳选择,至少在您的布局流畅的情况下–将小提琴的结果窗口缩小到三个红色框都不适合一行的位置,然后您会明白我的意思……

If you want all dividers to stay on the left of their parent, change their display style 如果您希望所有分隔线都位于其父级的左侧,请更改其显示样式

instead of: 代替:

display: inline-block;

use: 采用:

display: block;

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

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