Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

image

现在的代码是

    width: auto;
    height: auto;
    max-width: 100%;
    max-height: 100%;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

图片大的话,它会自动缩小,但是如果图片小于盒子大小,就不会进行放大了(如图),怎么办。放大保证不失真,任何一边触碰到父盒边缘就不放大了。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
105 views
Welcome To Ask or Share your Answers For Others

1 Answer

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    #container {
      margin: auto;
      width: 300px;
      height: 400px;
    }
    #image {
      max-width: 100%;
      max-height: 100%;
      object-fit: contain;
    }
  </style>
</head>
<body>
  <div id="container">
    <img id="image" src="https://tse2-mm.cn.bing.net/th/id/OIP.6q0DYSIPBOoMYtN8ee8G4QHaE5?w=266&h=180&c=7&o=5&dpr=2&pid=1.7">
  </div>
</body>
</html>

是你要的效果不


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...