98 lines
2.2 KiB
HTML
98 lines
2.2 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>常见垂直居中方案</title>
|
|
<style>
|
|
body,p{
|
|
margin: 0;
|
|
padding: 0;
|
|
}
|
|
.box {
|
|
height: 45px;
|
|
line-height: 45px;
|
|
background-color: #000;
|
|
text-align: center;
|
|
}
|
|
.box p {
|
|
color: #fff;
|
|
}
|
|
|
|
.box_2 {
|
|
height: 100px;
|
|
background-color: red;
|
|
|
|
/* 1
|
|
display: flex;
|
|
align-items: center;
|
|
*/
|
|
|
|
/* 2
|
|
position: relative;
|
|
top:50%;
|
|
transform:translateY(-50%);
|
|
*/
|
|
|
|
/* 3
|
|
父容器
|
|
position: relative;
|
|
|
|
子元素
|
|
position: absolute;
|
|
top: 50%;
|
|
margin-top: -35px;
|
|
*/
|
|
|
|
/* 2
|
|
父元素
|
|
padding-top: xxxpx
|
|
*/
|
|
|
|
|
|
|
|
}
|
|
.box_2_1 {
|
|
width: 1000px;
|
|
height: 70px;
|
|
background-color: blue;
|
|
margin: 0 auto;
|
|
}
|
|
|
|
.box_3 img{
|
|
width: 48px;
|
|
height: 48px;
|
|
border-radius: 100%;
|
|
vertical-align: middle;
|
|
}
|
|
|
|
</style>
|
|
</head>
|
|
<body>
|
|
|
|
|
|
<!--div内部的垂直水平居中-->
|
|
|
|
<div class="box">
|
|
<p>div内部的垂直水平居中</p>
|
|
</div>
|
|
|
|
<div class="box_2">
|
|
<div class="box_2_1">内部div需要在外部div中垂直水平居</div>
|
|
</div>
|
|
|
|
<!-- <div class="box_3">
|
|
<img src="https://upload.jianshu.io/users/upload_avatars/5957/6d510770-b55c-465f-80bb-10aab2714cfa.jpg?imageMogr2/auto-orient/strip|imageView2/1/w/96/h/96/format/webp" alt="">
|
|
<p>Bmy</p>
|
|
</div> -->
|
|
|
|
<div class="box_3">
|
|
<img src="https://upload.jianshu.io/users/upload_avatars/5957/6d510770-b55c-465f-80bb-10aab2714cfa.jpg?imageMogr2/auto-orient/strip|imageView2/1/w/96/h/96/format/webp" alt="">
|
|
<div>
|
|
<p>Bmy</p>
|
|
<p>Bmy</p>
|
|
</div>
|
|
</div>
|
|
|
|
</body>
|
|
</html> |