如何让一个盒子居中显示
在日常的网页开发中,我们经常需要让一个盒子水平垂直居中显示,以下我列出了几种常用方式
方法一:利用定位(常用方法,推荐)
这种方法的优点是简单直观,主要是利用了定位,元素定位之后,再使用margin样式将元素根据自身的宽高值的一半进行偏移调整,让其实现居中。
这种方法缺点也很明显,我们知道子元素的宽高,如果我们不能确定子元素的宽高,那么这个方法并不是很适合。
<!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>
.box {
position: relative;
width: 400px;
height: 400px;
margin: 0 auto;
background-color: blueviolet;
}
.son {
position: absolute;
top: 50%;
left: 50%;
margin-left: -50px;
margin-top: -50px;
width: 100px;
height: 100px;
background-color: #000;
}
</style>
</head>
<body>
<div class="box">
<div class="son"></div>
</div>
</body>
</html>
方法二:利用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>
.box {
position: relative;
width: 400px;
height: 400px;
margin: 0 auto;
background-color: blueviolet;
}
.son {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
margin: auto;
width: 100px;
height: 100px;
background-color: #000;
}
</style>
</head>
<body>
<div class="box">
<div class="son"></div>
</div>
</body>
</html>
方法三:利用 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>
.box {
position: relative;
width: 400px;
height: 400px;
margin: 0 auto;
background-color: blueviolet;
display: table-cell;
vertical-align: middle;
text-align: center;
}
.son {
width: 100px;
height: 100px;
background-color: #000;
display: inline-block;
}
</style>
</head>
<body>
<div class="box">
<div class="son"></div>
</div>
</body>
</html>
方法四:利用 display: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>
.box {
position: relative;
width: 400px;
height: 400px;
margin: 0 auto;
background-color: blueviolet;
display: flex;
justify-content: center;
align-items: center;
}
.son {
width: 100px;
height: 100px;
background-color: #000;
}
</style>
</head>
<body>
<div class="box">
<div class="son"></div>
</div>
</body>
</html>
方法五:利用transform
利用transform:translate的位移属性来居中
<!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>
.box {
position: relative;
width: 400px;
height: 400px;
margin: 0 auto;
background-color: blueviolet;
}
.son {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 100px;
height: 100px;
background-color: #000;
}
</style>
</head>
<body>
<div class="box">
<div class="son"></div>
</div>
</body>
</html>