ในการกำหนดขนาดของภาพพื้นหลังใน JavaScript ให้ใช้ backgroundSize คุณสมบัติ. ช่วยให้คุณกำหนดขนาดรูปภาพสำหรับพื้นหลังได้
ตัวอย่าง
คุณสามารถลองเรียกใช้โค้ดต่อไปนี้เพื่อเรียนรู้วิธีกำหนดขนาดของภาพพื้นหลัง:
<!DOCTYPE html>
<html>
<head>
<style>
#box {
border: 2px dashed blue;
width: 600px;
height: 400px;
background: url('https://www.tutorialspoint.com/videotutorials/images/coding_ground_home.jpg') no-repeat;
}
</style>
</head>
<body>
<button onclick="display()">Set size of background image</button>
<div id="box">
</div>
<script>
function display() {
document.getElementById("box").style.backgroundSize = "150px 200px";
}
</script>
</body>
</html>