Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you set a border opacity in CSS?

Tags:

css

opacity

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?

like image 649
mcbeav Avatar asked Oct 31 '10 05:10

mcbeav


People also ask

How do you make a transparent border in CSS?

Step 1: Create a div tag. Step 2: Specify the border-style property to be double to set two borders around the box. Step 3: Set the background-clip property to padding-box which clips the background color to the padding of the element.

How do you add a blur effect to a border in CSS?

Blurred Border If you want to visually blur the border of an element, use a pseudo-element, put it behind the parent element with z-index , apply a transparent border, add the background-clip , filter , and clip-path properties.

How do I make an image border transparent?

Click "Edit" and select "Fill" to get options to fill the border with a color or pattern. You'll also get the option to set the opacity, which will determine how transparent your border is. Set the opacity to 100 percent for a fully transparent border, or 50 percent for a semi-transparent border.

Is there a transparent color in CSS?

The CSS color name transparent creates a completely transparent color. Using rgba or hsla color functions, that allow you to add the alpha channel (opacity) to the rgb and hsl functions. Their alpha values range from 0 - 1.


1 Answers

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.

like image 167
kingjeffrey Avatar answered Oct 05 '22 23:10

kingjeffrey