首页 > css margin 百分比问题

css margin 百分比问题

margin 的top bottom的值使用百分时,如何使值相对于容器的高度进行计算?

目前用margin来调整上下间距,但是单位必须使用百分比.或者有其他办法使用百分比来调整元素的间距?


是根据宽度来的,认真看下标准


这样的场景感觉使用绝对定位更合适,也更加方便计算。


需要给html,body设置{height:100%},然后用height百分比就可以了,例子如下:
<!DOCTYPE html>
<html lang="en">
<head>

<meta charset="UTF-8">
<title>Document</title>

<style>
html,body{ height:100%; }
.one {height: 50%}
.two { height:50%; width:100%;background-color:lightgreen; line-height: 50%;}
.three{height: 50%;background-color: lightyellow}
</style>
</head>
<body>
<div class="one">
<div class="two">
<div class="three">内容</div>
</div>
</div>
</body>
</html>


可以试试em 或者 rem 不一定非要 %

【热门文章】
【热门文章】