nimi
nimi

Reputation: 1017

CSS Border on PNG image with transparent parts

Im trying to add a border on a PNG image I have (Example included). The thing is that when I add the border currently it adds it on a box shape around all the image and not on the exact vector (Meaning it includes the transparent parts in the image).

Is there any possible way to setup the configuration of the border that it won't consider the transparent areas. (Even if not in CSS... Maybe HTML5/JS?)

Example image

Example image with border

Upvotes: 64

Views: 85325

Answers (6)

Simon Nitzsche
Simon Nitzsche

Reputation: 773

I extended the top answer a bit which is better for my use.

body {
    background: black;
}
img.outlined {
    -webkit-filter: drop-shadow(2px 2px 0 white)
                    drop-shadow(-2px 2px 0 white)
                    drop-shadow(2px -2px 0 white)
                    drop-shadow(-2px -2px 0 white);

    filter: drop-shadow(2px 2px 0 white)
            drop-shadow(-2px 2px 0 white)
            drop-shadow(2px -2px 0 white)
            drop-shadow(-2px -2px 0 white);
}
<img class="outlined" src="https://upload.wikimedia.org/wikipedia/commons/a/a9/Example_title.png" />

<img class="plain" src="https://upload.wikimedia.org/wikipedia/commons/a/a9/Example_title.png" />

If someone still needs it.

Upvotes: 43

Tamal Chowdhury
Tamal Chowdhury

Reputation: 542

I implemented this technique with some tweaks and here are the results:

enter image description here

enter image description here

As you can see the circular images look good, so are the other shaped icons.

Here's my css, I added four rules for four sides, each with 5 pixels.

filter: drop-shadow(5px 0 0 white) 
        drop-shadow(0 5px 0 white)
        drop-shadow(-5px 0 0 white) 
        drop-shadow(0 -5px 0 white);

The prefixes are gonna added automatically since I am using scss.

body {
  background: black;
  height: 100vh;
  display: grid;
  place-items: center;
  overflow: hidden;
}

img {
  user-select: none;
  height: auto;
  width: 100px;
}

.stroke {
  filter: drop-shadow(5px 0 0 white) 
  drop-shadow(0 5px 0 white)
  drop-shadow(-5px 0 0 white) 
  drop-shadow(0 -5px 0 white);
}
<img class="stroke" src="data:image/svg+xml;base64,PHN2ZyBpZD0iTGF5ZXJfMSIgZGF0YS1uYW1lPSJMYXllciAxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAxMjIuODggMTEzLjQxIj48ZGVmcz48c3R5bGU+LmNscy0xe2ZpbGw6IzA2NWZkNDt9PC9zdHlsZT48L2RlZnM+PHRpdGxlPmJsdWUtbGlrZS1idXR0b248L3RpdGxlPjxwYXRoIGNsYXNzPSJjbHMtMSIgZD0iTTQuMjksNDcuNjRoMTkuM0E0LjMxLDQuMzEsMCwwLDEsMjcuODgsNTJWMTA5LjFhNC4zMSw0LjMxLDAsMCwxLTQuMjksNC4zMUg0LjI5QTQuMzEsNC4zMSwwLDAsMSwwLDEwOS4xVjUyYTQuMzEsNC4zMSwwLDAsMSw0LjI5LTQuMzFaTTU5LDQuNzdjMi4yNy0xMS40OCwyMS4wNy0uOTEsMjIuMzEsMTcuNkE3OS44Miw3OS44MiwwLDAsMSw3OS42OCw0MmgyNi44N2MxMS4xNy40NCwyMC45Miw4LjQ0LDE0LDIxLjU4LDEuNTcsNS43MiwxLjgxLDEyLjQ0LTIuNDUsMTUuMDkuNTMsOS0yLDE0LjY0LTYuNjUsMTkuMDYtLjMxLDQuNTItMS4yNyw4LjUzLTMuNDUsMTEuNjItMy42MSw1LjA5LTYuNTQsMy44OC0xMi4yMiwzLjg4SDUwLjQ1Yy03LjE5LDAtMTEuMTEtMi0xNS44MS03Ljg4VjU0LjgxQzQ4LjE2LDUxLjE2LDU1LjM1LDMyLjY2LDU5LDIwLjUxVjQuNzdaIi8+PC9zdmc+">

Upvotes: 23

Samantha Bretous
Samantha Bretous

Reputation: 189

This blog explains how to create a smooth outline. You can update the calculateStrokeTextCSS function to use this string.

