如何在div中水平和垂直放置图像

这个问题在这里已经有了答案:

  • Flexbox:水平和垂直居中8个答案

  • 你在这:

    .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>

    你尝试过使用柔性盒吗?

    编辑:根据评论,不,我不知道使用“对齐项目”,效果很好,我已经编辑相应的片段! :)

    .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/76015.html

    上一篇: How to center a image in a div horizontally and vertically

    下一篇: How to get this div to align center?