简体   繁体   English

如何使用CSS在背景上应用渐变?

[英]How can I apply a gradient over a background with CSS?

I'm getting my feet wet with CSS3 and I'm doing my best to convert a Photoshop comp to HTML. 我正在努力使用CSS3,并且正在竭尽全力将Photoshop组件转换为HTML。

I have multiple instances of a background (using background url) with differing heights and I'd like to apply a gradient on top of that background using rgba gradients (using the alpha channel). 我有一个背景的多个实例(使用背景url),这些实例的高度不同,并且我想使用rgba渐变 (使用alpha通道)在该背景的顶部应用渐变。 I'd obviously like to stay away from a static background image with the gradient built into the pixels. 我显然很想远离静态背景图像,并在像素中内置渐变。

Is there a way to do this in CSS by 'stacking' the gradient on top of the background url? 在CSS中,有没有一种方法可以通过将渐变“堆叠”在背景url顶部来实现?

I'm guessing if I can't do it in one element, I would put a container inside my background element, float it and make the width and height fill the background element, but that seems pretty messy. 我猜想如果不能在一个元素中做到这一点,我会在我的背景元素中放入一个容器,将其浮动并使宽度和高度填充背景元素,但这看起来很混乱。

Any advice is appreciated! 任何建议表示赞赏! Thanks for your time! 谢谢你的时间!

Here are two examples of the same background and gradient but at different heights: a nav and a footer 这是背景和渐变相同但高度不同的两个示例:导航和页脚

导航与背景和轻微的渐变在此处输入图片说明

The code would look something like this: 代码看起来像这样:

<nav>
 <ul>
  <li>Menu item 1</li>
  <li>Menu item 2</li>
  <li>Menu item 3</li>
  <li>Menu item 4</li>
 </ul>
</nav>

style: 样式:

nav {
  background : url('repeating-background-image.png') repeat;
  background: -moz-linear-gradient(top, rgba(0,0,0,0.65) 0%, rgba(0,0,0,0) 100%); /* FF3.6+ */
  background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(0,0,0,0.65)), color-stop(100%,rgba(0,0,0,0))); /* Chrome,Safari4+ */
  background: -webkit-linear-gradient(top, rgba(0,0,0,0.65) 0%,rgba(0,0,0,0) 100%); /*    Chrome10+,Safari5.1+ */
}

Is there a way to do this in CSS by 'stacking' the gradient on top of the background url? 在CSS中,有没有一种方法可以通过将渐变“堆叠”在背景url顶部来实现?

Yes: CSS3 allows multiple background images , separated by commas. 是:CSS3允许使用逗号分隔多个背景图像

As gradients behave like images, you can use them in conjunction with background images: 由于渐变的行为类似于图像,因此可以将它们与背景图像结合使用:

div {
    width: 400px;
    height: 400px;
    background: -moz-linear-gradient(top, rgba(0,0,0,0.65) 0%, rgba(0,0,0,0) 100%), url(http://www.pauldwaite.me.uk/images/professional.jpg); /* FF3.6+ */
    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,rgba(0,0,0,0.65)), color-stop(100%,rgba(0,0,0,0))), url(http://www.pauldwaite.me.uk/images/professional.jpg); /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(top, rgba(0,0,0,0.65) 0%,rgba(0,0,0,0) 100%), url(http://www.pauldwaite.me.uk/images/professional.jpg); /*    Chrome10+,Safari5.1+ */
}

This doesn't work in IE 8 or earlier, but then neither do CSS gradients. 这在IE 8或更早的版本中不起作用,但是CSS渐变也不起作用。 (Although Microsoft's filter property works in IE 8 and earlier, and that does support gradients with alpha transparency - see Can you use rgba colours in gradients produced with Internet Explorer's filter property? ). (尽管Microsoft的filter属性可在IE 8和更早版本中使用,并且确实支持具有alpha透明性的渐变-请参阅您可以在Internet Explorer的filter属性产生的渐变中使用rgba颜色吗? )。

http://jsfiddle.net/8gvZM/ http://jsfiddle.net/8gvZM/

background: #ffffff; /* old browsers */
background: -moz-linear-gradient(top, #ffffff 0%, #f6f6f6 47%, #ededed 100%); /* FF3.6+ */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#ffffff), color-stop(47%,#f6f6f6), color-stop(100%,#ededed)); /* Chrome,Safari4+ */
background: -webkit-linear-gradient(top, #ffffff 0%,#f6f6f6 47%,#ededed 100%); /* Chrome10+,Safari5.1+ */
background: -o-linear-gradient(top, #ffffff 0%,#f6f6f6 47%,#ededed 100%); /* Opera 11.10+ */
background: -ms-linear-gradient(top, #ffffff 0%,#f6f6f6 47%,#ededed 100%); /* IE10+ */
background: linear-gradient(top, #ffffff 0%,#f6f6f6 47%,#ededed 100%); /* W3C */
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#ffffff', endColorstr='#ededed',GradientType=0 ); /* IE6-9 */

and make a div above it with the background url on a lower opacity. 并在其上方创建一个div,并使用较低的不透明度的背景网址。

Is that what you mean? 你是这个意思吗?

Use CSS :before to create an additional (pseudo) element sitting on top of the original element. 使用CSS :before在原始元素之上创建一个额外的(伪)元素。

The original element would have the image background, and the :after element would have the gradient, with an opacity setting so that the original element shows through it. 原始元素将具有图像背景,而:after元素将具有渐变,并具有不透明度设置,以便原始元素通过它显示。

div {
    width: (whatever size you want to set it to)
    height: (ditto)
    position:relative;
    background:url('mainImage.jpg');
    z-index:5;
}

div::before {
    content:'';
    width: .... (same as main div)
    height: .... (same as main div)
    position:absolute;
    z-index:-3;
    display:block;
    opacity:0.5;
    background: linear-gradient(to bottom, #8fc400 0%,#ff0000 100%); /* plus add the other browser-specific gradient styles too */
}

I've done a jsFiddle for you to demonstrate: see here 我为您演示了一个jsFiddle: 请参见此处

Hope that helps. 希望能有所帮助。

[EDIT] Changed the details of the answer above slightly in response to OP's comment. [编辑]根据OP的评论,略微更改了以上答案的详细信息。 Now using :before rather than :after , and using z-index to layer things so that the actual text content is visible on top of both backgrounds. 现在使用:before而不是:after ,并使用z-index对事物进行分层,以便在两个背景上都可以看到实际的文本内容。

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

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