Can you set a border opacity in CSS?

Solution 1:

Unfortunately the opacity property 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);
    -webkit-background-clip: padding-box; /* for Safari */
    background-clip: padding-box; /* for IE9+, Firefox 4+, Opera, Chrome */
}

For extremely old browsers that don't support rgba (IE8 and older), 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);
    -webkit-background-clip: padding-box; /* for Safari */
    background-clip: padding-box; /* for IE9+, Firefox 4+, Opera, Chrome */
}

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).

I've added background-clip: padding-box; to the examples above to ensure the border remains transparent even if a solid background color is applied.

Solution 2:

It's easy, use a solid shadow with 0 offset:

#foo {
  border-radius: 1px;
  box-shadow: 0px 0px 0px 8px rgba(0,0,0,0.3);       
}

Also, if you set a border-radius to the element, it gives you pretty rounded borders

jsFiddle Demo

enter image description here

Solution 3:

As others have mentioned, CSS3 supports the rgba(...) syntax to specify a border color with an opacity (alpha) value.

Here's a quick JSFiddle demo 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 CSS RGBA border / background alpha double, 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.

Solution 4:

If you check your CSS coding with W3C validator, you will see if your CSS code is acceptable, even if it worked in the major browsers.

Creating a transparent border via CSS, as written above,

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

is not accepted by W3C standards, not even for CSS3. I used the direct input validator with the following CSS code,

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

The results were,

Value Error : border Too many values or values are not recognized : 1px solid rgba(255,0,0,0.5 )

Unfortunate that the alpha value (the letter "a" at the end of "rgb") is not accepted by W3C as part of the border color values as yet. I do wonder why it is not standardized, since it works in all browsers. The only hitch is whether you want to stick to W3C standards or step aside from it to create something in CSS.

To use W3C online CSS validator / Direct Input.

Always a good idea to use a validator to check your work, it really helps finding small or even large errors in coding when your going cross-eyed after hours of coding work.