css - 将背景图像拟合到 div
问题描述
我正在使用以下 css 使 div 在屏幕上始终全尺寸,
.container{
position: fixed;
top: 0;
left: 0;
bottom: 0;
right: 0;
overflow: auto;
background: url("../assets/shutterstock_585038551.jpg");
/*border: 1px solid black;*/
}
但是只有一部分背景图像显示出来,如何在不改变其纵横比的情况下适应背景图像。
解决方案
将以下内容添加到您的 CSS。
background-size: cover;
background-repeat:no-repeat;
这假设您的.container
div 是全宽和全高。
如果不是,还添加以下内容:
width: 100vw;
height:100vh;
关于背景大小的一些信息。
https://www.w3schools.com/cssref/css3_pr_background-size.asp
https://developer.mozilla.org/en-US/docs/Web/CSS/background-size
vh
关于和的一些信息vw
推荐阅读
- c - *scanf 中 %n 运算符的一致性和行为
- java - java-是否需要 revalidate() 方法?
- javascript - html value="" 不显示
- encryption-asymmetric - 您可以从 NCipher HSM 导出私钥并导入 Java 密钥库 (JCEKS)
- javascript - 使用 react-native 为 iPad/平板电脑启用横向模式
- linux - 我的 docker 镜像在我的驱动器上的哪个位置?(不在/下)
- android - threejs 压缩纹理 pc/mobile android/ios/chrome 兼容性
- java - 无法解析或不是类继承中的字段
- reactjs - 在将组件作为道具传递时,如何在 TypeScript(在 React 中)强制传递道具?
- regex - VSCode - 正则表达式字符集中的转义点