【问题标题】:Using background image over gradient在渐变上使用背景图像
【发布时间】:2014-11-03 11:09:36
【问题描述】:

我想要实现的是,我在 div 背景上有渐变,我试图在渐变上添加背景图像(图像只是一个图案),但要么只应用渐变,要么只应用图案,不是都 我用这段代码得到一个白色背景:

 #box{
    padding:50px;
    background:linear-gradient(45deg, #A50F06 0%, #51A351 100%) repeat scroll 0% 0% transparent url('bs-docs-masthead-pattern.png') repeat scroll center center transparent;
    } 

只有渐变:

#box{
background:linear-gradient(45deg, #A50F06 0%, #51A351 100%) repeat scroll 0% 0% 
}
 #box:after{
 background: transparent url('pattern.png') repeat scroll center center transparent;
}

我做错了什么?

【问题讨论】:

标签: css background-image gradient


【解决方案1】:

如下所示更新您的 CSS。

 #box{
  background:url('https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcSXwTmM2R_vmBHFpn720_8bGOaegnP5Kawh0wb4JggN5rUALGwGvw') no-repeat center center, linear-gradient(45deg, #A50F06 0%, #51A351 100%) repeat scroll 0% 0%;
  height:500px;
   }

FIDDLE DEMO

【讨论】:

    【解决方案2】:

    在伪元素中添加content:""

    * {
          margin: 0;
          padding: 0;
        }
        html,
        body {
          width: 100%;
          height: 100%;
        }
        #box {
          background: linear-gradient(45deg, #A50F06 0%, #51A351 100%) repeat scroll 0% 0%;
          width: 100%;
          height: 100%;
        }
        #box::after {
          content: "";
          background: url('http://s28.postimg.org/rfznph7ul/pattern.png');
          width: 100%;
          height: 100%;
          display: block;
        }
    <div id="box"></div>

    【讨论】:

    • 不,它不起作用...这是我的图片的链接,你能试一试吗(现在真的很烦我),还有你所说的关于添加 content:"" 为什么我们需要这样做吗?链接:dropbox.com/s/iexuzlqujo5bnlb/pattern.png?dl=0
    • 更新了你的图像正在工作的 sn-p 请检查我的答案@azazelroman
    • 是的,它正在工作:) 只需一个简单的问题就可以在不设置html and body 的情况下完成此操作吗?我的意思是我只需要在 div 上申请...实际上我正在使用引导框架并向正文添加样式可能会搞砸。
    • 我刚刚添加了它,因此 div 占据了全高 :) 如果您使用的是框架,则不需要
    • 没有 bodyhtml demo 的样式
    猜你喜欢
    • 2013-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多