css-背景图片全屏显示适配不同尺寸覆盖

1.实现

实现背景图片全屏铺满并自适应不同屏幕尺寸

html 复制代码
<!DOCTYPE html>  
<html lang="en">  
<head>  
    <meta charset="UTF-8">  
    <meta name="viewport" content="width=device-width, initial-scale=1.0">  
    <title>全屏背景图片</title>  
    <style>  
        body, html {  
            margin: 0;  
            padding: 0;  
            height: 100%;  
            width: 100%;  
        }  
  
        .background {  
            /* 使用背景图片 */  
            background-image: url('your-image-url.jpg');  
            /* 背景图片覆盖整个元素 */  
            background-size: cover;  
            /* 背景图片保持其比例 */  
            background-repeat: no-repeat;  
            /* 背景图片固定在容器的中心 */  
            background-position: center;  
            /* 使容器占满整个视口 */  
            height: 100vh;  
            width: 100vw;  
        }  
    </style>  
</head>  
<body>  
    <div class="background"></div>  
</body>  
</html>

2.background-attachment

设置背景图像是否固定或者随着页面的其余部分滚动。

css 复制代码
/* 默认的背景图像滚动效果 */  
body {  
    background-image: url('background.jpg');  
    background-attachment: scroll;  
}  
  
/* 固定背景图像 */  
body {  
    background-image: url('background.jpg');  
    background-attachment: fixed;  
}  
  
/* 背景图像在元素内部滚动 */  
.scroll-container {  
    background-image: url('background.jpg');  
    background-attachment: local;  
    overflow: scroll;  
    height: 300px;  
}
相关推荐
Mike_jia几秒前
UptimeRobot API状态监控:零成本打造企业级业务健康看板
前端
江城开朗的豌豆1 分钟前
React状态更新踩坑记:我是这样优雅修改参数的
前端·javascript·react.js
CodeSheep19 分钟前
Stack Overflow,轰然倒下了!
前端·后端·程序员
i紸定i19 分钟前
解决html-to-image在 ios 上dom里面的图片不显示出来
前端·ios·vue·html·html-to-image
ai.Neo21 分钟前
(第十七期)HTML图像标签详解:从入门到精通
前端·html
excel24 分钟前
JS 函数终极指南:this、闭包、递归、尾调用、柯里化,一次性吃透
前端
夏天想26 分钟前
html模拟websocket通信
前端
Stringzhua1 小时前
Vue中的数据渲染【4】
css·vue.js·css3
阿珊和她的猫5 小时前
v-scale-scree: 根据屏幕尺寸缩放内容
开发语言·前端·javascript
加班是不可能的,除非双倍日工资9 小时前
css预编译器实现星空背景图
前端·css·vue3