1.行高法
使文字内容的行高与div同高(最常用)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>行高法</title>
<style type="text/css">
div
{
width: 100px;
height: 40px;
background-color: #333;
}
p
{
line-height: 40px;
background-color: #baf;
}
</style>
</head>
<body>
<div id="div-1">
<p>西瓜小羽毛</p>
</div>
</body>
</html>
2.内边距法
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>内边距法</title>
<style type="text/css">
div
{
width: 100px;
height: 40px;
background-color: #333;
}
p
{
padding: 10px 0;
background-color: #baf;
}
</style>
</head>
<body>
<div id="div-1">
<p>西瓜小羽毛</p>
</div>
</body>
</html>
3.模拟表格法
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>模拟表格法</title>
<style type="text/css">
.wrapper
{
display: table;
background-color: #BBAAFF;
}
.cell
{
display: table-cell;
vertical-align: middle;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="cell">
<p>西瓜小羽毛</p>
</div>
</div>
</body>
</html>
4.translation
在不知道div大小的情况下很好用
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>transform属性</title>
<style type="text/css">
.box
{
width: 100px;
height: 60px;
background-color: #bfa;
}
p
{
position: relative;
top: 50%;
background-color: #BBAAFF;
transform: translateY(-50%);
/*
* translate参数百分比是移动自身长度的百分比
*/
}
</style>
</head>
<body>
<div class="box">
<p>西瓜小羽毛</p>
</div>
</body>
</html>
5.box布局
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>box布局</title>
<style type="text/css">
.center
{
width: 300px;
height: 200px;
padding: 10px;
border: 1px solid #ccc;
background:#000;
color:#fff;
margin: 20px auto;
display: -webkit-box;
-webkit-box-orient: horizontal;
-webkit-box-pack: center;
-webkit-box-align: center;
}
</style>
</head>
<body>
<div class="center">
<div class="txt">
<p>西瓜小羽毛</p>
<p>西瓜小羽毛</p>
<p>西瓜小羽毛</p>
</div>
</div>
</body>
</html>
6.flex布局
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>flex布局</title>
<style type="text/css">
.center
{
width: 300px;
height: 200px;
padding: 10px;
border: 1px solid #ccc;
background:#000;
color:#fff;
margin: 20px auto;
display: -webkit-flex;
align-items: center;
justify-content:center;
}
</style>
</head>
<body>
<div class="center">
<div class="txt">
<p>西瓜小羽毛</p>
<p>西瓜小羽毛</p>
<p>西瓜小羽毛</p>
</div>
</div>
</body>
</html>