img定位在div容器内

时间:2018-12-27 11:03:03

标签: html css image

无法在“ head-logo” div中获得 img ,以将两个垂直居中位置和水平居中位置。

我尝试了CSS解决方案,例如:

1)

.head-logo img  {
    margin-left: auto;
    margin-right: auto;
    display: block;
}

2)

.head-logo img {
vertical-align: middle;
}

3)

    .head-logo img  { 
    display: -webkit-flex;
    display: -ms-flex; 
    display: flex; 
    justify-content: center; 
    -ms-align-items: center; 
    align-items: center; 
    }

<div class="head-container">
    <div class="head-logo">
        <img src="img\logo.svg" height="70" width="70">
    </div>
    <div class="head-menu">

    </div>
</div>
            div {
                border: dotted 1px;
            }
            .head-container {
                width: 80%;
                height: 100px;
                margin: auto;
                padding: 5px;
                background-color: aqua;
            }
            .head-logo {
                vertical-align: middle;
                width: auto;
                height: 100px;
                background-color: red;
                float: left;
            }
            .head-logo img {

            }

当前结果是 img 停留在父div的左上角,我需要将其从所有方向居中放置。

可能我缺少css的新功能,期待您的帮助。

           div {
                border: dotted 1px;
            }
            .head-container {
                width: 80%;
                height: 100px;
                margin: auto;
                padding: 5px;
                background-color: aqua;
            }
            .head-logo {
                vertical-align: middle;
                width: auto;
                height: 100px;
                background-color: red;
                float: left;
            }
            .head-logo img {

            }
<div class="head-container">
    <div class="head-logo">
        <img src="https://via.placeholder.com/150x200/O%20https://placeholder.com/" height="70" width="70">
    </div>
    <div class="head-menu">

    </div>
</div>
 

4 个答案:

答案 0 :(得分:0)

您可以尝试对象匹配-例如:

.head-logo img {
    object-fit: contain;
    height: 100%;
}

对象适合的演示(包括用于ie的图像填充)https://codepen.io/jakob-e/pen/Pxwzpw

/* object fit polyfill */
//  is object fit supported
if (document.body.style.objectFit === undefined) {
    //  loop through all images    
    [].slice
        .call(document.querySelectorAll('img'))
        .map(function (img) {
        //  image has background-size cover or contain        
        if (['cover', 'contain']
            .indexOf(getComputedStyle(img).backgroundSize) !== -1) {
            //  use src as background image            
            img.style.backgroundImage = "url(" + img.src + ")";
            //  replace src with transparent gif
            img.src = 'data:;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7';
        }
    });
}
/* add this */
.head-logo img {
   height: 100%;
   object-fit: contain;
   background-size: contain;     /* used by polyfill */
   background-repeat: no-repeat; /* used by polyfill */
   
}



div {
    border: dotted 1px;
}
.head-container {
    width: 80%;
    height: 100px;
    margin: auto;
    padding: 5px;
    background-color: aqua;
}
.head-logo {
    vertical-align: middle;
    width: auto;
    height: 100px;
    background-color: red;
    float: left;
}
<div class="head-container">
    <div class="head-logo">
        <img src="https://i.pinimg.com/originals/33/b8/69/33b869f90619e81763dbf1fccc896d8d.jpg" height="70" width="70">
    </div>
    <div class="head-menu">

    </div>
</div>
            

答案 1 :(得分:0)

div{
  height: 300px;
  width: 300px;
  padding: 10%;
  background: red;
  box-sizing: border-box;
}

div img{
  
  height: 100%;
  width: 100%; 
}
<div>
  <img src="https://media.gettyimages.com/photos/trent-boult-of-new-zealand-bowls-during-day-two-of-the-second-test-picture-id1087035928" alt="">
</div>

答案 2 :(得分:0)

尝试以下操作:(忽略长图像链接:p)

