Open roxy0724 opened 5 years ago
水平垂直居中有多种方案实现。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0
}
html,body{
width: 100%;
height: 100%;
}
body {
position: relative;
}
.box {
width: 200px;
height: 200px;
background-color: aquamarine;
position: absolute;
left: 50%;
top: 50%;
margin-top: -100px;
margin-left: -100px;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0
}
html,body{
width: 100%;
height: 100%;
}
body {
position: relative;
}
.box {
width: 200px;
height: 200px;
background-color: aquamarine;
position: absolute;
margin: auto;
top: 0; left: 0; bottom: 0; right: 0;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0
}
html,body{
width: 100%;
height: 100%;
}
body {
display: flex;
justify-content: center;
align-items: center;
}
.box {
width: 200px;
height: 200px;
background-color: rgb(11, 77, 55);
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0
}
html,body{
width: 100%;
height: 100%;
}
.box {
width: 200px;
height: 200px;
background-color: aquamarine;
position: relative;
left: 50%;
top: 50%;
transform: translate(-50%,-50%)
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
<style>
.box{
width: 200px;
height: 200px;
border: 1px solid red;
display: flex;
align-items: center;
justify-content: center;
}
.innerBox{
width: 50px;
height: 50px;
background-color: brown;
}
</style>
<body>
<div class="box">
<div class="innerBox">
</div>
</div>
</body>
<style>
.box{
width: 200px;
height: 200px;
border: 1px solid red;
position: relative;
}
.innerBox{
width: 50px;
height: 50px;
background-color: brown;
position: absolute;
top: 50%;
left: 50%;
margin: -25px 0 0 -25px;
}
</style>
<body>
<div class="box">
<div class="innerBox"></div>
</div>
</body>
<style>
.box{
width: 200px;
height: 200px;
border: 1px solid red;
position: relative;
}
.innerBox{
width: 50px;
height: 50px;
background-color: brown;
position:absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%)
}
</style>
<body>
<div class="box">
<div class="innerBox"></div>
</div>
</body>
<style>
.content{
width: 100px;
height: 100px;
background: #ccc;
}
.box {
height: 500px;
display: flex;
align-items: center;
justify-content: center;
}
</style>
<body>
<div class="box">
<div class="content"></div>
</div>
</body>
2.绝对定位、负边距
<style>
.box {
position: relative;
height: 500px;
}
.content {
position: absolute;
width: 100px;
height: 100px;
background: #cccccc;
top: 50%;
left: 50%;
margin: -50px 0 0 -50px;
}
</style>
3.table-cell(仅适用于inline或inline-block元素)
<style>
.box {
display: table-cell;
text-align: center;
vertical-align: middle;
height: 500px;
width: 500px;
}
</style>
4.transform
<style>
.content{
position: relative;
height: 500px;
}
.box {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
</style>
尽可能写多的实现方案