How to center a image in a div horizontally and vertically
This question already has an answer here:
你在这:
.video{
width: 100%;
height: 500px;
background-color: peru;
position: absolute;
}
.moldura{
display: block;
width: 50px;
border: 1px solid black;
/* Here is the important code*/
position: relative;
top: 50%;
left: 50%;
transform:translate(-50%, -50%);
}
.moldura img{
width: 50px;
}
<!DOCTYPE html>
<html>
<head>
<title>TESTE</title>
<link rel="stylesheet" type="text/css" href="estilo.css">
</head>
<body>
<div class="video">
<div class="moldura">
<img src="https://lh4.ggpht.com/SKQHLsT8xsNpXeL5si4bBqSNqdy8Qbvzk15J3qWTp55AnnkbNO6-vBJhIBTQxyq16YE=w300">
</div>
</div>
</body>
</html>
Have you tried using flex boxes?
EDIT: As per comment, no i didn't know about using "align-items", works well, I've edited the snippet accordingly! :)
.video{
display:flex;
justify-content: center;
align-items: center;
flex-direction:column;
height:250px;
background-color: peru;
}
.moldura{
}
.moldura img{
height:50px;
border:1px solid black;
}
<!DOCTYPE html>
<html>
<head>
<title>TESTE</title>
<link rel="stylesheet" type="text/css" href="estilo.css">
</head>
<body>
<div class="video">
<div class="moldura">
<img src="https://lh4.ggpht.com/SKQHLsT8xsNpXeL5si4bBqSNqdy8Qbvzk15J3qWTp55AnnkbNO6-vBJhIBTQxyq16YE=w300">
</div>
</div>
</body>
</html>
添加了行高css属性。
.video{
width: 100%;
height: 500px;
background-color: peru;
position: absolute;
}
.moldura{
display: block;
width: 50px;
margin:0 auto;
line-height:500px;
height:100%;
}
.moldura img{
width: 50px;
vertical-align:middle;
}
<!DOCTYPE html>
<html>
<head>
<title>TESTE</title>
<link rel="stylesheet" type="text/css" href="estilo.css">
</head>
<body>
<div class="video">
<div class="moldura">
<img src="https://lh4.ggpht.com/SKQHLsT8xsNpXeL5si4bBqSNqdy8Qbvzk15J3qWTp55AnnkbNO6-vBJhIBTQxyq16YE=w300">
</div>
</div>
</body>
</html>
链接地址: http://www.djcxy.com/p/76016.html
上一篇: 使用FlexBox时垂直对齐输入
下一篇: 如何在div中水平和垂直放置图像