【发布时间】:2022-02-05 06:42:39
【问题描述】:
如何使用图片作为div的背景?
I want to make this image as background
and the result i got is like this
这是我的代码
<s.Container
ai={"center"}
style={{
backgroundImage: "url(" + "/config/images/bg2.png" + ")",
backgroundPosition: "center",
backgroundSize: "cover",
backgroundRepeat: "no-repeat",
position: "relative",
}}
> </s.Container>
【问题讨论】:
-
div的高宽比和图片一样吗?它应该适用于背景大小:封面,所以我唯一的猜测是图像纵横比可能不等于 div 纵横比。
-
如果div大小为100%,如何让图片大小与div大小匹配?
-
这可能是一个线索,我之前在
width: 100%使用图像时遇到过问题。如果您要归档全屏宽度,请尝试使用width: 100vw,否则,只需设置定义的宽度。希望它有效