标签:
这点不用多说,水平居中用text-align:center,垂直居中用line-hight:高值
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
#one{
width: 400px;
height: 400px;
border: 1px solid red;
position: relative;/*父盒子要相对定位*/
}
#two{
width: 100px;
height: 100px;
border: 1px solid blue;
position: absolute;/*子盒子要绝对定位*/
left: 50%;
top: 50%;
margin-top: -50px;/*向上偏移自身高度的一半*/
margin-left: -50px; /*向左自身宽度的一半*/
}
</style>
</head>
<body>
<div id="one">
<div id="two"></div>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>居中table</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
#one{
width: 400px;
height: 400px;
border: 1px solid red;
display: table-cell;
vertical-align: middle;
text-align: center;
}
#two{
width: 100px;
height: 100px;
border: 1px solid blue;
display: inline-block;/*须转化为内联元素*/
}
</style>
</head>
<body>
<div id="one">
<div id="two"></div>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>css3transform</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
#one{
width: 400px;
height: 400px;
border: 1px solid red;
position: relative;
}
#two{
width: 100px;
height: 100px;
border: 1px solid blue;
left: 50%;
top: 50%;
-webkit-transform: translate(-50%, -50%);
position: absolute;
}
</style>
</head>
<body>
<div id="one">
<div id="two"></div>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>弹性盒子布局flex</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
#one{
width: 400px;
height: 400px;
border: 1px solid red;
display: flex;
justify-content: center;/*水平居中*/
align-items: center;/*垂直居中*/
}
#two{
width: 100px;
height: 100px;
border: 1px solid blue;
}
</style>
</head>
<body>
<div id="one">
<div id="two"></div>
</div>
</body>
</html>
标签:
原文地址:http://www.cnblogs.com/goweb/p/5577025.html