1、不知道inner自身盒子宽高
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>水平垂直居中</title>
<style>
* {
margin: 0;
padding: 0;
}
.outer {
position: relative;
width: 400px;
height: 400px;
background-color: blue;
}
.inner {
width: 100px;
height: 100px;
position: absolute;
left: 50%;
top: 50%;
background: red;
transform: translate(-50%, -50%);
}
</style>
</head>
<body>
<div class="outer">
<div class="inner">
</div>
</div>
</body>
<script>
let ele = document.querySelector('.inner');
let dis = window.getComputedStyle(ele,null).display;
console.log(dis);
</script>
</html>
2、利用margin负值 (知道自身的盒子宽高 )
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>水平垂直居中</title>
<style>
* {
margin: 0;
padding: 0;
}
.outer {
position: relative;
width: 400px;
height: 400px;
background-color: blue;
}
.inner {
position: absolute;
left: 50%;
top: 50%;
width: 100px;
height: 100px;
background: red;
margin-top: -50px;
margin-left: -50px;
}
</style>
</head>
<body>
<div class="outer">
<div class="inner">
</div>
</div>
</body>
<script>
let ele = document.querySelector('.inner');
let dis = window.getComputedStyle(ele,null).display;
console.log(dis);
</script>
</html>
3、利用元素在垂直方向具有自动填充特性 margin: auto
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>水平垂直居中</title>
<style>
* {
margin: 0;
padding: 0;
}
.outer {
position: relative;
width: 400px;
height: 400px;
background-color: blue;
}
.inner {
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
width: 100px;
height: 100px;
margin: auto;
background-color: red;
}
</style>
</head>
<body>
<div class="outer">
<div class="inner">
</div>
</div>
</body>
<script>
let ele = document.querySelector('.inner');
let dis = window.getComputedStyle(ele,null).display;
console.log(dis);
</script>
</html>
4、通过设置父元素为table-cell
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>水平垂直居中</title>
<style>
* {
margin: 0;
padding: 0;
}
.outer {
width: 400px;
height: 400px;
background-color: blue;
display: table-cell;
text-align: center;
vertical-align: middle;
}
.inner {
width: 100px;
height: 100px;
background-color: red;
display: inline-block;
}
</style>
</head>
<body>
<div class="outer">
<div class="inner">
</div>
</div>
</body>
<script>
let ele = document.querySelector('.inner');
let dis = window.getComputedStyle(ele,null).display;
console.log(dis);
</script>
</html>
5、line-height
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>水平垂直居中</title>
<style>
* {
margin: 0;
padding: 0;
}
.outer {
width: 400px;
height: 400px;
line-height: 400px;
background-color: blue;
text-align: center;
}
.inner {
width: 100px;
height: 100px;
background-color: red;
display: inline-block;
vertical-align: middle;
}
</style>
</head>
<body>
<div class="outer">
<div class="inner">
</div>
</div>
</body>
<script>
let ele = document.querySelector('.inner');
let dis = window.getComputedStyle(ele,null).display;
console.log(dis);
</script>
</html>
6、flex布局
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>水平垂直居中</title>
<style>
* {
margin: 0;
padding: 0;
}
.outer {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
justify-content: center;
align-items: center;
width: 400px;
height: 400px;
background-color: blue;
}
.inner {
flex: none;
width: 100px;
height: 100px;
background-color: red;
}
</style>
</head>
<body>
<div class="outer">
<div class="inner">
</div>
</div>
</body>
<script>
let ele = document.querySelector('.inner');
let dis = window.getComputedStyle(ele,null).display;
console.log(dis);
</script>
</html>