Can you set a border opacity in CSS?

CssOpacity

Css Problem Overview


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

border-opacity: 0.7;

If not, does anyone have an idea how I could do so without using images?

Css Solutions


Solution 1 - Css

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 - Css

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 - Css

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 https://stackoverflow.com/questions/2626039/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 - Css

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.

Solution 5 - Css

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((bordersize2)+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>

Update:

This answer is outdated, since after all this question is more than 8 years old. Today all up to date browsers support rgba, box shadows and so on. But this is a decent example how it was 8+ years ago.

Solution 6 - Css

As an alternate solution that may work in some cases: change the border-style to dotted.

Having alternating groups of pixels between the foreground color and the background color isn't the same as a continuous line of partially transparent pixels. On the other hand, this requires significantly less CSS and it is much more compatible across every browser without any browser-specific directives.

Solution 7 - Css

Other answers deal with the technical aspect of the border-opacity issue, while I'd like to present a hack(pure CSS and HTML only). Basically create a container div, having a border div and then the content div.

<div class="container">
  <div class="border-box"></div>
  <div class="content-box"></div>
</div>

And then the CSS:(set content border to none, take care of positioning such that border thickness is accounted for)

.container {
  width: 20vw;
  height: 20vw;
  position: relative;
}
.border-box {
  width: 100%;
  height: 100%;
  border: 5px solid black;
  position: absolute;
  opacity: 0.5;
}
.content-box {
  width: 100%;
  height: 100%;
  border: none;
  background: green;
  top: 5px;
  left: 5px;
  position: absolute;
}

Solution 8 - Css

One may also consider other border styles (dashed, dotted) to make the border partly fully transparent:

http://jsfiddle.net/c1rvp3ga

body {
    background: url('http://i.imgur.com/pr86mh.jpg');
}

#foo {
  border: 5px dashed #b00;
  background: #ddd;
  background-clip: padding-box;
  padding: 8px;
  width: 100px;
  margin: 30px;
}

<p id=foo>some content</p>

Solution 9 - Css

Thinking outside the box a bit here: if you are working with SVG basic shapes, you can use a combination of stroke, stroke-width (browser support > 97%), and stroke-opacity (browser support > 99%) to do essentially the same thing the OP is asking.

For example, this declaration:

circle {
    stroke: blue;
    stroke-width: 5px;
    stroke-opacity: 0.4
}

will add a translucent blue halo around the perimeter of a <circle> with #000 fill.

This JSFiddle provides a demo for several SVG basic shapes. The fiddle uses a red fill and blue stroke to highlight a major difference here between stroke and border - half the stroke-width is inside the perimeter of the basic shape.

This gives a "double border" appearance (from outside towards the center for each basic shape: blue->(blue + red = purple)->red) that would be impossible to achieve with CSS border alone (but border + outline could - see the above JSFiddle for an example using a <div>), and difficult to achieve (in the case of radial basic shapes) with a radial-gradient().

Solution 10 - Css

try this:

