第一種
絕對定位方法:不確定當前div的寬度和高度,採用 transform: translate(-50%,-50%); 當前div的父級添加相對定位(position: relative;)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>水平垂直居中方式1</title>
<style>
/*使用絕對定位,不確定子div的寬高,父元素需要添加position:relative*/
.father {
background: red;
position: relative;
width: 500px;
height: 500px;
}
.son {
background: green;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
}
</style>
</head>
<body>
<div class="father">
<div class="son">hello simon</div>
</div>
</body>
</html>
第二種
絕對定位方法:確定了當前div的寬度,margin值爲當前div寬度一半的負值
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>水平垂直居中方式2</title>
<style>
/*使用絕對定位,確定了子div的寬高,margin值爲子div寬高的一半的負值*/
.father {
background: red;
position: relative;
width: 500px;
height: 500px;
}
.son {
width: 200px;
height: 200px;
background: green;
position: absolute;
left: 50%;
top: 50%;
margin-left: -100px;
margin-top: -100px;
}
</style>
</head>
<body>
<div class="father">
<div class="son">hello simon</div>
</div>
</body>
</html>
第三種
絕對定位方法:絕對定位下top left right bottom 都設置0 ,margin設置爲auto
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>水平垂直居中方式3</title>
<style>
/*使用絕對定位,確定了子div的寬高,left、right、top、bottom都設置爲0,margin設置爲auto*/
.father {
background: red;
position: relative;
width: 500px;
height: 500px;
}
.son {
width: 200px;
height: 200px;
background: green;
position: absolute;
left: 0;
top: 0;
bottom: 0;
right: 0;
margin: auto;
}
</style>
</head>
<body>
<div class="father">
<div class="son">hello simon</div>
</div>
</body>
</html>
第四種
flex佈局方法:當前div的父級添加flex css樣式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>水平垂直居中方式4</title>
<style>
/*使用flex佈局,爲父div添加flex佈局樣式align-items和justify-content*/
.father {
background: red;
position: relative;
width: 500px;
height: 500px;
-webkit-display: flex;
display: flex;
-webkit-align-items: center;
align-items: center;
-webkit-justify-content: center;
justify-content: center;
}
.son {
width: 200px;
height: 200px;
background: green;
}
</style>
</head>
<body>
<div class="father">
<div class="son">hello simon</div>
</div>
</body>
</html>
第五種
table-cell實現水平垂直居中: table-cell middle center組合使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>水平垂直居中方式5</title>
<style>
/*table-cell、vertical-align、text-align組合使用*/
/*子div只能不能是塊狀元素,否則只能垂直居中,無法水平居中*/
.father {
width: 500px;
height: 500px;
display: table-cell;
vertical-align: middle;
text-align: center;
background-color: red;
}
.son {
width: 200px;
height: 200px;
background-color: green;
display: inline-block;
}
</style>
</head>
<body>
<div class="father">
<div class="son">hello simon</div>
</div>
</body>
</html>
第六種
絕對定位:calc() 函數動態計算實現水平垂直居中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>水平垂直居中方式6</title>
<style>
.father {
position: relative;
border: 1px solid #f40;
background-color: #f30;
width: 80vw;
height: 80vh;
}
.son {
position: absolute;
width: 30vw;
height: 30vh;
background-color: #ff0;
left: -moz-calc((80vw - 30vw)/2);
top: -moz-calc((80vh - 30vh)/2);
left: -webkit-calc((80vw - 30vw)/2);
top: -webkit-calc((80vh - 30vh)/2);
left: calc((80vw - 30vw)/2);
top: calc((80vh - 30vh)/2);
}
</style>
</head>
<body>
<!--絕對定位+calc()動態計算實現水平垂直居中-->
<div class="father">
<div class="son">
hello simon
</div>
</div>
</body>
</html>