<!DOCTYPE html>
<html>
<head>
<title>Vue 设置背景图片宽度自适应</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
<style>
.bg-image {
width: 100%; /* 宽度自适应 */
height: auto; /* 高度根据宽度自动调整 */
background-size: cover; /* 背景图片覆盖整个元素 */
background-position: center; /* 背景图片居中 */
}
</style>
</head>
<body>
<div id="app">
<div class="bg-image" :style="{ backgroundImage: 'url(' + imageUrl + ')' }"></div>
</div>
<script>
new Vue({
el: '#app',
data: {
imageUrl: 'https://example.com/your-image.jpg' // 替换为你的图片URL
}
});
</script>
</body>
</html>
div 元素,其类名为 bg-image,并通过 Vue 的 :style 绑定动态设置背景图片。width: 100%:使背景图片的宽度自适应父容器的宽度。height: auto:保持图片的宽高比,高度会根据宽度自动调整。background-size: cover:确保背景图片覆盖整个元素,同时保持图片比例。background-position: center:使背景图片居中显示。imageUrl 数据属性到 bg-image 类的背景图片 URL。你可以将 imageUrl 替换为你自己的图片路径。上一篇:vue key
下一篇:ant design vue 4
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站