所有语法在css中都是正确的,关于bg-image但没有得到显示

问题描述 投票:0回答:1

我无法在html div中获取背景图片url是正确的所有语法都是正确的。

HTML

<div class="img-container"></div>

CSS

.img-container {
   background-image: url("../img/photosm.jpg");
 }
html css
1个回答
0
投票

你可以测试两件事之一,

  1. 确保图像的路径正确。在devtools网络选项卡中,您可以查看图像是否正确加载。
  2. 使用图像作为背景不会使容器继承图像的高度和宽度。它需要有它自己可以这么说。所以尝试设置高度/宽度。
.img-container {
   background-image: url("../img/photosm.jpg");
   background-repeat: no-repeat;
   background-position: center;
   background-size: cover;
   height: 20rem;
   width: 30rem;
 }
© www.soinside.com 2019 - 2024. All rights reserved.