垂直居中页面上的元素
这个问题在这里已经有了答案:
试试下面:
Vertical-align
将适用于display:table;
html{
position: relative;
background-color: #e1e8f0;
font-size:62.5%;
margin: 0px;
padding: 0px;
font-family: Arial,"Helvetica Neue",Helvetica,sans-serif;
height: 500px;
}
.navbar{
border: none;
}
body{
background-color: #e1e8f0;
font-family: Arial,"Helvetica Neue",Helvetica,sans-serif;
position: relative;
height: 500px;
width: device-width;
}
.signup{
position: relative;
height: 100%;
width: 100%;
display:table;
margin: auto auto;
font-size: 1.6em;
font-family: Arial,"Helvetica Neue",Helvetica,sans-serif;
border: 1px solid black;
}
.signup input{
margin:20px;
text-decoration: none;
border: 1px solid #e4e6e8;
font-family: Arial,"Helvetica Neue",Helvetica,sans-serif;
border-radius: 5px;
padding: 2px;
}
.message{
width: 50%;
float: left;
overflow: hidden;
}
.message pre{
overflow: hidden;
}
table{
height: 100%;
margin: auto auto;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="E:d v/MEAN Lynda - Developing for the MEAN Stack and MongoDB/projects/others/lib/bootstrap.css">
<link rel="stylesheet" type="text/css" href="E:d v/MEAN Lynda - Developing for the MEAN Stack and MongoDB/projects/others/css/my css.css">
</head>
<body>
<nav class="navbar navbar-default" style="background-color:#222222;color:red;">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="file:///E:/d%20v/MEAN%20Lynda%20-%20Developing%20for%20the%20MEAN%20Stack%20and%20MongoDB/projects/others/lib/login.html" style="color:#fff;">SITE NAME</a>
</div>
<div class="collapse navbar-collapse" id="#myNavbar">
<ul class="nav navbar-nav navbar-right">
<li><a href="#" style="color:#fff;" >WHO</a></li>
<li><a href="#" style="color:#fff;" >Sign up</a></li>
<li><a href="#" style="color:#fff;" >Login</a></li>
</ul>
</div>
</div>
</nav>
<div class="signup">
<form action="#" class="center-block" method="get">
<table border="black">
<tr>
<td><label for="emailId">Email id</label>
<td><input type="email" placeholder="you@example.com" id="emailId"></input><br>
</tr>
<tr>
<td><label for="password">Password</label>
<td><input type="password" placeholder="********" id="password"></input><br>
</tr>
<tr>
<td><label for="name">Name</label>
<td><input type="text" placeholder="Steve Jobs :)" id="name"></input><br>
</tr>
<tr>
<td></td>
<td><button type="submit" class="btn btn-primary">Sign Up</button></td>
</tr>
</table>
</form>
</div>
您必须将样式应用于表单
<style>
form {
margin: 15% 40% 0;
}
</style>
几个解决方案:
您可以使用flex
及其属性( justify-content
和align-items
),例如vertical-align
,并将这些行添加到您想垂直对齐的每个父级(例如: .center
)
.center {
display:-webkit-flex;
display:flex;
align-items:center;
justify-content:center;
}
如果您不喜欢flex
解决方案,则可以将父级的位置设置为“相对”( position:relative;
),并将此代码插入到子级中:
.center {
position:relative;
}
.center table {
position:absolute;
top:50%;
left:50%;
-webkit-transform:translate(-50%,-50%);
-moz-transform:translate(-50%,-50%);
transform:translate(-50%,-50%);
}
希望它有帮助=]
链接地址: http://www.djcxy.com/p/75881.html