tags:

views:

39

answers:

3

Is there a straight forward way to make the border of an element semi-transparent with something like say

border-opacity:0.7;

?

If not anyone have an idea how i could do so without using images?

A: 

not as far as i know there isn't what i do normally in this kind of circumstances is create a block beneath with a bigger size((bordersize*2)+originalsize) and make it transparent using

filter:alpha(opacity=50);
-moz-opacity:0.5;
-khtml-opacity: 0.5;
opacity: 0.5;

here is an example
#main{
    width:400px;
    overflow:hidden;
    position:relative;
}
.border{
width:100%;
position:absolute;
height:100%;
background-color:#F00;
filter:alpha(opacity=50);
-moz-opacity:0.5;
-khtml-opacity: 0.5;
opacity: 0.5;

}
.content{
    margin:15px;/*size of border*/
    background-color:black;
}
<div id="main">
    <div class="border">
    </div>
    <div class="content">
    testing
    </div>
</div>
Breezer
Yeah that is what i ended up doing actually, it just sucks fudging with the positioning of the elements.
mcbeav
added an example so you could see clearer how i was thinking :)
Breezer
it *can* be done - but not with broad cross-browser support. However, chances are good that any browser that supports css opacity on background color would also support `rgba(...)` in border colors. [you can try it out here](http://jsfiddle.net/pqH4r/).
Lee
@Lee, IE supports the "filter" opacity, but not any form of rgba (until IE9).
kingjeffrey
+3  A: 

Unfortunately the opacity element makes the whole element (including any text) semi-transparent. The best way to make the border semi-transparent is with the rgba color format. For example, this would give a red border with 50% opacity:

div {
    border: 1px solid rgba(255, 0, 0, .5);
}

The problem with this approach is that some browsers do not understand the rgba format and will not display any border at all if this is the entire declaration. The solution is to provide two border declarations. The first with a fake opacity, and the second with the actual. If a browser is capable, it will use the second, if not, it will use the first.

div {
    border: 1px solid rgb(127, 0, 0);
    border: 1px solid rgba(255, 0, 0, .5);
}

The first border declaration will be the equivalent color to a 50% opaque red border over a white background (although any graphics under the border will not bleed through).

kingjeffrey
yeah and then we're back to the problem he first had ^^"I thought about using rgba for the border-color, but it works very poorly across modern browsers."while my solutions works in pretty much all browsers
Breezer
Actually, rgba works excellently in modern browsers (unless you think IE6-8 are "modern").
kingjeffrey
well they're suppose to be :Pand when you got 50%+ using them you should see to it that it works for ie aswell imo at least for ie7+
Breezer
And that is why there is the "fake opacity" fall back. My life as a web designer became a lot easier when I accepted that not every browser needs to render identically. If they support `border-radius`, then they get rounded corners. If not, they don't. The content is still accessible, it still looks fine, it just looks better if they are using a capable browser. I've never had one client complain about this in the past 1.5 years of operating this way on every project.
kingjeffrey
+1  A: 

As others have mentioned: CSS-3 says that you can use the rgba(...) syntax to specify a border color with an opacity (alpha) value.

here's a quick example if you'd like to check it.

  • It works in Safari and Chrome (probably works in all webkit browsers).

  • It works in Firefox

  • I doubt that it works at all in IE, but I suspect that there is some filter or behavior that will make it work.

There's also this stackoverflow post, which suggests some other issues--namely, that the border renders on-top-of any background color (or background image) that you've specified; thus limiting the usefulness of border alpha in many cases.

Lee
thanks. yeah, i stopped worrying about I.E. probably sounds bad, but its just a pathetic browser. I just direct I.E. users to a mobile version. Too bad I.E. still holds over 50% of the market share. Not that this comment has anything to do with the question
mcbeav
The border issue can be cured with `background-clip: padding-box;` (and until that is supported, you can use the -webkit and -moz vendor extensions).
kingjeffrey
@kingjeffrey - that's good to know--thanks!.
Lee
@Lee I've added this answer to the question you've linked to.
kingjeffrey