html - 如何使背景图像响应?
问题描述
目前,当我调整窗口大小时,div 变得不那么宽,但图像仍然固定在左侧。因此,如果我在移动浏览器上查看它,我会看到图像的左侧。我不知道如何在调整图像大小时始终注视图像的中心,而图像的左侧和右侧变宽。图像为 2560 x 677。
这是我现在拥有的 CSS:
#showcase {
position: relative;
margin: auto;
min-height: 600px;
background: url("banner.png"); }
#showcase .primary-overlay {
background: rgba(0, 0, 0, 0.3);
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%; }
这是相关的div:
<section id="showcase" class="py-5">
<div class="primary-overlay text-white">
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<h1 class="display-1 mt-5 pt-5">
<strong>FILLER TEXT</strong>
</h1>
<p class="lead">BLAH BLAH BLAH </p>
<div class="text-white display-4" id="countdown"></div>
感谢您的任何见解。
解决方案
用于background-position
在元素内定位背景。而且您可能还想background-size
用来告诉图像如何调整大小。
#showcase {
position: relative;
margin: auto;
min-height: 600px;
background-image: url("banner.png");
background-position: center;
background-size: contain; /* or cover */
}
推荐阅读
- javascript - Javascript - 更新数组中某些项目属性的最佳方法
- memory - 如何找到 BIOS 芯片的地址空间?
- matlab - 如何操作按钮位置属性?
- javascript - DM 中的 AwaitMessages 不起作用 Discord.js
- c# - 将整数拆分并存储为两个字节
- javascript - 帆布,对角线较粗
- ruby - 在计算阶乘时,为什么这种递归方法不起作用?
- c# - 如何使用修剪功能修剪列表内部
- postgresql - 错误 CREATE DATABASE 无法在事务块内运行
- javascript - React App - 为什么当小时数为零时 m 倒计时显示一个点?而我的分钟不是 00 时通过 59