Right align an image with max-width and max-height

2.7k Views Asked by At

How do I right align an image which has max-width and max-height given? Please see the styles below. The height and width are dynamically set. When the width is adjusted , the image moves to left side. How to prevent this and keep the image on the right edge while the width gets changed?

    <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

There are 1 best solutions below

3
On

Just use flex and margin-left: auto to align the image right, and do not specify the img dimensions in the img tag.

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