1.第一種
利用margin,div1的寬減去div2的寬就是div2margin-left的數(shù)值:(100-40)/2=30
div1的高減去div2的高就是div2margin-top的數(shù)值:(100-40)/2=30
按 Ctrl+C 復(fù)制代碼
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>demo</title>
</head>
<body>
<style type="text/css">
.div1{ width: 100px; height: 100px; border: 1px solid #000000;}
.div2{ width:40px ; height: 40px; background-color: green;}
.div22{
margin-left: 30px;margin-top: 30px;
}
</style>
<div class="div1">
<div class="div2 div22">
</div>
</div>
</body>
</html>

第一種
第2種
利用css的 position屬性,把div2相對(duì)于div1的top、left都設(shè)置為50%,然后再用margin-top設(shè)置為div2的高度的負(fù)一半拉回來(lái),用marg-left設(shè)置為寬度的負(fù)一半拉回來(lái),css如下設(shè)置
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>demo</title>
</head>
<body>
<style type="text/css">
.div1{ width: 100px; height: 100px; border: 1px solid #000000;}
.div2{ width:40px ; height: 40px; background-color: green;}
.div11{
position: relative;
}
.div22{
position: absolute;top:50%;left: 50%;margin-top: -20px;margin-left: -20px;
}
</style>
<div class="div1 div11">
<div class="div2 div22">
</div>
</div>
</body>
</html>

第二種
第三種
還是用css的position屬性,如下的html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>demo</title>
</head>
<body>
<style type="text/css">
.div1{ width: 100px; height: 100px; border: 1px solid #000000;}
.div2{ width:40px ; height: 40px; background-color: green;}
.div11{
position: relative;
}
.div22{
position: absolute;margin:auto; top: 0;left: 0;right: 0;bottom: 0;
}
</style>
<div class="div1 div11">
<div class="div2 div22">
</div>
</div>
</body>
</html>

第三種
第四種
利用css3的新增屬性table-cell, vertical-align:middle;
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>demo</title>
</head>
<body>
<style type="text/css">
.div1{ width: 100px; height: 100px; border: 1px solid #000000;}
.div2{ width:40px ; height: 40px; background-color: green;}
.div11{
display: table-cell;vertical-align: middle;
}
.div22{
margin: auto;
}
</style>
<div class="div1 div11">
<div class="div2 div22">
</div>
</div>
</body>
</html>

第四種
第五種方法
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>demo</title>
</head>
<body>
<style type="text/css">
.div1{ width: 100px; height: 100px; border: 1px solid #000000;}
.div2{ width:40px ; background-color: green;}
.div11{
display: table-cell;vertical-align: middle;
}
.div22{
margin: auto;
}
</style>
<div class="div1 div11">
<div class="div2 div22">
div居中方法
</div>
</div>
</body>
</html>

第五種方法
第六種方法
利用flexbox布局
直接在父元素上使用flexbox的布局
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>demo</title>
<style type="text/css">
.div1 {
width: 100px;
height: 100px;
border: 1px solid #000000;
}
.div2 {
height: 40px;
width: 40px;
background-color: green;
}
.div11 {
display: flex;
/*!*flex-direction: column;*!可寫可不寫*/
justify-content: center;
align-items: center;
}
</style>
</head>
<body>
<div class="div1 div11">
<div class="div2 div22">
</div>
</div>
</body>
</html>

第六種方法
第七種方法
利用transform的屬性,注意子絕父相定位
缺點(diǎn):需要支持Html5
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>demo</title>
<style type="text/css">
body {
margin: 100px auto;
position: relative;
}
.div1 {
width: 100px;
height: 100px;
border: 1px solid #000000;
background-color: red;
}
.div2 {
height: 40px;
width: 40px;
background-color: green;
}
.center {
position: absolute;
top: 50%;
left: 50%;
-ms-transform: translate(-50%, -50%);
-moz-transform: translate(-50%, -50%);
-o-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
}
</style>
</head>
<body>
<div class="div1 center">
我是外部盒子
<div class="div2 center">
我要居中
</div>
</div>
</body>
</html>

第七種
第八種
兩者都要固定定位,不常用
缺點(diǎn):需要設(shè)置position屬性,網(wǎng)頁(yè)復(fù)雜時(shí)容易擾亂頁(yè)面布局,而且只是元素的起始位置居中
<html>
<head>
<meta charset="UTF-8">
<title>demo</title>
<style type="text/css">
.div1 {
width: 100px;
height: 100px;
border: 1px solid #000000;
background-color: red;
position: relative;
}
.div2 {
height: 40px;
width: 40px;
background-color: green;
margin:30px 30px;
}
.center{
position: fixed;
left: 50%;
}
</style>
</head>
<body>
<div class="div1 center">
<div class="div2 center">
我要居中
</div>
</div>
</body>
</html>

第八種方法