给定大小区域,背景图片不变形处理

原始图片:

知识储备

一: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;

相关文章
相关标签/搜索