How to use two backgrounds for one element and put second background on top of first?

两盒软妹~` 提交于 2019-12-07 05:56:25

If you only care about recent WebKit browsers, you can use CSS3's multiple backgrounds:

See: http://jsfiddle.net/thirtydot/xCnZs/

HTML:

<h1>Heading Level 1</h1>

CSS:

h1 {
    font: 42px/1 Georgia, serif; 
    color: #fff;
    margin: 0;
    padding: 12px;

    background: url(http://i.stack.imgur.com/rgOES.png) top left repeat, -webkit-gradient(linear, left top, left bottom, color-stop(0%,#6db3f2), color-stop(50%,#54a3ee), color-stop(51%,#3690f0), color-stop(100%,#1e69de));

    text-shadow: 2px 2px 3px #000;
}

I used Photoshop to quickly make this image:

It's a transparent .png with just a random texture on it. It doesn't tile very well, but I didn't try to make it do so; it was just to show the concept.

It would be very difficult to make the texture with pure CSS. Though probably not impossible.

I'd recommend using embedded elements. Don't use a span because spans are inline elements and you'll have to make them display:block to get the correct overlay. I'd but the gradient in the outer-most element and your transparent texture inside it. Semantically your probably should have a div on the outside with you h1 inside it like so.

<div class="gradient">
    <h1 class="texture">Heading Level 1</h1>
</div>

Good news! You don't have to worry about only going webkit for the gradients.

    .gradient { background: -webkit-gradient(linear, left top, left bottom, from(#cfcdd2), to(#fff)); 
    background: -moz-linear-gradient(top,  #cfcdd2,  #fff); 
    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#cfcdd2', endColorstr='#000000'); }
    .texture { background: url('myimage.png'); }

The above code will give you a gray to white gradient (top to bottom) just change out the colors with your colorscheme. It works in most browsers (not ie7 but yes ie8).

I'll be happy to help further if you need advice.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!