css中绝对居中

方法1,

left: 0;
top: 0;
right: 0;
bottom: 0;
margin: auto;
方法2,
calc(50% - 100px) -----------(100px为中间方块一半的宽度。)
 
方法3,
 
margin-left:-100px;(100px为中间方块一半的宽度。)
margin-top:-100px;(100px为中间方块一半的高度。)
方法4,
css3 ‘translate’
     transform:(-50%,-50%)
方法5,
  给父级元素增加:
display: flex;
justify-content: center;
align-items: center;
下面是例子:
<!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>
html, body {
height: 100%;
}
.box {
height: 100%;
display: flex;
justify-content: center;
align-items: center;
}
.inner.one {
background: lightgreen;
width: 100px;
height: 100px;
}
.inner.two {
background: gold;
width: 100px;
height: 100px;
}
</style>
</head>
<body>
<div class="box">
<div class="inner one">1</div>
</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>
html, body {
margin: 0;
padding: 0;
}
html {
height: 100%;
}
body {
height: 100%;
}
div {
width: 30%;
height: 30%;
background: pink;
position: absolute;
left: 0;
top: 0;
right: 0;
bottom: 0;
margin: auto;
}
</style>
</head>
<body>
<div></div>
</body>
</html>

..

.

.

.

.

 

猜你喜欢

转载自www.cnblogs.com/yxybky/p/10089637.html