1 max-width
<!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>图片自适应-1</title>
<style>
.img {
max-width: 200px;
}
img {
display: inline-block;
max-width: 100%;
height: auto;
}
</style>
</head>
<body>
<div class="img">
<img src="./cat.jpg" alt="" srcset="">
</div>
</body>
</html>
2 background-image
<!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>图片自适应-2</title>
<style>
html {
height: 100%;
background-color: red;
}
body {
width:100%;
height: 100%;
background: url('./bg.jpg') no-repeat;
background-size: 100%;
background-attachment:fixed;
background-position: center 180px;
margin: 0px;
}
.header {
height: 60px;
background-color: green;
}
.container {
padding-top: 100px;
}
.content {
color: royalblue;
text-align: center;
height: 60px;
}
</style>
</head>
<body>
<div class="header">header</div>
<div class="container">
<div class="content">
我是内容1
</div>
<div class="content">
我是内容2
</div>
<div class="content">
我是内容3
</div>
</div>
</body>
</html>