How can I draw a border around a translucent image? (css)


not clear

I'd like to know how to draw a border around a semi-transparent image, that is, I don't need either a box-shaped border nor a border-radius, but a border that really fits the shape of the image itself.

According to this article, this is not possible, and this is only recommended for editing images in some photo editing programs such as Photoshop.

But what if the image input is not mine? This software option won't work if I need to process a series of user-entered images onto my website and add borders.

One way to fix that is to use canvas, but isn't there a simple, easy css way to do this? thanks.

not clear

As of now ( Jan 31, 2015 ), there is a way to do this without using canvas, using pure CSS and only two lines of code.

The trick is to use css filterand -webkit-filterproperties to draw two shadows without blur, one for the positive axis and one for the negative axis, that will wrap around the image, giving the ( hopefully ) desired effect.

Note : IE doesn't support css filters at all (hope Spartan does better), this is a compatibility table . (Thanks to web-tiki )

The first snippet (the fiddle ) will apply the simplest possible border .

img {
  -webkit-filter: drop-shadow(1px 1px 0 black)
                  drop-shadow(-1px -1px 0 black);
  filter: drop-shadow(1px 1px 0 black) 
          drop-shadow(-1px -1px 0 black);
}

body {
  background-color: lightcoral;
}
<img src="http://i.imgur.com/GZoXRjS.png" width="250">

As you can see, some images (like this awesome baymax render ) need more tweaking and you can see that the right border is smaller than the left border.

With that in mind, here is the perfect border code snippet ( fiddle ), with only minor value tweaks.

img {
  -webkit-filter: drop-shadow(2px 1px 0 black)
                  drop-shadow(-1px -1px 0 black);
  filter: drop-shadow(2px 1px 0 black) 
          drop-shadow(-1px -1px 0 black);
}

body {
  background-color: khaki;
}
<img src="http://i.imgur.com/GZoXRjS.png" width="250">

That should cover the border nicely, but we can still have more fun with it, check out this great luminance effect snippet ( fiddle ).

img{
    -webkit-filter: drop-shadow(1px 1px 0 black) 
                    drop-shadow(-1px -1px 0 white);
    filter:drop-shadow(1px 1px 0 black) 
           drop-shadow(-1px -1px 0 white);
}

body{
    background-color:lightblue;
}
<img src="http://i.imgur.com/GZoXRjS.png" width="250">

Hope this helps anyone wondering that semi-transparent images have wrapping borders!

Related


How can I draw a border around a translucent image? (css)

not clear I'd like to know how to draw a border around a semi-transparent image, that is, I don't need either a box-shaped border nor a border-radius, but a border that really fits the shape of the image itself. According to this article, this is not possible,

How can I set a transparent border around an image using CSS?

microphone I'm trying to create an oval shape on hover when someone hovers over a link. By doing this, I lost the image that was part of the link. I know what I'm doing wrong but what? .pagelinks { float: right; margin-right: 48%; display: inline-block;

How can I set a transparent border around an image using CSS?

microphone I'm trying to create an oval shape on hover when someone hovers over a link. By doing this, I lost the image that was part of the link. I know what I'm doing wrong but what? .pagelinks { float: right; margin-right: 48%; display: inline-block;

How can I remove the border around the image?

username How can I remove the border around the square? Exterior: my HTML: <input id="black" type="image" src="html5-canvas-drawing-app/images/color-swatch-brown.png" onClick="changeColorBlack()"> my css: #black{ border:none; outline:none; background:none; pa

How can I set a border around these divs using CSS?

Ben I'm trying to make a progress indicator for a Gantt chart showing actual progress compared to target progress like this: The black bar is the goal and the red bar is the actual progress. The sample code (generated on the fly) is this: <div style='border: 1

How can I set a border around these divs using CSS?

Ben I'm trying to make a progress indicator for a Gantt chart showing actual progress compared to target progress like this: The black bar is the goal and the red bar is the actual progress. The sample code (generated on the fly) is this: <div style='border: 1

How can I put a border around the icon? (HTML/CSS)

tintin islam What is the code for this kind of boundary Sumet Patel Try this Html <div class="wrapper"> <div id="container_2"><img src="https://cdn1.iconfinder.com/data/icons/iconbeast-lite/30/crown.png"/></div><div id="container_2"><img src="https://cdn1.ic

How can I draw this border?

User 2071969 How to draw this gradient border (border fade out transparent) and radial gradient background? my code: Drawable/bg.xml Radial gradient code is here Drawable/border.xml <item> <shape android:shape="rectangle

How can I draw a border around the viewport using GL_LINES?

Dng I can't get the shader to run on the periphery of the texture. I will use https://learnopengl.com/ and draw line primitives with indexed vertices. The problem is that only the right and bottom lines are drawn, the top and left are missing. I wrote a small

How can I draw a border around the viewport using GL_LINES?

Dng I can't get the shader to run on the periphery of the texture. I will use https://learnopengl.com/ and draw line primitives with indexed vertices. The problem is that only the right and bottom lines are drawn, the top and left are missing. I wrote a small

How can I draw a border around the viewport using GL_LINES?

Dng I can't get the shader to run on the periphery of the texture. I will use https://learnopengl.com/ and draw line primitives with indexed vertices. The problem is that only the right and bottom lines are drawn, the top and left are missing. I wrote a small

How can I draw a border around the viewport using GL_LINES?

Dng I can't get the shader to run on the periphery of the texture. I will use https://learnopengl.com/ and draw line primitives with indexed vertices. The problem is that only the right and bottom lines are drawn, the top and left are missing. I wrote a small

How can I draw a border around the viewport using GL_LINES?

Dng I can't get the shader to run on the periphery of the texture. I will use https://learnopengl.com/ and draw line primitives with indexed vertices. The problem is that only the right and bottom lines are drawn, the top and left are missing. I wrote a small

How can I draw a border around the viewport using GL_LINES?

Dng I can't get the shader to run on the periphery of the texture. I will use https://learnopengl.com/ and draw line primitives with indexed vertices. The problem is that only the right and bottom lines are drawn, the top and left are missing. I wrote a small

How to draw half of a border around a box using CSS

Azan Khan I have a design that has to draw half a border around text. The method I'm using to hide half of the box's border forces me to use an absolutely positioned child that goes out of the flow of the relatively positioned parent. The result is that when t

How to draw half of a border around a box using CSS

Azan Khan I have a design that has to draw half a border around text. The method I'm using to hide half of the box's border forces me to use an absolutely positioned child that goes out of the flow of the relatively positioned parent. The result is that when t

How to draw half of a border around a box using CSS

Azan Khan I have a design that has to draw half a border around text. The method I'm using to hide half of the box's border forces me to use an absolutely positioned child that goes out of the flow of the relatively positioned parent. The result is that when t

How to draw half of a border around a box using CSS

Azan Khan I have a design that has to draw half a border around text. The method I'm using to hide half of the box's border forces me to use an absolutely positioned child that goes out of the flow of the relatively positioned parent. The result is that when t

How to draw border around UILabel?

Yep Is it possible for UILabel to draw border around itself? This is useful for debugging text position, viewing the position and the actual size of the label. Vladimir You can set the border of the label via its underlying CALayer property: #import <QuartzCor

How to draw a border around the canvas

Valla Doharis I am drawing an image on a canvas with a white background color. I would like to draw a border around the canvas but am unable to do so. Here is my code: canvas.width = image.width; canvas.height = image.height; var context = canvas.getContext('2