Skip to content

Instantly share code, notes, and snippets.

@Teraflopst
Last active August 11, 2016 20:06
Show Gist options
  • Save Teraflopst/972968ccc4040adcc33c383ed6f7bd02 to your computer and use it in GitHub Desktop.
Save Teraflopst/972968ccc4040adcc33c383ed6f7bd02 to your computer and use it in GitHub Desktop.
CSS 布局方案
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>CSS布局</title>
<style type="text/css">
.container {
width: 400px;
height: 600px;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
background: #FF004E;
}
</style>
</head>
<body>
<div class="container">
<h2>transform 实现垂直水平居中</h2>
<p>优点:不需要知道宽高,灵活</p>
<p>缺点:兼容不好,在Mobile设备上建议使用</p>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>CSS布局</title>
<style type="text/css">
.container {
width: 400px;
height: 600px;
background: #FF004E;
top: 50%;
left: 50%;
position: absolute;
margin-left: -200px;
margin-top: -300px;
}
</style>
</head>
<body>
<div class="container">
<h2>绝对定位实现垂直水平居中</h2>
<p>优点:兼容性好</p>
<p>缺点:需要知道宽高,不够灵活</p>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment