
Asked by Alex van den Hoogen a Freelance Designer from the Netherlands
I have build a website which has a variable background. However I want to use div’s that are transparent. This works, but when I want to use images in those div’s they are transparent too, which gives an very strange effect.
Thus my question is, is it possible to make a transparent div with opaque text and images in it?
This cannot be removed by declaring styles specific to the children. Using png images as background images for content-heavy areas may not be the best idea and wont work smoothly cross-browser.
The best solution is to seperate the content and background into seperate DIVs and apply a position:relative to the DIV holding the content. This is done within the parent element that then appears to have a translucent background.
The Bits&Pixels blog demonstrates this effect
Source: http://bitsamppixels.com/2008/02/11/cross-browser-transparent-columns/
<div id=”column-1″ class=”container”>
<div class=”overlay”></div>
<div class=”content”>
<h2>Content</h2>
</div>
</div>…Set the container to position: relative; float: left;
this allows the parent to contain all children if they are floated.css#column-1 {
position: relative;
float: left;
width: 500px; /* remember to set a width */
}Not let’s take care of the transparency on the “overlay”-div.
css.overlay{
position: absolute;
top: 0; /* These positions makes sure that the overlay */
bottom: 0; /* will cover the entire parent */
left: 0;
width: 100%;
background: #000;
opacity: 0.65;
-moz-opacity: 0.65; /* older Gecko-based browsers */
filter:alpha(opacity=65); /* For IE6&7 */
}.content {
position: relative; /* Moves the Text above the transparency */
}
Check out the rest of the tutorial to find out how to make it work in IE6. You can find it here.
Demo Page
OR follow on Twitter
Twitter.com/DeDestruct
Comments
Just use a transparent PNG. IE will turn off anti-aliasing on text included in a div with the filter property. Circumvent the whole mess with the PNG.
It wouldn’t work in IE6, but what does?
Chris Poteet’s last blog post… HTML 5 vs. XHTML 2: The Future of Web Standards - Powered by CommentLuv
I haven’t tried using transparent divs yet. I’ll try it out using this howto guide.
There is currently no way to achieve what you are hoping for in a standards compliant markup solution. You can either choose 1) to use a transparent png 2) to use extensive scripting to make it (mostly) work
or 3) to forget the scheme and rethink your design plans until CSS catches up a little more.
The definition given by W3C says that the opacity affects the element that it is applied to as well as ALL children. This stinks I know.
nice effect, thanks
Have you seen this website:
http://www.icoblog.com/2008/05/new-site-design-for-iconicocom.html
They’ve got a really nice design, on the left sidebar they have ‘holes’ in the panel and two layers of transparency so that you can see through to the background. It’s really clever.
I had the same problem in my designs. This is how I fixed it.
Just set up a containing div that has no content, for everything. Within it, add a div that has your background, with the same dimensions. (Of course you’ll want to test things like this in IE too, just to be paranoid.) Afterwards, list everything that’s opaque in the original div box, the completely invisible one, with a higher z-index than your background.
Here’s a quick code sample.
HTML fragment:
<div class=”menu”>
<div class=”menubacking”></div>
<div class=”menuitem”>Homepage</div>
<div class=”menuitem”>About Us</div>
<div class=”menuitem”>Contact Us</div>
</div>
CSS:
div.menu {
position: fixed;
left: 0;
top: 0;
padding: 0;
border: 0;
margin: 0 0 0 0;
height: 100%;
width: 33%;
}
div.menubacking {
z-index: -10;
position: fixed;
left: 0;
top: 0;
padding: 0;
border: 0;
margin: 0 0 0 0;
height: 100%;
width: 33%;
background-color: #300000;
/* for IE */
filter:alpha(opacity=60);
/* CSS3 standard */
opacity:0.6;
}
div.menuitem {
z-index: 10;
position: relative;
margin-left: 0;
margin-bottom: 3%;
padding: 0;
border: 0;
left: 0;
background-color: #FFFFFF;
height: 5%;
/* for IE */
filter:alpha(opacity=100);
/* CSS3 standard */
opacity:1.0;
}
Cheers.
–Mike Pritt
Midnight Oil Development
Trackbacks
Leave a reply