Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overlay a CSS gradient with an RGBA colour?

I'm wanting to add a transparent black overlay to a button for it' :active state, so when you click it, it's the same gradient but with just an overlay of e.g. rgba(0,0,0,.3)

The way I thought this would work is (using webkit in this example):

background:rgba(0,0,0,.3), -webkit-linear-gradient(top, #fcfcfc 0%,#bababa 100%);

or without the comma, and the order reversed... but nothing shows up at all!

I'm not keen on adding another div to act as the overlay to do it, so is there a strictly CSS way to do this? I was thinking maybe it's a :before or :after pseudo class, but I don't have a clue how to use these!

Would really appreciate an answer, this has been bugging me for a long time.

like image 211
alexbass Avatar asked Jun 02 '12 01:06

alexbass


People also ask

How do you overlay gradient in CSS?

CSS gradients allow us to display smooth transitions between two or more colours. They can be added on top of the background image by simply combining the background-image URL and gradient properties.

How do I add a gradient color to an image in CSS?

CSS Linear GradientsTo create a linear gradient you must define at least two color stops. Color stops are the colors you want to render smooth transitions among. You can also set a starting point and a direction (or an angle) along with the gradient effect.

How do you make a transparent gradient in CSS?

To add transparency, we use the rgba() function to define the color stops. The last parameter in the rgba() function can be a value from 0 to 1, and it defines the transparency of the color: 0 indicates full transparency, 1 indicates full color (no transparency).


1 Answers

You can't do that; rgba defines a colour, not an image. What you can do is use a gradient that's not a gradient:

background: -webkit-linear-gradient(rgba(0, 0, 0, .3), rgba(0, 0, 0, .3)), -webkit-linear-gradient(top, #fcfcfc 0%,#bababa 100%);

This is why I always specify background-image instead of using the shorthand when developing - it makes debugging easier.

like image 132
Ry- Avatar answered Sep 19 '22 05:09

Ry-