右对齐图像的最大宽度和最大高度

时间:2018-09-17 08:31:19

标签: jquery html css right-align

如何正确对齐具有最大宽度和最大高度的图像?请查看下面的样式。 高度和宽度是动态设置的。调整宽度后,图像移动到左侧。如何防止这种情况,并在更改宽度时将图像保持在右侧边缘?

    <div class="img-label-wrapper">
      <img src="../assets/portal/css/x-logo.png" class="titleResource" width="60px" height="20px">                    
    </div>

    .img-label-wrapper {
       position: relative;
       top: -5px;
       text-align: right;
       width: 100%;
    }    
    .img-label-wrapper .titleResource {
       max-width: 100px !important;
       max-height: 20px !important;
       float: right;
       margin-top: unset !important;
    }

1 个答案:

答案 0 :(得分:2)

只需使用flexmargin-left: auto来使图像右对齐,并且不要在img标签中指定img尺寸。

.img-label-wrapper {
  display: flex;
}

.titleResource {
    max-width: 100px;
    max-height: 20px;
    margin-left: auto;
}
<div class="img-label-wrapper">
  <img class="titleResource" src="https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.svg?v=a010291124bf">                   
</div>

相关问题