简体   繁体   English

CSS限制背景重复(多个背景图像)

[英]CSS limited background repeat (multiple background images)

I am trying to make a cool background effect (with alpha transparency and rounded corners) for a drop-down menu with a single CSS entry. 我试图为一个带有单个CSS条目的下拉菜单制作一个很酷的背景效果(带有Alpha透明度和圆角)。

I have a top cap (180 x 4 px), a bottom cap (180 x 20px) and a repeating middle (180 x 2px). 我有一个顶盖(180 x 4 px),一个底盖(180 x 20px)和一个重复的中间(180 x 2px)。

Here is my existing relevant CSS: 这是我现有的相关CSS:

background-image: url('images/drop_top.png'), url('images/drop_bottom.png'), url('images/drop_middle.png');
background-position:left top, left bottom, 0px 10px;
background-repeat:no-repeat, no-repeat, repeat-y;

The problem is that the middle section which needs to be expandable/tilable is repeating all the way under the top and bottom caps--such that my rounded corners are now square because they have the repeating middle under them. 问题是需要可扩展/可扩展的中间部分在顶部和底部下方一直重复 - 这样我的圆角现在是方形的,因为它们下方有重复的中间部分。

Is there some way to prevent the multiple backgrounds from overlapping?? 有没有办法防止多个背景重叠?

Thanks in advance! 提前致谢!

Background-origin and/or background-clip should do the trick. 背景原点和/或背景剪辑应该可以解决问题。 Just set top & bottom borders equal to the height of your cap graphics, then set drop_middle to background-clip:padding-box 只需将顶部和底部边框设置为等于封面图形的高度,然后将drop_middle设置为background-clip:padding-box


EDIT: Here is a complete solution, but for a horizontal orientation: http://jsfiddle.net/nGSba/ 编辑:这是一个完整的解决方案,但横向方向: http//jsfiddle.net/nGSba/

#box
{
    display: inline-block;
    margin: 1em;
    padding: 9px;
    border-left:9px solid transparent;
    border-right:9px solid transparent;
    background-image: url(http://s11.postimage.org/ufpdknvjz/left.png), 
          url(http://s11.postimage.org/6ng294tj3/right.png),
          url(http://www.css3.info/wp-content/themes/new_css3/img/main.png);
    background-repeat: no-repeat, no-repeat, repeat-x;
    background-position: left top, right top, left top;
    background-origin: border-box,border-box,padding-box;
    background-clip: border-box,border-box,padding-box;
}

What got me stuck was the transparent on the border-color . 让我卡住的是border-color transparent The background will always go under the border, so if your border is solid the background will still be invisible. 背景将始终位于边框下方 ,因此如果边框是实线,则背景仍然不可见。

Whatever your element is that you are applying the images to, try doing the following (I'll assume div for sake of illustration): 无论您使用图像的元素是什么,请尝试执行以下操作(为了说明,我将假设为div ):

div {
    background-image: url('images/drop_top.png'), url('images/drop_bottom.png');
    background-position:left top, left bottom;
    background-repeat:no-repeat, no-repeat;
    position: relative;
}

div:after {
    position: absolute;
    top: 10px ; /* whatever your top image height is */
    bottom: 10px; /* whatever your bottom image height is */
    left: 0;
    right: 0;
    z-index: -1;
    background-image: url('images/drop_middle.png');
    background-position: left top;
    background-repeat: repeat-y;
}

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

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