CSS 多个背景图像和背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5427371/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple background-images and a background-color
提问by Frederik Wordenskjold
Suppose I want to render an arrow in CSS, which should have a head, a tail and flexible width so it can contain text. I can of course create multiple divs to get what I want, but how can this be done in CSS3?
假设我想在 CSS 中渲染一个箭头,它应该有一个头部、一个尾部和灵活的宽度,以便它可以包含文本。我当然可以创建多个 div 来获得我想要的东西,但是如何在 CSS3 中做到这一点?
I can use multiple background images:
我可以使用多个背景图像:
div.arrow{
background: url('arrowtail.png') left no-repeat, url('arrowhead.png') right no-repeat;
}
The html:
html:
<div class="arrow">This text is on a transparent background</div>
This gives me an div
with an arrow-head and tail, and a transparent middle-section.
It does not seem possible specify the color to the middle section.
这给了我一个div
带有箭头的头部和尾部,以及一个透明的中间部分。似乎不可能指定中间部分的颜色。
With only one background-image, you could do this:
只有一张背景图片,你可以这样做:
div.arrow{ background: red url('some_image.png') no-repeat; }
I know this is doable in lots of ways, but is the background-color property really lost from the shorthand definition?
我知道这在很多方面都是可行的,但是背景颜色属性真的从速记定义中丢失了吗?
回答by BoltClock
No, it's not exactly lost from the shorthand declaration. You can still specify the background color, but only for the last (middle) layer (regardless of whether you put an image there):
不,它并没有完全从速记声明中丢失。您仍然可以指定背景颜色,但仅限于最后(中间)层(无论您是否将图像放在那里):
div.arrow {
background: url('arrowtail.png') left no-repeat,
url('arrowhead.png') right no-repeat,
red;
}
Note that for your scenario, your images may have to have completely opaque backgrounds. The background color will show under any transparent pixels of your images.
请注意,对于您的场景,您的图像可能必须具有完全不透明的背景。背景颜色将显示在图像的任何透明像素下。
Declaring background-color
separately, however, may be much better for your scenario as it lets you use different colors based on the same background images (if you're good with transparent pixels on the parts of your images to be filled with the CSS background color):
background-color
但是,单独声明可能更适合您的场景,因为它允许您根据相同的背景图像使用不同的颜色(如果您擅长使用 CSS 背景颜色填充图像部分的透明像素):
div.arrow {
background: url('arrowtail.png') left no-repeat,
url('arrowhead.png') right no-repeat;
}
/* Assuming your red arrow has this ID */
#red {
background-color: red;
}
回答by Jonathan Miller
I find using multiple background images to be problematic for things like this. Have you considered using the :before and :after pseudo elements? I wrote up a quick example:
我发现使用多个背景图像对于这样的事情是有问题的。您是否考虑过使用 :before 和 :after 伪元素?我写了一个简单的例子:
<style>
.arrow { display:block; margin:0; padding:0; width:200px; height:45px; line-height:45px; text-align:center; background:#ddd; }
.arrow:before { float:left; display:block; margin:0; padding:0; width:25px; height:45px; background:#ccc; content:''; }
.arrow:after { float:right; display:block; margin:0; padding:0; width:25px; height:45px; background:#ccc; content:''; }
</style>
<div class="arrow">This text is on a transparent background</div>
Just replace the background color in the :before and :after declarations to the arrow images you want.
只需将 :before 和 :after 声明中的背景颜色替换为您想要的箭头图像。