div {
  border: dotted 1px;
}
.head-container {
  width: 80%;
  height: 100px;
  margin: auto;
  padding: 5px;
  background-color: aqua;
}
.head-logo {
  width: 150px;
  height: 100px;
  background-color: red;
  float: left;
  display: flex;
  justify-content: center;
}
.head-logo img {
  display: block;
  margin: auto;
  vertical-align: middle;
}
<div class="head-container">
    <div class="head-logo">
        <img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxMTEhUTExMWFhUWGBgaGBgYFxcXGhcaFRgWFxYYGhcYHSggGBolHRcYITEhJSkrLi4uFx8zODMtNygtLisBCgoKDg0OGxAQGy0lICUtLS0tLS8vLS0tLS0tLy0tLS8tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLS0tLf/AABEIAKgBLAMBIgACEQEDEQH/xAAcAAACAgMBAQAAAAAAAAAAAAAEBQMGAAIHAQj/xAA7EAABAwMDAgUCAwc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x3APIqZRtQYAXuHUcTkEczRVhcpkAJJHWBnMd6sXijTkEbkxk9BNV9m2WEymfxRkRHeT8xThkDrBDWBGuusbm3DEkBHHMAml1hpaFp3KGZ7jsP61Yb+6aS2UwFEkEEE59vg5oMMrPx0wOOlKVyFqGrxy8rBE81C2wa9rKjB1IhNWbaCfvnOfrWydPChkY6AY/XrXtZXiTMJh1nbQNvT9OAPvApmpICY/z2NZWVhmAxY88Aoxx/k1u26lcScj9R2rysoSIJElvrxBGECZyR9qW27hQNogBRVIA5EjJ98YrKyvKKngajNi4wACRAxGT8zijWEpGZk/zPU1lZRWYxXIh9vdbDvQ5tMciJjsJ4rd3WZxtgTuiIg8YnocfrXtZRLkYaBlCeVkVSFNX3BLbXykny0gT+Ic5kwdx7j+VBX7y3F7du0HhU/H1H9a9rK31GGoAc1XxPLIJ3LH4gFGPfpOKs1jcAncPb9Kysr1XOp4x6k92wCJ60LcIgATWVlJfqNy7m11a728n0jMd4IxSx+SIkAdE9vtWVlbgOpKchU0JFpjwCiFnAOP8Ag0bfFboIkhI4jj5xWVldBzS3GZGpbgFrpydplcn4rRTygIMx3rKyosrkkTm53OiI10bUg2SRwecfejtU1FDowAZ/SsrK0ZWOpTj8rIQFO4Oy6iOBVe8R6eHxAMc+1ZWU1shqdHNnPpHQle/8CC2ESSEgDPXndPseI961YB9TZEiUz79yfevaylLkY3c4mPIxsSC0sIDi4MFwr+iwEkH9aLtdP9AIACwuSY5wBHxisrKMuY0MQajpy1SIVtEgfQH2FBN2aQkgZCp7cn+lZWUiz3J3YhtQFOmAmSBj27VE8xnAxXtZRWT3BBJn/9k=" height="70" width="120">
    </div>
    <div class="head-menu">

    </div>
</div>

答案 3 :(得分:0)

几乎没有办法。我首选的是

empty
            div {
                border: dotted 1px;
            }
            .head-container {
                width: 80%;
                height: 100px;
                margin: auto;
                padding: 5px;
                background-color: aqua;
            }
            .head-logo {

                width: 100px;
                height: 100px;
                background-color: red;
                float: left;
                
                 overflow: hidden;
                
                position: relative;
           
            }
            .head-logo img {
              position: absolute;
              width: 100%;
              height: auto;
              
              top: -50%;
              right: -50%;
              bottom: -50%;
              left: -50%;
              margin: auto;
            
            }

此解决方案的好处是,它将始终位于父容器的中心,在旧版浏览器中效果很好,如果您希望徽标保持相同大小或将其设置为,则可以将img宽度设置为“ 100px”等固定值缩放到父容器

<div class="head-container"> <div class="head-logo"> <img src="https://placeholder.com/wp-content/uploads/2018/10/placeholder.com-logo1.png" height="70" width="70"> </div> <div class="head-menu"> </div> </div>在父容器中,请确保较大的图片不会从容器中溢出