views:

2077

answers:

4

What's the best way (if any) to make the inside box transparent so the image can be seen with no opacity (clear image) and the rest of the outer box opaque. So far this is what I'm doing:

<style>
#a {
  background-color: black;
  float: left;
} #b {
    opacity : 0.4;
    filter: alpha(opacity=40); 
} #div {
    position:absolute;
    height:30px;
    width:30px;
    top:90px;
    left:90px;
    border:1px solid #FFF;
    background:transparent;
}
</style>

<div id="a">
  <div id="b">    
    <img src="http://clagnut.com/images/ithaka.jpg" />
  </div>
</div>
<div id="div"></div>

Any ideas? thx

+5  A: 

The maximum opacity of an element is the opacity of its parent element. So if div#b has an opacity of 40%, if his children have 100% opacity in style they will also be 40% absolute opacity.

To accomplish what you're describing (at least what I think you're describing), one way could be to have both the transparent wrapper and the image children of a parent div with relative positioning. You can absolutely position both of the children inside of that wrapper so that the image shows up on top of the transparent box.

Edit: Here is the code for the effect you are describing. My example has a 480 x 320 image, and a 30-pixel border:

<style>
    #back {background-image:url(mypicture.jpg);
               width:480px;
               height:320px;
               position:relative;}
    #middle {position:absolute;
                 width:480px;
                 height:320px;
                 background-color:#000;
                 opacity:0.4;
                 filter:alpha(opacity=40);
                 top:0;
                 left:0;}
    #front {position:absolute;
                width:420px; /* 30px border on left & right */
                height:260px; /* 30px border on top & bottom */
                background-image:url(mypicture.jpg);
                background-position:-30px -30px; /* compensate for the border */
                top:30px;
                left:30px;}
</style>

<div id="back">
    <div id="middle">
    </div>
    <div id="front">
    </div>
</div>
Rex M
Don't make him think! It was a great answer to begin with, but I guess he needed you to DO IT for him before he'd accept.
TM
If this was school I would agree with you, but is professionals looking for solutions to specific problems. When I am searching for an answer, I look for a code sample from someone who has solved it. If I need to understand in-depth later, I will then.
Rex M
Rex M, I dunno, I'd say a professional would have even more incentive to understand it, since he's likely to have to do it again. Code examples are a great aid to understanding though.
TM
+1  A: 

If I understand you correctly, try using just one div (i.e. get rid of the outer one with ID "a") and setting a colored border around it. Or you could get more flexibility by "faking" a border using 4 divs for the left, right, top, and bottom edges and 4 more for the corners.

It's kind of hard to know what you mean without an example page, or screenshots of what you expect and what you're actually getting.

EDIT: I was about to edit in basically the same thing Rex M wrote. Here's another (although idealistically inferior) way to do it:

<style>
#a {
    float: left;
    position: relative;
}
div.overlay {
    opacity: 0.4;
    background-color: black;
    position: absolute;
}
#t {
    left: 0; top: 0; height: 90px; width: 450px;
}
#b {
    left: 0; top: 120px; height: 218px; width: 450px;
}
#l {
    left: 0; top: 90px; height: 30px; width: 90px;
}
#r {
    left: 120px; top: 90px; height: 30px; width: 330px;
}
</style>
<div id="a">
    <div id="t" class="overlay"></div>
    <div id="b" class="overlay"></div>
    <div id="l" class="overlay"></div>
    <div id="r" class="overlay"></div>
    <img src="http://clagnut.com/images/ithaka.jpg"&gt;
</div>
David Zaslavsky
I think what he wants is a border that is semitransparent, so the image is partially visible below it.
TM
Even though what REX M has is more "idealistic" your code is useful if you are changing the image with javascript, in which case this snippet is more useful.
Shard
A: 

To follow on what Rex M said, you'll need to change things so that the non-transparent elements aren't children of the transparent elements.

You can use absolute or relative positioning to line up your "border" with the picture, although this can often have inconsistencies between browsers.

The most painless way off the top of my head is to use javascript to get the top and left pixel locations of the image and set the top/left css properties of the border to match (and set the size of the border to that of the image).

UPDATE:

The asker showed an example of what he is trying to recreate. In the example linked, the shaded areas (the "not selected" area) of the picture is created by 4 divs.

The top and bottom divs are the full width of the image, and are set to have a height that is the difference between the top/bottom of the selection box and the top/bottom of the image respectively.

The side divs have height and width modified so that they fill in the "side areas" of the image.

The sizes are updated via a mousemove event.

TM
Why the downvote?
TM
A: 

If you want to be sure that the images have a certain color for a background, you could just as well stick a background to all IMG-elements in your stylesheet:

div#a img { background: #FFF; }

Anyhow, the filter-property in CSS should not be relied upon, as it is not part of the official specifications for CSS 2.1.

I might have misunderstood the question, though. Could you rephrase it or provide pictures of expected results?

Arve Systad
well, I would like the whole image have a dark opacity, and just the box be transparent, so I can see the original colors of the picture without any opacity throughout the box.