简体   繁体   中英

how to fit image without stretching and maintain aspect ratio in div tag in html

My image size is 640*820.And I want to fit this image into screen of size 310*520. While doing this maintain aspect ratio(without stretch).

<div class="abc" style="
    background: url("images/phone.png");
    position: absolute;
    max-width: 100%;
    width: auto;
    margin-left:10px;
    background-size: contain;
    top: 50px;">

You could just state the width of the image and have the height auto

.div img {
   width: 310px;
   height: auto;
}
<div class="div">
  <img src="#" title="#">
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM