div水平垂直居中的几种方式笔记
1、利用定位
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
#f{
width: 400px;
height: 400px;
background-color: aqua;
position: relative;
}
#z{
width: 200px;
height: 200px;
background-color: white;
position: absolute;
top: 50%;
left: 50%;
margin-top: -100px;
margin-left: -100px;
}
</style>
</head>
<body>
<div id="f">
<div id="z"></div>
</div>
</body>
</html>
2、利用margin:auto
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
#f{
width: 400px;
height: 400px;
background-color: aqua;
position: relative;
}
#z{
width: 200px;
height: 200px;
background-color: white;
margin: auto;
position: absolute;
top: 0px;
left: 0px;
right: 0px;
bottom: 0px;
}
</style>
</head>
<body>
<div id="f">
<div id="z"></div>
</div>
</body>
</html>
3、利用display:table-cell
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
#f{
width: 400px;
height: 400px;
background-color: aqua;
display: table-cell;
vertical-align:middle;
text-align: center;
}
#z{
width: 200px;
height: 200px;
background-color: white;
display: inline-block;
}
</style>
</head>
<body>
<div id="f">
<div id="z"></div>
</div>
</body>
</html>
4、flex设置垂直水平都居中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
#f{
width: 400px;
height: 400px;
background-color: aqua;
display: flex;
justify-content: center;
align-items: center;
}
#z{
width: 200px;
height: 200px;
background-color: white;
}
</style>
</head>
<body>
<div id="f">
<div id="z"></div>
</div>
</body>
</html>
5、利用transform
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
#f{
width: 400px;
height: 400px;
background-color: aqua;
position: relative;
}
#z{
width: 200px;
height: 200px;
background-color: white;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50% ,-50%);
}
</style>
</head>
<body>
<div id="f">
<div id="z"></div>
</div>
</body>
</html>
6、利用calc计算
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
#f{
width: 400px;
height: 400px;
background-color: aqua;
position: relative;
}
#z{
width: 200px;
height: 200px;
background-color: white;
position: absolute;
top: calc(50% - 200px / 2);
left: calc(50% - 200px / 2);
}
</style>
</head>
<body>
<div id="f">
<div id="z"></div>
</div>
</body>
</html>
7、效果