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;  
}
相关推荐
郑州光合科技余经理17 小时前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
唐璜Taro18 小时前
Vue3 + TypeScript 后台管理系统完整方案
前端·javascript·typescript
dustcell.18 小时前
haproxy七层代理
java·开发语言·前端
掘金酱18 小时前
「寻找年味」 沸点活动|获奖名单公示🎊
前端·人工智能·后端
患得患失94919 小时前
【前端】前端动画优化的核心
前端
Xin_z_19 小时前
Vue3 + Sticky 锚点跳转被遮挡问题解决方案
前端·javascript·vue.js
REDcker19 小时前
WebCodecs VideoDecoder 的 hardwareAcceleration 使用
前端·音视频·实时音视频·直播·webcodecs·videodecoder
修炼前端秘籍的小帅19 小时前
Stitch——Google热门的免费AI UI设计工具
前端·人工智能·ui
精神状态良好19 小时前
实战:从零构建本地 Code Review 插件
前端·llm
荒诞英雄19 小时前
Vue3 Teleport我真是没招了
前端·vue.js