简体   繁体   English

背景图像未显示

[英]Background-image not showing

I am trying to turn images that appear on the site using the tag into a background image.我正在尝试将使用标签显示在网站上的图像转换为背景图像。 MY first code looked like this我的第一个代码看起来像这样

Html:网址:

<img class= "house" src="images/House-03.png" width="122px" height="118px"/>

CSS CSS

.house {
display:block;
position:absolute;
float:right;
bottom:0;
right:0;

} }

Now I'm trying to change it like this:现在我试图像这样改变它:

Html html

<p class= "house"> </p>

CSS CSS

.house {
    display:block;
    position:absolute;
    background-image:url(../images/House-03.png);
    width:122px;
    height:148px;
    float:right;
    bottom:0;
    right:0;
}

Except the image doesn't appear!除了图像没有出现! Any help will be appreciated, Thanks!任何帮助将不胜感激,谢谢!

try setting the background image size property 尝试设置背景图像大小属性

.house {
    display:block;
    position:absolute;
    background:url(../images/House-03.png);
    background-repeat:no-repeat;
    background-size: 100% 100%;
    width:122px;
    height:148px;
    float:right;
    bottom:0;
    right:0;
}

hope this helps? 希望这可以帮助?

put quotes around your image url 在你的图片网址上加上引号

background-image:url("../images/House-03.png");

ETA : per comment below... quotes are indeed not required! ETA:以下评论......确实不需要报价! just mentioned it here to troubleshoot the original question. 刚刚在这里提到它来解决原始问题。

I personally had the pathname and url syntax correct. 我个人认为路径名和网址语法正确。 The only issue was 唯一的问题是

background-img: url("") 

needing to be changed to.. 需要改为......

background: url("")

And my background pattern revealed itself triumphantly. 我的背景模式得意洋洋地展现出来。

Make sure your image is accessible! 确保您的图像可以访问!

After a lot of frustration, it turned out that my problem had to do with the image not being accessible... You can always test out your code with another image on another site that is certainly accessible... 经过很多挫折之后,事实证明我的问题与图像不可访问有关...你总是可以在另一个可以访问的网站上用另一个图像测试你的代码...

As shown by other answers there can be many things that can prevent a background from showing.正如其他答案所示,有很多事情可以阻止背景显示。 So even though the question is "Why is the image not showing?"所以即使问题是“为什么图像不显示?” a more general question is "How can I find out why my CSS-rule doesn't seem to take effect?"一个更普遍的问题是“我怎样才能找出为什么我的 CSS 规则似乎没有生效?”

I had the same or similar problem about background image not showing up and was able to find the answer by adding the "!important" -qualifier to my CSS-rule.我遇到了关于背景图像未显示的相同或类似问题,并且能够通过将 "!important" -qualifier 添加到我的 CSS 规则中来找到答案。 That made the image appear, which made me realize there must be some other CSS-rules with higher priority that prevent this particular rule about background from taking effect.这使得图像出现,这让我意识到必须有一些其他具有更高优先级的 CSS 规则来阻止这个关于背景的特定规则生效。

That is a good trouble-shooting tip in general.一般来说,这是一个很好的故障排除技巧。 If some CSS-rule doesn't seem to have any effect, try adding "!important" to it, and see if that helps.如果某些 CSS 规则似乎没有任何效果,请尝试向其中添加“!important”,看看是否有帮助。 If so, look for other rules that are preventing your rule from taking effect.如果是这样,请查找阻止您的规则生效的其他规则。

Try omitting one of the leading dots (../).尝试省略前导点之一 (../)。 I had this exact same problem thinking two dots is the previous directory but it's actually two back.我有这个完全相同的问题,认为两个点是前一个目录,但实际上是两个点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM