问题
Consider the following jsfiddle for reference:
http://jsfiddle.net/apmmw2ma/
<div class='outer'>
<div class='inner'>
Inner.
</div>
Outer.
</div>
div.outer {
position: absolute;
left: 10px;
top: 10px;
border: 5px solid green;
padding: 10px;
}
div.inner {
position: absolute;
left: 0;
top: 100%;
border: 10px solid red;
padding: 15px;
}
As you can see, the “inner” box (with the red border) is positioned relative to the outer’s padding-box: left:0
positions it just to the right of outer’s border, and top:100%
appears to mean “100% of the content plus padding, but not the border”.
Unfortunately, adding box-sizing: border-box
to the outer div seems to have no effect.
I want to position a child element directly below its parent’s border-box, i.e. the two borders should abut no matter how thick they are. Is this possible?
回答1:
Unfortunately this is not possible without knowing the border widths in advance. If you don't know the border widths in advance, or if they are dynamic, then you're out of luck.1
The area of an element's containing block is indeed defined as the padding edge of the element forming the containing block. This is explicitly stated in the spec, and is by design; descendants aren't normally supposed to overflow the border of their container, unless the container has overflow: visible
and does not establish a BFC (and even then, the effect is only visual; it doesn't affect layout). Otherwise, the border isn't much of a border anymore.
Generally, if you want to lay out elements such that they interact by their border or outer edges, you don't want to lay them out as ancestors and descendants. At the very least you want them to be siblings2, otherwise they should be completely unrelated.
This seems like an oversight to me; the meaning of
top: x%
should really depend on thebox-sizing
value of the parent...
The purpose of box-sizing
is to change how the size of a box is calculated (i.e. whether or not the padding or the borders should add to the dimensions specified by width
and height
); while you can use it to alter the size of an element's padding box, the area of the containing block, if the element generates one, is still defined by that padding box.
1This could conceivably be solved with custom properties, but on the precondition that you must assign the same custom property to both the parent's border-width and to the child's respective offsets, which is basically another way of saying "you must know the border widths in advance" or at least, have control over them.
2Floats, for example, are highly predisposed to the border edge of boxes, so much so that they can appear to collapse margins in places where you normally wouldn't expect it to occur.
回答2:
Try using display:table;
for the outer div.
Fiddle: http://jsfiddle.net/apmmw2ma/9/
回答3:
Unfortunately it is not possible to do this without repeating the value of the parent’s border width.
However, if repeating the border width value is acceptable, the following solution works:
div.inner {
top: <desired top value, e.g. 100%>;
margin-top: <parent’s border-bottom-width>;
left: <desired left value, e.g. 0>;
margin-left: -<parent’s border-left-width>;
}
In the future, the same will be possible with calc()
, which at the time of this writing is not widely enough supported:
div.inner {
top: calc(<desired top value, e.g. 100%> + <parent’s border-bottom-width>);
left: calc(<desired left value, e.g. 0> - <parent’s border-left-width>);
}
If I may dream for the future, I would like to be able to refer to the property values of the parents/ancestors inside a calc()
expression. Then I could write something like:
/* Hypothetical code that will never work */
div.inner {
top: calc(100% + parent.border-bottom-width);
left: calc(0 - parent.border-left-width);
}
回答4:
Use box-shadow
This makes use of the box-sizing model the way you expect:
http://jsfiddle.net/apmmw2ma/6/
-webkit-box-shadow:inset 0px 0px 0px 5px green;
-moz-box-shadow:inset 0px 0px 0px 5px green;
box-shadow:inset 0px 0px 0px 5px green;
box-sizing:border-box;
-moz-box-sizing:border-box;
-webkit-box-sizing:border-box;
来源:https://stackoverflow.com/questions/25367174/is-it-possible-to-position-an-element-relative-to-the-border-box-of-its-parent