css - 多个背景图像 - 无法将图像置于前面

标签 css

我使用 CSS3 渐变作为菜单按钮的背景颜色/悬停颜色,但在悬停时无法让我的背景之一(图标)出现在渐变上方。 CSS:

  #nav_menu a:hover {
    background-image: url("../img/menu_icon.png") no-repeat 3%, 50%;
    background-image: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #aa2329), color-stop(100%, #bb4d50));
    background-image: -webkit-linear-gradient(#aa2329, #bb4d50);
    background-image: -moz-linear-gradient(#aa2329, #bb4d50);
    background-image: -o-linear-gradient(#aa2329, #bb4d50);
    background-image: linear-gradient(#aa2329, #bb4d50); 
}

悬停时顶部背景图像不可见。通过为 li 提供单独的背景图像并将图标作为嵌套在 li 中的链接的背景图像,我能够在不悬停时使其工作。我不能在这里做同样的事情,因为我必须使用链接的悬停伪类作为渐变背景图像。任何建议将不胜感激。

最佳答案

您正在使用 background-image属性修改background-position/background-repeat特性。使用background shorthand property (不是 background-image ),你可以使用这样的东西:

background: url("..") no-repeat 3%, 50%;

本质上就是:

background-image: url("..);
background-repeat:no-repeat;
background-position:3%, 50%;

现在,根据 the spec - 对于多个背景,语法如下:

[ <bg-layer> , ]* <final-bg-layer>

<bg-layer> = <bg-image> || <position> [ / <bg-size> ]? || <repeat-style> || <attachment> || <box> || <box> 
<final-bg-layer> = <bg-image> || <position> [ / <bg-size> ]? || <repeat-style> || <attachment> || <box> || <box> || <'background-color'>

因此,您将使用以下内容:

WORKING EXAMPLE HERE

background: url("//placehold.it/100") no-repeat 3%, 50%,
           -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #aa2329), color-stop(100%, #bb4d50)),
           -webkit-linear-gradient(#aa2329, #bb4d50);
background: url("//placehold.it/100") no-repeat 3%, 50%,
           -moz-linear-gradient(#aa2329, #bb4d50);
background: url("//placehold.it/100") no-repeat 3%, 50%,
            -o-linear-gradient(#aa2329, #bb4d50);
background: url("//placehold.it/100") no-repeat 3%, 50%,
            linear-gradient(#aa2329, #bb4d50); 

关于css - 多个背景图像 - 无法将图像置于前面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22386652/

相关文章:

html - CSS 联系表没有响应

css - wordpress.com 和托管 wordpress 之间的风格差异

html - Bootstrap 3 行问题中的堆叠选项卡

html - 表格中的表格未正确显示(奇怪的间距)

javascript - 无需大量剪切和粘贴的多张幻灯片

html - Chrome 打印一些带有灰色填充的文本框

css - CSS 的 Bundler/Minifier(什么是 JavaScript 的 Browserify、Stitch 等)?

html - 在位置 :absolute parent 内垂直对齐一个 div

html - 链接适合 100% 的空间

Html 文本控件(DIV 无法识别动态创建的另一个 div??)