为什么当位置是相对的时,height和top属性不工作? [英] why does height and top attribute not work when position is relative?
问题描述
这会将框定位在顶部以下
This will position the box a little below the top
<div style="height: 10em; width: 50%; left: 25%; top:2em; position: relative; background: white;">Hello World</div>
这将把框放在顶部附近,它看起来不像height和top属性
工作。箱子的高度不是50%,箱子不是顶部以下50%。
This will position the box near the top and it looks like neither the height nor top property is
working. The height of the box is not 50%, and the box is not 50% below the top.
<div style="height: 50%; width: 50%; left: 25%; top:20%; position: relative; background: white;">Hello World</div>
我是一个初学者在这个东西,但它似乎如果左和宽工作与百分比不应高于和高度?
I am pretty much a beginner at this stuff, but it would seem if left and width work with a percentage shouldn't top and height?
推荐答案
您的盒子不能有 c $ c>是父级的父级的一个百分比,如果它的父级没有固定的高度,因为然后框不知道它需要多高(通常这样的父级将
body
或一些其他包装器元素)。
Your box can't have a height
that's a percentage of its parent if its parent doesn't have a fixed height, because then the box wouldn't know how high it needs to be (typically such a parent would be body
or some other wrapper element).
作为直接的结果, top
也不会有任何效果。
As a direct consequence, top
with a percentage won't have any effect either.
这篇关于为什么当位置是相对的时,height和top属性不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!