如何在HTML页面上垂直居中我的DIV?我认为通过将余量设置为0 auto
可以解决问题,但事实并非如此,因为它只是水平居中并且不会在页面上垂直居中我的#main div
这是一个快速的小提琴:https://jsfiddle.net/stevdbbz/
以下是有问题的HTML和CSS:
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
* {
font-family: Segoe UI;
font-size: 9pt;
box-sizing: border-box;
}
body {
background: rgb(70,70,70);
padding: 0;
margin: 0;
}
#main {
border: 1px solid rgb(180,180,180);
width: 800px;
height: 650px;
margin: 0 auto;
}
#boxA {
padding-top: 7px;
padding-bottom: 7px;
padding-left: 3px;
border-bottom: 1px solid rgb(180,180,180);
background: #FFF;
}
#boxB {
height: 573px;
width: 200px;
border: 0;
float: left;
background: #FFF;
}
#boxC {
background: rgb(240,240,240);
height: 573px;
width: 598px;
border-left: 1px solid rgb(180,180,180);
border-top: 0;
border-bottom: 0;
border-right: 0;
display: inline-block;
}
#boxD {
background: rgb(240,240,240);
border-top: 1px solid rgb(180,180,180);
height: 44px;
text-align: center;
display: table;
width: 100%;
}
#menu {
list-style-type: none;
padding: 0;
margin: 0;
}
#menu li {
padding: 4px;
border: 1px solid #FFF;
}
#menu li:hover {
cursor: pointer;
}
.selected {
background: rgb(51,153,255);
color: #FFF;
border: 1px solid #FFF;
font-weight: bold;
}
.hidden{ display:none; }
.item {
width: 100%;
height: 100%;
}
input[type="button"]
{
cursor:pointer;
border: 1px solid #707070;
background-color: #F0F0F0;
border-radius: 4px;
box-shadow: inset 0 1px 2px #fff, inset 0 -0.7em #DDD;
background-image: -ms-linear-gradient(top, #F1F1F1 0%, #E3E3E3 50%, #D0D0D0 100%);
padding: 3px 6px;
width: 75px;
}
input[type="button"]:hover
{
cursor:pointer;
background-color: #EAF6FD;
border: 1px solid #3C7FB1;
box-shadow: inset 0 1px 2px #fff, inset 0 -0.7em #BEE6FD, 0 0 3px #A7D9F5;
}
input[type="button"][disabled], input[type="button"][disabled]:hover
{
border: 1px solid #ADB2B5;
text-shadow: 1px 1px #fff;
cursor:default;
background-color: #F4F4F4;
box-shadow: inset 0 1px 2px #fff;
}
</style>
</head>
<body>
<div id="main">
<div id="boxA"><b>Application Title</b></div>
<div id="boxB">
<ul id="menu">
<li data-show="#1">Menu Item 1</li>
<li data-show="#2">Menu Item 2</li>
<li data-show="#3">Menu Item 3</li>
</ul>
</div>
<div id="boxC">
<div id="1" class="hidden item">
<b>Content Tab 1</b>
</div>
<div id="2" class="hidden item">
Content Tab 2
</div>
<div id="3" class="hidden item">
Content Tab 3
</div>
</div>
<div id="boxD">
<div style="display: table-cell; vertical-align: middle;">
<input type="button" value="Search" class="btn" disabled>
<input type="button" value="Save" class="btn" disabled>
<input type="button" value="Add" class="btn" disabled>
<input type="button" value="Clear All" class="btn">
<input type="button" value="Delete" class="btn" disabled>
<input type="button" value="Export" class="btn" disabled>
<input type="button" value="Recall" class="btn" disabled>
</div>
</div><!-- End of Main -->
</body>
</html>
答案 0 :(得分:0)
您可以display:table;
答案 1 :(得分:0)
您可以使用 3个简单规则将元素相对于视口垂直居中:
#main {
position: relative;
top: 50vh; // positions the top edge of the element 50% down the viewport
margin: -325px auto 0; // moves the top edge down the element by 325px (650px height / 2)
border: 1px solid rgb(180,180,180);
width: 800px;
height: 650px;
}
答案 2 :(得分:0)
您可以使用flexbox。这是一个例子:
<div class="middle-align">
<p>This is a vertically centered paragraph</p>
</div>
</div>
和css
.middle-align {
height:400px;
background-color:gray;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-ms-flex-align: center;
-webkit-align-items: center;
-webkit-box-align: center;
align-items: center;
}