This question already has an answer here:
Can I assign the opacity
property to the background
property of a div
only and not to the text on it?
I've tried:
background: #CCC;
opacity: 0.6;
but this doesn't change the opacity.
This question already has an answer here:
Can I assign the opacity
property to the background
property of a div
only and not to the text on it?
I've tried:
background: #CCC;
opacity: 0.6;
but this doesn't change the opacity.
A great way to do this would be to use css3 indeed.
Create a div width a class - e.g. supersizer on top of your page:
then set following css-properties:
You can't. You have to have a separate div that is just that background, so that you can only apply the opacity to that.
I tried doing this recently, and since I was already using jQuery, I found the following to be the least hassle:
text
div a solid background color, because that will be the JS-less default.text
div's height, and apply it to thebackground
div.I'm sure there's some kind of CSS ninja way to do all this with only floats or something, but I didn't have the patience to figure it out.
It sounds like you want to use a transparent background, in which case you could try using the
rgba()
function:A small example showing how
rgba
can be used.As of 2018, practically every browser supports the
rgba
syntax.For LESS users only:
If you don't like to set your colors using RGBA, but rather using HEX, there are solutions.
You could use a mixin like:
And use it like
Actually this is what a built-in LESS function also provide:
See How to convert HEX color to rgba with Less compiler?
For anyone coming across this thread, here's a script called thatsNotYoChild.js that I just wrote that solves this problem automatically:
http://www.impressivewebs.com/fixing-parent-child-opacity/
Basically, it separates children from the parent element, but keeps the element in the same physical location on the page.
use
This method will work in all browsers