Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transparent border over image

Tags:

css

I am trying to create a transparent border for my image and place it over the image using CSS.

For example please see the image below:

enter image description here

To achieve this I tried the following code but I am facing the following problems:

  1. The border is not over the image; its around the image and not allowing the image to fit 100% inside the parent div

  2. To Make the border transparent I used "opacity" but its making the image transparent too which I don't want.

You can check the code live here: http://jsfiddle.net/6GK45/

I could create a div and made the border color transparent and then place it over the image but the problem is the width of my image is fixed (277px) but the height is not. So this will not work for me.

Could you please tell me how to create the transparent image border and place it over the image just like in the image above.?

HTML:

<div class="box" >  
   <img class="lightbox" src="myimage.jpg" />
   This is text
</div>

CSS

.box {
    width:277px; 
    background:#FCFBDF;    
}

.lightbox {
    border: 5px solid red;
    z-index:999; 
    opacity:0.3; 
}

img {
    width:277px;
}
like image 796
black_belt Avatar asked Sep 26 '13 19:09

black_belt


People also ask

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.

How do I add a transparent border to an image in CSS?

In CSS, we can create a transparent border by using the border property in a nested div tag.

What is transparent border?

CSS Transparent border means that the behind object can be seen if we apply the border as transparent or rgba. Transparent border property beautifies the HTML page by making behind objects distinctly visible.

How do you make a border radius transparent?

If you want your border to be transparent (or semitransparent), and you are setting a black background, you need to set the background limited to the inner part, so that the border can be seen. Show activity on this post. You can use a container to provide a border offset if you need it.


1 Answers

How's this - it uses :after to create a pseudo-element which places the border on top of the image, not outside it. http://jsfiddle.net/6GK45/8/

.imgWrap:after{
    content:"";
    position:absolute;
    top:0; bottom:0; left:0; right:0;
    opacity:0.5;
    border:5px solid red;
}

UPDATE: If it's important to preserve the ability to right-click on the image, you can do it like this with an additional wrapper: http://jsfiddle.net/6GK45/24/

like image 80
andi Avatar answered Sep 19 '22 17:09

andi