我有一个父div和一个子div.I想中心知道我是否可以表示宽度和高度作为父母的百分比,例如居中根据父div的子div
<!doctype html>
<head>
<meta charset='utf-8' />
<title>Horizontal center a div</title>
<style type='text/css'>
.one{
width:800px;
height:100px;
background-color:grey;
}
.two{
margin-top:30px;
margin-bottom:30px;
min-height:40px;
margin-left:30px;
width:90px;
background-color:pink;
display:inline-block;
}
</style>
</head>
<body>
<div class="one">
<div class="two">lorem ipsum</p>
</div>
</body>
</html>
要看到它在行动http://jsfiddle.net/thiswolf/3qRgH/
外格(父)具有height of 100px
和我的孩子div有一个height of 40px
。我想居中父主题在我的孩子,所以,100px - 40px = 60px
和我将60px分为两次,并指定子div margin-top:30px
和margin-bottom:30px;
。我howev呃想以百分比的形式表达利润率。我将如何表达利润率%。
如果。一是宽度是固定的,你可以自己算算,并表达百分之它,如果你的变量可以使用JavaScript实时更改它。见http://www.w3schools.com/css/css_margin.asp – dotoree
在这种情况下,简单的答案是30%,但那不是。 – Gandalf