[英]Set div 50% height of parent div
I want the child div to be a 50% height of the parent, so if the parent div was 100%, the child div would be 50%, if the parent div was 1000px, the child div would still be 50%. 我希望子div为父级的50%高度,因此如果父div为100%,则子div为50%,如果父div为1000px,则子div仍为50%。
Here's a basic fiddle of what i've done so far http://jsfiddle.net/6PA6k/17/ 这是我到目前为止所做的基本小提琴http://jsfiddle.net/6PA6k/17/
Here's the html 这是html
<div class="wrapper">
<div class="button-container">
<div class="button"></div
</div>
</div>
Here's the css 这是css
* {
margin:0;
padding:0;
}
html {
height:100%;
width:100%;
}
body {
height:100%;
width:100%;
}
.wrapper {
height:100%;
width:100%;
background:red;
}
.button-container {
height:100%;
width:50px;
}
.button {
width:50px;
height:50px;
background:blue;
}
UPDATED 更新
Demo: http://jsfiddle.net/6PA6k/22/ 演示: http : //jsfiddle.net/6PA6k/22/
* {
margin:0;
padding:0;
}
html {
height:100%;
width:100%;
}
body {
height:100%;
width:100%;
}
.wrapper {
position: relative;
height:80%;
width:100%;
background:red;
}
.button-container {
position: relative;
height:50%;
width:300px;
background:blue;
}
.button {
position: absolute;
top: 50%;
height:50px;
width:50px;
background:yellow;
margin-top: -25px;
}
I may be confused about what you are asking, but changing: 我可能会对你提出的问题感到困惑,但改变了:
.button {
width:50px;
height:50px;
background:blue;
}
To: 至:
.button {
width:50px;
height:50%;
background:blue;
}
Does what you seem to want... http://jsfiddle.net/c3eB8/ 你似乎想要什么... http://jsfiddle.net/c3eB8/
EDIT 编辑
Ok, so now that I understand the question better, in order to center the child div vertically with a parent div without fixed dimensions, you can use absolute positioning. 好的,现在我更好地理解了这个问题,为了使子div垂直居中,没有固定尺寸的父div,你可以使用绝对定位。
.button {
width:50px;
height:50px;
background:blue;
margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
}
Here is a fiddle: http://jsfiddle.net/8UjvB/ 这是一个小提琴: http : //jsfiddle.net/8UjvB/
Maybe this: http://jsfiddle.net/helderdarocha/8p5MT/ 也许这个: http : //jsfiddle.net/helderdarocha/8p5MT/
It places the 50x50 button in the middle of the parent: 它将50x50按钮放在父级的中间:
.button-container {
height:100%;
width:50px;
background: green;
position: relative;
}
.button {
width:50px;
height:50px;
background:blue;
margin: auto;
position: absolute;
top: 0; bottom: 0;
}
(add position:relative
to the parent, and margin:auto; position:absolute; top:0; bottom:0
to the .button
statement) (添加
position:relative
对于父级, margin:auto; position:absolute; top:0; bottom:0
到.button
语句)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.