我有一个按钮与图像作为背景,我想调整大小的图像大小取决于屏幕分辨率。对于图像,我使用宽度:100%,但在这种情况下,如果我将它用于按钮,按钮大小调整到屏幕分辨率,但不是它用作背景的图像。我可以做什么,使图像调整大小与按钮相同的方式?
<!DOCTYPE html>
<html>
<body>
<button class="dog"><img src="pictures/dog.png" ></button>
<style>
.dog {
position:absolute;
width:100%;
left:1%;
top:6%;
}
</style>
</body>
</html>
您可以删除元素中的图像,并将该图像设置为按钮的背景。使用CSS背景属性,如
background-size
,使背景自动适合您的按钮。
null
.bill {
width: 180px;
height: 80px;
background: url("https://www.fillmurray.com/640/360");
background-position: center;
background-size: cover;
color: floralwhite;
font-weight: bold;
font-size: 1.4em;
}
<button class="bill">Bill Me!</button>