Below is an image of what I am trying to accomplish in CSS:
As you can see, I have a gradient with a pattern on top. The pattern itself looks like this:
This pattern in photoshop has a 50% opacity to give it the look I have in the first image.
So to the HTML & CSS i’ve figured that I will need two elements to accomplish this just like this:
<header class="header background-gradient" id="header">
<div class="background-pattern">
// contents
</div>
</header>
Now, what I have attempted is the following:
.background-gradient {
background: #4261cf;
background: -moz-linear-gradient(45deg, #3023ae 0%, #53a0fd 100%);
background: -webkit-gradient(linear, left bottom, right top, color-stop(0%, #3023ae), color-stop(100%, #53a0fd));
background: -webkit-linear-gradient(45deg, #3023ae 0%, #53a0fd 100%);
background: -o-linear-gradient(45deg, #3023ae 0%, #53a0fd 100%);
background: -ms-linear-gradient(45deg, #3023ae 0%, #53a0fd 100%);
background: linear-gradient(45deg, #3023ae 0%, #53a0fd 100%);
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='$gradient-start', endColorstr='$gradient-end',GradientType=1 );
color: white; }
Above is the gradient effect which looks perfect, now where i struggle is with the pattern, if I apply the pattern in css like this:
.background-pattern {
background: url(../images/pattern.jpg) repeat;
opacity: .5; }
The issue I now face is all the child elements take on the .5 opacity and I don’t know how I can avoid this?
You can checkout my fiddle to see this working in action.
5
Answers
FIDDLE GOES HERE
I have managed to figure it out mostly based on the comment provided by @Zentoaku
I have applied to the background-gradient css:
Hope this helps anyone else doing the same sort of thing
EDIT: You should also ensure that you set
.background-pattern
to have aheight
of100%
to ensure it fits within the parent elements sizes.What you can do is
and now
This solves the problem, unfortunately a position absolute is required to work with z-index. Your choice whether to use it or not.
(the indented stuff is new css and i just pulled out the h1 from the header element)
updated fiddle: http://jsfiddle.net/v82Luxfc/7/
(nice gradient btw, looks sick!)
It looks like there’s already a suitable answer, but I figure I’ll throw in one more different one. Some newer browsers let you layer multiple background images (or gradients) on top of each other. The result would look something like this:
(Not tested)
Since it’s a lot of CSS, I decided to try omitting bg-repeat (is repeat by default) and some of the browser prefixes. (I’m not sure where
-ms-linear-gradient
came from. The first MS browser to support gradients allows you to use the standardized syntax)Of course, this approach has the disadvantage of not working on older browsers. I believe IE9 is the earliest to support multiple backgrounds, and IE10 is the earliest to support gradients.
http://jsfiddle.net/5sbn1fn6/2/
HTML:
CSS: