目录
1. 使用display: flex;进行上下居中布局
在容器里面添加以下几行css语句
display: flex;
justify-content: center;
align-items: center;
注意: 设为Flex布局以后,子元素的float、clear和vertical-align属性将失效。
<!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>
body{
margin: 0;
padding: 0;
height: 9999px; //模仿内容很长的页面
}
.popup__wrapper {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0, 0, 0, 0.5);
z-index: 999;
display: flex;
justify-content: center;
align-items: center;
}
.popup {
box-sizing: border-box;
width: 670px;
height: 300px;
background: #fff;
text-align: center;
padding: 70px 10px 20px 10px;
border-radius: 4px;
}
.popup__title{
font-size: 30px;
font-weight: 600;
color: #004165;
margin-bottom: 20px;
}
.popup__btnWrapper {
width: 50%;
margin: 0 auto;
display: flex;
justify-content: space-between;
}
.popup__text{
font-size: 20px;
margin-bottom: 50px;
}
button{
width: 154px;
cursor: pointer;
border-radius: 4px;
line-height: 45px;
font-weight: 600;
}
.popup__yesBtn {
color: #fff;
background-color: #007dba;
border: 1px solid #006ba1;
}
</style>
</head>
<body>
<div class="popup__wrapper">
<div class="popup">
<div class="popup__title">Confirmation</div>
<div class="popup__text">Are you sure you want to do this action?</div>
<div class="popup__btnWrapper">
<button class="popup__yesBtn">Yes</button>
<button class="popup__noBtn">No</button>
</div>
</div>
</div>
</body>
</html>
2. 使用position:absolute + margin:负值
在需要上下左右居中的元素里添加以下几行css语句:
绝对定位,top和left 为 50%, margin的left和top为自身宽高一半
position: absolute;
width: 670px;
height: 300px;
left: 50%;
margin-left: -335px; // width的一半
top: 50%;
margin-top: -150px; //height的一半
同理也可以用calc来计算
绝对定位,top和left 为 父元素的一半减自身一半
position: absolute;
top: calc(50% - 150px);
left: calc(50% - 335px);
<!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>
body{
margin: 0;
padding: 0;
height: 9999px; //模仿内容很长的页面
}
.popup__wrapper {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0, 0, 0, 0.5);
z-index: 999;
}
.popup {
box-sizing: border-box;
position: absolute;
width: 670px;
height: 300px;
background: #fff;
left: 50%;
margin-left: -335px;
top: 50%;
margin-top: -150px;
text-align: center;
padding: 70px 10px 20px 10px;
border-radius: 4px;
}
.popup__title{
font-size: 30px;
font-weight: 600;
color: #004165;
margin-bottom: 20px;
}
.popup__btnWrapper {
width: 50%;
margin: 0 auto;
display: flex;
justify-content: space-between;
}
.popup__text{
font-size: 20px;
margin-bottom: 50px;
}
button{
width: 154px;
cursor: pointer;
border-radius: 4px;
line-height: 45px;
font-weight: 600;
}
.popup__yesBtn {
color: #fff;
background-color: #007dba;
border: 1px solid #006ba1;
}
</style>
</head>
<body>
<div class="popup__wrapper">
<div class="popup">
<div class="popup__title">Confirmation</div>
<div class="popup__text">Are you sure you want to do this action?</div>
<div class="popup__btnWrapper">
<button class="popup__yesBtn">Yes</button>
<button class="popup__noBtn">No</button>
</div>
</div>
</div>
</body>
</html>
3. 使用position:absolute + transform: translate(-50%, -50%);
在需要上下左右居中的元素里添加以下几行css语句:
同方法二的原理一样,在absolute以后,往x, y轴方向移动width, height的一半
position: absolute;
width: 670px;
height: 300px;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
<!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>
body{
margin: 0;
padding: 0;
height: 9999px; //模仿内容很长的页面
}
.popup__wrapper {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0, 0, 0, 0.5);
z-index: 999;
}
.popup {
box-sizing: border-box;
position: absolute;
width: 670px;
height: 300px;
background: #fff;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
text-align: center;
padding: 70px 10px 20px 10px;
border-radius: 4px;
}
.popup__title{
font-size: 30px;
font-weight: 600;
color: #004165;
margin-bottom: 20px;
}
.popup__btnWrapper {
width: 50%;
margin: 0 auto;
display: flex;
justify-content: space-between;
}
.popup__text{
font-size: 20px;
margin-bottom: 50px;
}
button{
width: 154px;
cursor: pointer;
border-radius: 4px;
line-height: 45px;
font-weight: 600;
}
.popup__yesBtn {
color: #fff;
background-color: #007dba;
border: 1px solid #006ba1;
}
</style>
</head>
<body>
<div class="popup__wrapper">
<div class="popup">
<div class="popup__title">Confirmation</div>
<div class="popup__text">Are you sure you want to do this action?</div>
<div class="popup__btnWrapper">
<button class="popup__yesBtn">Yes</button>
<button class="popup__noBtn">No</button>
</div>
</div>
</div>
</body>
</html>
同样以上三种方法也适用于你想把一个元素始终上下左右居中的情况。举一反三嘛。个人推荐方法一, 三。
以上的代码都可以创建一个html文件,直接查看效果。
今天在审核同事代码的时候他在实现弹出窗口,成片的position:absolute; fix。很混乱。所以我想把我平时用的实现方法写出来。希望有一天能帮助到需要的人。
如果有任何错误或者还有可以改进的地方,欢迎评论指导。在此感谢。