This is a regular problem with opacity. You simply cannot set opacity on some box without hitting the children. This is because, for the children, the parent’s lower opacity is their “100% opaque”. So the children see 0.4 as 100%, or as opaque as they can possibly be.
Some ideas:
I’ve done it where, so long as the children were approx a set height, made the background div and the content as siblings rather than parent-child.
So like
<header>
everything to be semi-opaque in here
</header>
<stuff to be fully opaque here/>
Then #header can have its CSS like you have it, with a set height (height can be in em’s), and the child <stuff> gets a negative margin to pull it up over the #header area.
This method is brittle and creates more code than otherwise necessary, so while I’ve done it, it should probably be a last resort.
Another method is an actual image that looks like it’s semi-opaque. It will work if the #header doesn’t grow much or at all in height and cannot slide around from left to right (or, whatever’s behind it cannot slide around from left to right). The image isn’t a PNG with alpha-transparency: it’s the background image with a semi-trans layer of whatever colour (white?) you want layered on top of it… then you flatten it and make it an 8-bit PNG (or jpg if you want). This will mean no fancy crap necessary for IE6 and an overall smaller filesize (than a semi-trans PNG).
Does not work if stuff on the page moves (if page is flexible width or content inside header can grow down) <— but if you’re willing to make an image that’s quite tall then some growth can be allowed and there can be enough “image” there… or, alternatively, the image can fade to bg colour at the bottom.
CSS3 background-opacity using rgba to set both the colour and the opacity. Does not work in IE, but if your design is ok with a solid background colour for IE, this can be an option. So if you have a background image and you’re making it partially white, you could do so:
background-image: url(blah.png);
background-color: rgba(255, 255, 255, 255, 0.4);
Hm, I forget if you can make the actual background image 0.4… I’d have to look it up.
What you do is pull the headerstuff as high up as the #header is tall. Maybe when starting out with this stuff, use ugly background colours so you can see what’s happening.
#header {
width: 940px;
height: 132px;
background-color: #530D1C;
filter:alpha(opacity=40);
opacity: 0.4;
-moz-opacity:0.4; /*you know this is only for like FF2 or whatever, right?*/
}
#headerstuff {
height: 132px;
[b]margin-top: -132px;[/b]
}
That basically should do it, however if that’s not working at all on your page, there may be other code interfering.
I have an example of of this opacity trick but since it involves a bunch of other stuff, it’s not really the best example for this. Maybe I’ll make a simple one.
For example 2, I think I’d have to download some software, because my OS won’t take screenshots if I’m opening a menu in the GIMP. Sucks. If you have any basic image editing software that has “layers” and lets you set the opacity per layer then I can go through a basic version of example 2. Though I’d prolly want an example image (whatever is the background of #header) to do it with.
This is confusing me. I’m starting to suspect what you want is not what I thought I read.
Do you have an image you can post (links are nicer than attachments because they’re faster… img.ur or something) that shows what you’re going for? Then I can code some examples.
You were looking in Firefox? Aha. Not sure who else does this. Ah, Safari and Chrome. *Edit seems to also be in Opera too
So in my original code, I made the assumption (which is absolutely true if we don’t change any opacity) that elements later in source order stack “higher” than elements earlier in source order, so just pulling the later element up with a negative top margin also keeps that element on top (remove opacity to see this).
However, at least in Gecko and webkit (*edit and Presto) browsers, this stops being true once we make things less opaque. Arg.
However, simply setting headerstuff to position: relative (which I often end up doing for other reasons in more complicated code anyway, which is why I never saw this) ensures the later-in-source stuff remains on top… and so not seethrough.
basically, what im trying to do,
is make sure the elements of a div with opacity - (ie logos) DONT also end up with the opacity that i set on the background colour, not quite sure how else to explain it…