<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style type="text/css">
.max-div {
position: relative;
/* 高度按照自己的需求设定 */
width: 200px;
height: 200px;
}
.div-img {
position: absolute;
/* 图片大小 */
max-width: 100%;
max-height: 100%;
/* 以下三个属性处理图片居中问题 */
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
</style>
</head>
<body>
<div class="root">
<div class="max-div">
<img class="div-img"
src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2Ftp10%2F210926112Z2F31-0-lp.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1648794314&t=81b0f4a1e2c50bc4f740c62bf8e1e485"
alt="">
</div>
</div>
</body>
</html>
以下是示例