<h2>Snippet for making borders transparent</h2>
<div>
  <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta.
    Mauris massa. Vestibulum lacinia arcu eget nulla. <b>Lorem ipsum dolor sit amet, consectetur adipiscing elit</b>. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Curabitur sodales ligula in libero. Sed dignissim
    lacinia nunc. <i>Lorem ipsum dolor sit amet, consectetur adipiscing elit</i>. Curabitur tortor. Pellentesque nibh. Aenean quam. In scelerisque sem at dolor. Maecenas mattis. Sed convallis tristique sem. Proin ut ligula vel nunc egestas porttitor.
    <i>Lorem ipsum dolor sit amet, consectetur adipiscing elit</i>. Morbi lectus risus, iaculis vel, suscipit quis, luctus non, massa. Fusce ac turpis quis ligula lacinia aliquet. Mauris ipsum. Nulla metus metus, ullamcorper vel, tincidunt sed, euismod
    in, nibh. Quisque volutpat condimentum velit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nam nec ante. Sed lacinia, urna non tincidunt mattis, tortor neque adipiscing diam, a cursus ipsum ante quis
    turpis. Nulla facilisi. Ut fringilla. Suspendisse potenti. Nunc feugiat mi a tellus consequat imperdiet. Vestibulum sapien. Proin quam. Etiam ultrices. <b>Nam nec ante</b>. Suspendisse in justo eu magna luctus suscipit. Sed lectus. <i>Sed convallis tristique sem</i>.
    Integer euismod lacus luctus magna. <b>Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos</b>. Quisque cursus, metus vitae pharetra auctor, sem massa mattis sem, at interdum magna augue eget diam. Vestibulum
    ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Morbi lacinia molestie dui. Praesent blandit dolor. Sed non quam. In vel mi sit amet augue congue elementum. Morbi in ipsum sit amet pede facilisis laoreet. Donec lacus nunc,
    viverra nec, blandit vel, egestas et, augue. Vestibulum tincidunt malesuada tellus. Ut ultrices ultrices enim. <b>Suspendisse in justo eu magna luctus suscipit</b>. Curabitur sit amet mauris. Morbi in dui quis est pulvinar ullamcorper. </p>
</div>
<div id="transparentBorder">
  This &lt;div&gt; has transparent borders.
</div>

And here comes our magical CSS..

* {
  padding: 10pt;
  font: 13px/1.5 Helvetica Neue, Arial, Helvetica, 'Liberation Sans', FreeSans, sans-serif;
}

b {
  font-weight: bold;
}

i {
  font-style: oblique;
}

H2 {
  font-size: 2em;
}

div[id='transparentBorder'] {
  height: 100px;
  width: 200px;
  padding: 10px;
  position: absolute;
  top: 40%;
  left: 30%;
  text-align: center;
  background: Black;
  border-radius: 4px;
  border: 10pt solid rgba(0, 0, 0, 0.5);
  -moz-background-clip: border;
  /* Firefox 3.6 */
  -webkit-background-clip: border;
  /* Safari 4? Chrome 6? */
  background-clip: border-box;
  /* Firefox 4, Safari 5, Opera 10, IE 9 */
  -moz-background-clip: padding;
  /* Firefox 3.6 */
  -webkit-background-clip: padding;
  /* Safari 4? Chrome 6? */
  background-clip: padding-box;
  /* Firefox 4, Safari 5, Opera 10, IE 9 */
  text-align: center;
  margin: 0;
  color: #fff;
  cursor: pointer;
}

Check out the http://jsfiddle.net/darkdemon666/D529s/24/">Demo</a> here.

Solution 11 - Css

Since this answer is top of Google results, decided to post an updated (2021) answer for the rookies like me.

You can set a border opacity buy using an rgba color.

border:2px solid rgba(232, 69, 69,.5); /* notice the .5 */

See example fiddle here - http://jsfiddle.net/joshdane/74pybasm/33/

You can use it, and enjoy it.

There is some discussion that older browsers don't support rgba, but most people are not using older browsers anymore. If you are just learning, don't worry about supporting older browsers.

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmcbeavView Question on Stackoverflow
Solution 1 - CsskingjeffreyView Answer on Stackoverflow
Solution 2 - CssPedro L.View Answer on Stackoverflow
Solution 3 - CssLeeView Answer on Stackoverflow
Solution 4 - CssMark2090View Answer on Stackoverflow
Solution 5 - CssBreezerView Answer on Stackoverflow
Solution 6 - CssStephan SamuelView Answer on Stackoverflow
Solution 7 - CssRBkView Answer on Stackoverflow
Solution 8 - CssFriedrichView Answer on Stackoverflow
Solution 9 - CssCSSBurnerView Answer on Stackoverflow
Solution 10 - CssA MalikView Answer on Stackoverflow
Solution 11 - CssJoshua DanceView Answer on Stackoverflow