This occurred to me, but maybe not to other users of this site, so I thought I would point it out. When you use relative positioning of the text, the space it used to take up is still there. This isn't apparent in the solution mentioned above because he doesn't have much text and extra spacing below the image doesn't matter.
http://www.mandarindesign.com/troops...backgroundhard
although dan66 is exactly right, positioning 2 divs in the same spot is the trick. In my opinion, a better way to do it would be to make the transparent div positioned absolutely inside a container with... well, I don't know what the technical term for it is - Whatever property that's set when a div has either relative or absolute positioning set on it. When this is the case, all absolutely positioned elements will be relative to the boundaries of its container. Thus, give some element relative positioning, then use the two div trick inside it. the semi-transparent div with absolute positioning will be layered relative to the corners of this container, but completely outside of the normal text flow. Thus, the second div will automatically display over the semi-transparent div because the semi-trans div is not a part of normal text flow.
There's one thing I can't figure out though. For this to work, the text div has to have relative positioning applied to it, but doesn't need any positioning modifications. Otherwise, the text will be much darker than it would have been without the trick, but will get just a little opacity from the background (in FF at least). Seems odd to me, but there's obviously something I don't understand about that property for which I don't know the name of that is applied when an element is given relative or absolute positioning.
With this approach, I was able to implement a semi-transparent table-based calendar showing through to a background image. Very pretty, and should be cross-browser. Thanks for the head start, all.
Here's an excerpt of my CSS that applies to the effect I'm describing:
Code:
td {
width: 100px;
height: 100px;
vertical-align:top;
}
.transparent_background {
width: 100px;
height: 100px;
position: absolute;
background-color: green;
opacity:.5;
filter:alpha(opacity=50);
}
.text {
position:relative;
}
After I came up with this, I found a forum with a similar solution, although it wasn't clear what was necessary and why it wouldn't work in opera. from
http://www.ahfb2000.com/webmaster_he...ead.php?t=3318 Code:
<html>
<head>
<title>Transparent div</title>
<style type="text/css">
<!--
body {
background:url(http://hstrial-dbayly.homestead.com/files/dog.jpg) no-repeat top left;
margin:0;
}
#one {
background:#ff0000;
width:200px;
height:216px;
filter:progid:DXImageTransform.Microsoft.Alpha(opacity=20);
-moz-opacity:0.20;
position:absolute;
top:0;
left:0;
}
#two {
width:200px;
height:216px;
position:absolute;
top:0;
left:0;
font-family:arial;
font-size:18px;
color:#ffffff;
padding:20px;
}
//-->
</style>
</head>
<body>
<div id="one"></div>
<div id="two">
The text has to go in a separate div to the transparent div.
This effect will not work in Opera browsers.
</div>
</body>
</html>
I'd be interested to know in what ways people are using this solution, and in what situations it fails (and why). Thoughts?