原始圖片:
知識儲備
一:background-size:cover
把背景圖像擴展至足夠大,以使背景圖像徹底覆蓋背景區域。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<style>
.img-box {
width: 300px;
height: 200px;
background-image: url(./demo.jpg);
background-position: center center;
background-repeat: no-repeat;
background-size: cover;
border: 1px solid red;
}
</style>
<body>
<div class="img-box">
</div>
</body>
</html>
二:background-size:contain
把圖像擴展至最大尺寸,以使寬度和高度 徹底適應內容區域。
background-size: cover; 修改成: background-size: contain;