简体   繁体   English

可以设置背景图片CSS的背景颜色吗?

[英]Can you set background colour of background image CSS?

bit of a strange request but I was wondering if it is possible to set the background colour of a background image applied to a div? 奇怪的请求,但我想知道是否可以设置应用于div的背景图像的背景色? I know you can set the background colour of an <img /> . 我知道您可以设置<img />的背景颜色。

I've tried: 我试过了:

background: #FFFFFF url(image.png) no-repeat bottom center;

Which just changes the colour of the div that the image is applied to. 只是改变了图像所应用的div的颜色。

Thanks for any help at all 谢谢你的帮助

*edit, added image to better explain issue: *编辑,添加图片以更好地解释问题:

在此处输入图片说明

The last pod in this right hand nav has the background image/shadow applied, i would ideally like to give this shadow a background color so that it blends in with the background of the page rather than being white like the containing div. 右边导航中的最后一个窗格已应用了背景图像/阴影,我希望为该阴影提供背景颜色,以便使其与页面背景融合,而不是像包含div的白色那样。

Use 采用

background: #FFFFFF url(image.png) no-repeat bottom center;

it will fill up the background of the element (div, ...) with the image.png and fill the rest with the color #FFFFFF 它将使用image.png填充元素(div,...)的背景,并将其余颜色填充为#FFFFFF

If those are all a single image (it's usually considered bad practice to have content text in images, btw), then I'd recommend using your image editor to make the space between the blocks transparent (don't forget to include the shadows, so they have alpha transparency over the correct color, instead of over white). 如果这些都是单个图像(通常在图像中包含内容文本,这通常被认为是不好的做法,顺便说一句),那么我建议您使用图像编辑器来使块之间的空间透明(不要忘记包含阴影,因此它们在正确的颜色(而不是白色)上具有Alpha透明度)。 That will allow the HTML element's background color to show through. 这样可以显示HTML元素的背景色。

That said, I would personally make those blocks into regular text and HTML blocks (so the only images are the decoration images on the right hand side of each), which you can easily add the shadow to with box-shadow . 就是说,我个人将这些块制作为常规文本和HTML块(因此,唯一的图像是每个图像的右侧的装饰图像),您可以使用box-shadow轻松地添加box-shadow

The box-shadow property is, by nature, transparent other than where the shadow itself is. 本质上, box-shadow属性是透明的,而不是阴影本身所在的位置。 This means that its "background" will always be what is underneath it (in simple cases, this would be the background of the containing element, though it can also be text or images, if the element with box-shadow is on top of it). 这意味着其“背景”将始终是其下方的内容(在简单情况下,这将是包含元素的背景,尽管如果带有box-shadow的元素位于其上方,也可以是文本或图像。 )。

Check out my jsFiddle to see what I'm talking about (it's a quick one, so it doesn't look exactly like the effect you're going for, but that effect is possible with a little creativity): http://jsfiddle.net/DXrk6/1/ 查看我的jsFiddle,看看我在说什么(这是一种快速的方法,因此它看起来并不完全像您想要的效果,但是这种效果可能需要一点创意): http:// jsfiddle .net / DXrk6 / 1 /

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

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