css += "drop-shadow(calc($stroke-width-img * " +
  cos +
  ") calc($stroke-width-img * " +
  sin +
  ") 0 $stroke-color-img)"

https://codepen.io/inegoita/pen/ZEQVPLx?editors=1010

Upvotes: 0

MSC
MSC

Reputation: 3386

Three years on the question is still valid. As I (originally) wanted a thicker stroke, I ended up using 8 drop shadows (one for each point of the compass) to get it looking just so.

Strangely, using 8 shadows with an x- and y- offset of 1px yields an outline which looks about 5px wide, but introducing transparency into the colour seems to help dial this back to a slightly soft but quite attractive result:

img {
    --stroke-pos: 1px;
    --stroke-neg: -1px;
    --stroke-color: rgba(0, 255, 0, 0.2);
    filter: drop-shadow(var(--stroke-pos) 0 0 var(--stroke-color)) 
      drop-shadow(var(--stroke-neg) 0 var(--stroke-color))
      drop-shadow(0 var(--stroke-pos) 0 var(--stroke-color))
      drop-shadow(0 var(--stroke-neg) 0 var(--stroke-color))
      drop-shadow(var(--stroke-pos) var(--stroke-pos) 0 var(--stroke-color)) 
      drop-shadow(var(--stroke-pos) var(--stroke-neg) 0 var(--stroke-color))
      drop-shadow(var(--stroke-neg) var(--stroke-pos) 0 var(--stroke-color))
      drop-shadow(var(--stroke-neg) var(--stroke-neg) 0 var(--stroke-color));   
}

As you can see, CSS variables come in handy here (or Sass / Less).

Upvotes: 10

undefined
undefined

Reputation: 4135

As of now (January 31st 2015) there is a way to do that without using canvas, with pure CSS, and with only 2 lines of code.

The trick is using the css filter and -webkit-filter properties to draw two drop shadows with no blur, one for the positive axis and one for the negative, which will wrap around the image, which will provide the (hopefully) desired effect.

Note: css filters are not at all supported in IE (let's hope Spartan does better), here is a compatibility table.

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

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="https://i.sstatic.net/Ju44K.jpg" width="250">

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

With that in mind, here is the perfected border snippet (fiddle) with just a really tiny value tweak.

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="https://i.sstatic.net/Ju44K.jpg" width="250">

That should cover borders pretty well, but we can still have more fun with this, look at this awesome lightness 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="https://i.sstatic.net/Ju44K.jpg" width="250">

Hope this helps anyone wondering about the possibility of a wrap-around border for semitransparent images!

Upvotes: 104

Darren
Darren

Reputation: 7

Came across needing to do this myself - came up with this hack. A series of overlaid images behind my original that were slightly out of step with each other. Context ctx3 is a copy of the original image and this would replicate a white silhouette behind the original several times.

      ctx3.shadowColor = "rgba(255,255,255,1)";
      ctx3.globalCompositeOperation = 'source-over';
      ctx3.shadowOffsetX = 2;
      ctx3.shadowOffsetY = 2;
      ctx3.shadowBlur = 0;
      ctx3.drawImage(YourImageSource,0,0);
      ctx3.shadowOffsetX = -2;
      ctx3.shadowOffsetY = -2;
      ctx3.shadowBlur = 0;
      ctx3.drawImage(YourImageSource,0,0);
      ctx3.shadowOffsetX = -2;
      ctx3.shadowOffsetY = 2;
      ctx3.shadowBlur = 0;
      ctx3.drawImage(YourImageSource,0,0);
      ctx3.shadowOffsetX = 2;
      ctx3.shadowOffsetY = -2;
      ctx3.shadowBlur = 0;
      ctx3.drawImage(YourImageSource,0,0);
      ctx3.shadowOffsetX = 0;
      ctx3.shadowOffsetY = 2;
      ctx3.shadowBlur = 0;
      ctx3.drawImage(YourImageSource,0,0);
      ctx3.shadowOffsetX = 0;
      ctx3.shadowOffsetY = -2;
      ctx3.shadowBlur = 0;
      ctx3.drawImage(YourImageSource,0,0);
      ctx3.shadowOffsetX = 2;
      ctx3.shadowOffsetY = 0;
      ctx3.shadowBlur = 0;
      ctx3.drawImage(YourImageSource,0,0);
      ctx3.shadowOffsetX = -2;
      ctx3.shadowOffsetY = 0;
      ctx3.shadowBlur = 0;
      ctx3.drawImage(YourImageSource,0,0);

Upvotes: -2

Related Questions