简体   繁体   English

多个CSS3背景,替换顶层

[英]Multiple CSS3 backgrounds, replace top layer

I have a button with a background-image property that sets 1) an icon for the button and 2) a CSS3 background gradient. 我有一个带有background-image属性的按钮,该属性设置1)按钮的图标和2)CSS3背景渐变。 I would now like to override the background gradient further down the page, so the icon remains the same and I can create many button colours by simply overriding the background gradients. 我现在想覆盖页面下方的背景渐变,因此图标保持不变,并且我可以通过简单地覆盖背景渐变来创建许多按钮颜色。

Is there currently a way to override a specific layer of a multiple background property? 当前是否有一种方法可以覆盖多个背景属性的特定层?

http://gard.me/1ulmH http://gard.me/1ulmH

HTML: HTML:

<a class="newButton blue" href="#">hello world</a>

CSS: CSS:

.newButton /* Orange by default */
{
    margin: 20px;
    display: inline-block;
    padding: 12px 20px;

    background: none;
    background-repeat: no-repeat;
    background-position:  9px 5px;
    background-position:  9px 5px, 0 0;

    -webkit-border-radius: 3px;
    -moz-border-radius: 3px;
    border-radius: 3px;
    border-width: 1px;
    border-style: solid;

    font-family: Verdana, Arial, Sans-Serif;
    text-decoration: none;
    text-align: center;

    /* Orange stuff */
    color: #FFECEA;
    border-color: #A03E33;
    background-position: 0 0;

    background-color: #E46553;
    background-image: url('http://www.waveclothing.co.uk/media/Shopping%20Cart.png'), -o-linear-gradient(bottom, #D15039 0%, #F27466 100%);
    background-image: url('http://www.waveclothing.co.uk/media/Shopping%20Cart.png'), -moz-linear-gradient(bottom, #D15039 0%, #F27466 100%);
    background-image: url('http://www.waveclothing.co.uk/media/Shopping%20Cart.png'), -webkit-linear-gradient(bottom, #D15039 0%, #F27466 100%);
    background-image: url('http://www.waveclothing.co.uk/media/Shopping%20Cart.png'), -ms-linear-gradient(bottom, #D15039 0%, #F27466 100%);
    background-image: url('http://www.waveclothing.co.uk/media/Shopping%20Cart.png'), -webkit-gradient(linear, left bottom, left top, color-stop(0, #D15039), color-stop(1, #F27466));
}

.newButton.blue { /* Blue */ /* Here I need to overwrite the button background colour */
    background-image: -webkit-gradient(linear, left bottom, left top, color-stop(0, #0B3661), color-stop(1, #0E4479));
}

enter code here You need to give it the full image usage just like the original definition, because the new definition is going to overwrite the whole background. enter code here您需要像原始定义一样为其提供完整的图像用法,因为新定义将覆盖整个背景。 So 所以

.newButton.blue {
    background-image: url('http://www.waveclothing.co.uk/media/Shopping%20Cart.png'), -webkit-gradient(linear, left bottom, left top, color-stop(0, #0B3661), color-stop(1, #0E4479)); 
}

Updated: 更新:

If you really want to individually switch the gradients, then you need to either put a span element in the a tag to place your icon image into and set that background independently on the icon ( span ) and gradient ( a ) OR since the gradients are new browser technology, do those on a :before or :after pseudoelement set to sit below the a tag. 如果你真的想单独设置梯度,那么你需要要么把一个span元素在a独立的标签,将您的图标图像到并设置背景上的图标( span )和梯度( a以后的梯度新的浏览器技术,请在设置为a标记下方的伪元素:before:after上执行这些操作。 Something like: 就像是:

a { 
    position: relative; 
    z-index: 1;
    ...icon related background code here...
}
a:after {
    content: '';
    display: block;
    position: absolute;
    z-index: -1;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    ...gradient related background code here...
}

EDIT: Note, as I reread your original question, it appears you may want the gradient above the icon. 编辑:请注意,当我重读您的原始问题时,似乎您可能需要图标上方的渐变。 If so, you need to swap the background code for what I gave above. 如果是这样,您需要将背景代码换成我上面提供的内容。

When you set a new value for "background-image" it fully overrides its previous definition. 当您为“背景图像”设置新值时,它将完全覆盖其先前的定义。 Only the last definition applied will prevail. 仅以最后应用的定义为准。

I suggest you include the icon url for every background-image definition. 我建议您为每个背景图片定义都包含图标网址。

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

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