【发布时间】:2019-02-09 08:33:59
【问题描述】:
目标是使用百分比而不是绝对值在其父元素下方居中。这样,如果尺寸发生变化,位置值就不需要改变。
下面的hover 元素在outer 下方居中,但定位需要绝对值,这取决于hover 和outer 的大小。如果其中任何一个发生更改,则位置值必须更改。
可以通过百分比实现在父级下方居中吗?
Codepen:https://codepen.io/anon/pen/dadjpg
<div id="outer">
<div id="hover"></div>
</div>
#outer {
width: 200px;
height: 200px;
background: blue;
position: relative;
}
#hover {
width: 50px;
height: 50px;
background: yellow;
position: absolute;
margin: auto;
left: 75px;
bottom: -50px;
}
【问题讨论】: