简体   繁体   English

为什么背景图像在CSS中比内联背景颜色具有更高的优先级?

[英]Why is background-image having more priority in the CSS than inline background-color?

I was trying to override a pre-defined background-image in a CSS with an inline background-color thinking it would actually override, but it looks it doesn't. 我试图用内联背景色覆盖CSS中的预定义背景图像,以为实际上可以覆盖,但是看起来并没有。 How can I achieve this? 我该如何实现?

 .section { position: relative; top: 0; left: 0; width: 250px; height: 250px; background-image: url("https://static.pexels.com/photos/36487/above-adventure-aerial-air.jpg"); background-size: cover; } 
 <div class="section" style="background-color: rgb(250, 40, 38)"> </div> 

Thanks. 谢谢。

https://www.w3.org/TR/css3-background/#the-background-color https://www.w3.org/TR/css3-background/#the-background-color

This property sets the background color of an element. 此属性设置元素的背景色。 The color is drawn behind any background images. 颜色绘制任何背景图像的后面

You can have both background-color and background-image . 您可以同时具有background-colorbackground-image If you want to override the background-image you need to do this with background: rgb(250, 40, 38) . 如果要覆盖background-image ,则需要使用background: rgb(250, 40, 38)

As for the WHY: 至于为什么:

you have these background properties: 您具有以下背景属性:

background-image
background-repeat
background-color
background-size

etc. 等等

Now if you set background you include all these properties so you can specify this: 现在,如果您设置background ,则包括所有这些属性,因此可以指定以下内容:

background-image: url(image.jpg)

or background-color: rgb(250, 40, 38) background-color: rgb(250, 40, 38)

or background: rgb(250, 40, 38) background: rgb(250, 40, 38)

This last one will assume there is no image because it is not specified in background and will thus override it with the specified background color 最后一个将假定没有图像,因为它不是在background指定的,因此将使用指定的背景色覆盖它

Just set background-image:none; 只需设置background-image:none;

eg; 例如;

 .section { position: relative; top: 0; left: 0; width: 250px; height: 250px; background-image: url("https://static.pexels.com/photos/36487/above-adventure-aerial-air.jpg"); background-size: cover; } 
 <div class="section" style="background-image:none; background-color: rgb(250, 40, 38)"> </div> 

EDIT: The reason image has higher priority is if you want to use an image but an object can extend beyond the image size (and you have repeat set to none) then the background-color fills the rest of the space. 编辑:图像具有更高优先级的原因是,如果您想使用图像,但是对象可以扩展到图像尺寸之外(并且您将重复设置为none),那么背景色将填充其余空间。

I think the issue is that you need to both add your css for the new colour but also the styling for the background image itself. 我认为问题在于您不仅需要为新颜色添加css,还需要为背景图像本身添加样式。

If you are wanting to hide the image so that your colour will come through and be visible add 如果您想隐藏图像以使颜色通过并可见,请添加

 <div class="section" style="background-color: rgb(250, 40, 38);background-image:none;"> </div> 

That should do the trick. 这应该够了吧。 :) Hope this helps :) 希望这可以帮助

Try this. 尝试这个。 Worked for me. 为我工作。

 .section { position: relative; top: 0; left: 0; width: 250px; height: 250px; background-image: url("https://static.pexels.com/photos/36487/above-adventure-aerial-air.jpg"); background-size: cover; } 
 <div class="section" style="background-image:none; background-color: rgb(250, 40, 38)"> </div> 

Use background instead of background-image and background-color : 使用background代替background-imagebackground-color

.section {
  position: relative;
  top: 0;
  left: 0;
  width: 250px;
  height: 250px;
  background: url("https://static.pexels.com/photos/36487/above-adventure-aerial-air.jpg");
  background-size: cover;
}

<div class="section" style="background: rgb(250, 40, 38)">

and it will work! 它会工作!

The spec was written with progressive enhancement in mind: 编写规范时要牢记逐步增强的意义:

Use background image, to display an image, and background color as a fallback for older browsers. 使用背景图片显示图片,并使用背景颜色作为旧版浏览器的后备广告。

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

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