一、文字
<style>
div {
border: 1px solid red;
width: 500px;
height: 300px;
text-align: center;
line-height: 300px;
}
</style>
<body>
<div>
垂直水平居中
</div>
</body>
-
vertical-align: middle
+text-align:center
方法
只要用过vertical-align: middle
都知道这个方法不一定有用,我也遇到过很多次,也没有去找原因,今天就花了些时间来彻底搞明白它。
vertical-align: middle
是要一个兄弟元素作为参照物,设置这个兄弟元素的高度与父元素一致,然后也让兄弟元素vertical-align: middle
就可以了,但是有一点要注意,vertical-align: middle
只作用于inline
和inline-block
。
<style>
* {
margin: 0;
padding: 0;
}
div {
border: 1px solid red;
width: 500px;
height: 300px;
text-align: center;
}
div span {
height: 100%;
vertical-align: middle;
display: inline-block;
background: red;
}
div p {
display: inline-block;
vertical-align: middle;
}
</style>
<body>
<div>
<span></span>
<p>垂直水平居中</p>
</div>
</body>
<style>
div {
border: 1px solid red;
width: 500px;
height: 300px;
display: flex;
justify-content: center;
align-items: center;
}
</style>
<body>
<div>
垂直水平居中
</div>
</body>
<style>
* {
margin: 0;
padding: 0;
}
div {
height: 300px;
width: 500px;
border: 1px solid red;
display: grid;
grid-template-columns: 500px;
grid-template-rows: 300px;
justify-items: center;
align-items: center;
}
</style>
<body>
<div>
<p>垂直水平居中</p>
</div>
</body>
二、图片
<style>
* {
margin: 0;
padding: 0;
}
div {
height: 300px;
width: 500px;
border: 1px solid red;
display: grid;
grid-template-columns: 500px;
grid-template-rows: 300px;
justify-items: center;
align-items: center;
}
div img{
border: 1px solid blue;
}
</style>
<body>
<div>
<img src="http://img1.imgtn.bdimg.com/it/u=2246428853,1389854504&fm=15&gp=0.jpg" alt="">
</div>
</body>
<style>
* {
margin: 0;
padding: 0;
}
div {
height: 300px;
width: 500px;
border: 1px solid red;
display: flex;
justify-content: center;
align-items: center;
}
div img{
border: 1px solid blue;
}
</style>
<body>
<div>
<img src="http://img1.imgtn.bdimg.com/it/u=2246428853,1389854504&fm=15&gp=0.jpg" alt="">
</div>
</body>
<style>
* {
margin: 0;
padding: 0;
}
div {
height: 300px;
width: 500px;
border: 1px solid red;
text-align: center;
display: table-cell;
vertical-align: middle;
}
div img {
border: 1px solid blue;
}
</style>
<body>
<div>
<img src="http://img1.imgtn.bdimg.com/it/u=2246428853,1389854504&fm=15&gp=0.jpg" alt="">
</div>
</body>
-
position
+transform
方法,此方法适用于不知道图片宽高的情况下
<style>
* {
margin: 0;
padding: 0;
}
div {
height: 300px;
width: 500px;
border: 1px solid red;
position: relative;
}
div img {
border: 1px solid blue;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
<body>
<div>
<img src="http://img1.imgtn.bdimg.com/it/u=2246428853,1389854504&fm=15&gp=0.jpg" alt="">
</div>
</body>
<style>
* {
margin: 0;
padding: 0;
}
div {
height: 300px;
width: 500px;
border: 1px solid red;
position: relative;
}
div img {
width: 100px;
height: 100px;
border: 1px solid blue;
position: absolute;
top: 50%;
left: 50%;
/* 移动宽高的一半 */
margin-top: -50px;
margin-left: -50px;
}
</style>
<body>
<div>
<img src="http://img1.imgtn.bdimg.com/it/u=2246428853,1389854504&fm=15&gp=0.jpg" alt="">
</div>
</body>
-
position
第二种使用方法,不管知不知道宽高都可以使用
<style>
* {
margin: 0;
padding: 0;
}
div {
height: 300px;
width: 500px;
border: 1px solid red;
position: relative;
}
div img {
border: 1px solid blue;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
}
</style>
<body>
<div>
<img src="http://img1.imgtn.bdimg.com/it/u=2246428853,1389854504&fm=15&gp=0.jpg" alt="">
</div>
</body>