简体   繁体   English

在CSS中添加具有透明背景的图像边框

[英]Adding border of an image with transparent background in css

I'm converting a PSD to HTML and having trouble with a border. 我正在将PSD转换为HTML,并且边框出现问题。 Is there any way to add a border to an image with transparent background? 有什么方法可以向具有透明背景的图像添加边框吗? Let's say for example I have this image. 举例来说,我有这张图片。

胡萝卜

And so there's a caret on the image and I want to do something like this.(See below): 因此,图像上有一个尖号,我想做这样的事情(见下文):

胡萝卜与图像边框

How can I make the white border in the image? 如何在图像中形成白色边框?

You can use a png image with transparent background. 您可以使用具有透明背景的png图像。

http://jsfiddle.net/BWxfv/ http://jsfiddle.net/BWxfv/

#caret {
    position: absolute;
    left: 0;
    top: 0;
    z-index: 2;
}

Here's a way using an extra (required unfortunately) element and a bunch of pseudo elements. 这是使用一个额外的(不幸的是必需的)元素和一堆伪元素的方法。

I've used different colors so you can see the two 'caret's. 我使用了不同的颜色,因此您可以看到两个“插入符号”。

Codepen Demo Codepen演示

HTML 的HTML

<div class="wrapper">
  <img src="http://lorempixel.com/output/nightlife-q-c-500-200-9.jpg" alt="" />
  <div class="main">
    <div class="caret-border"></div> <!---extra div -->
  </div>
</div>

CSS 的CSS

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  background: lightgrey;
}

.wrapper {
  width:650px;
  margin: 10px auto;
  border:1px solid grey;
}

.wrapper img {
  display: block;
  width:100%;
  height:auto;
}

.main {
  position: relative;
  min-height:150px;
  background: #bada55;
}
.main:before,
.main:after {
  position: absolute;
  content:"";
  height:0;
  width:calc(50% - 16px);
  height:0px;
  transform:translateY(-100%);
  //top:-16px;
  z-index:2;
  border-style:solid;

}

.main:before {
  left:0;
  border-width: 0px 16px 16px 0;
  border-color:transparent transparent green transparent;
}

.main:after {
  right:0;
  border-width: 0px 0px 16px 16px;
  border-color:transparent transparent green transparent;
}

.caret-border {
  height:0px;
  background: red;
  position: absolute;
  width:100%;

}

.caret-border:before,
.caret-border:after{
  position: absolute;
  content:"";
  height:0px;
  top:0;
  width:calc(50% - 14px);
  z-index:1;
  border-style:solid;
  transform:translateY(-100%);
  //top:-18px;
}

.caret-border:before {
  left:0;
  border-width: 0px 16px 18px 0;
  border-color:transparent transparent white transparent;
}

.caret-border:after{
  right:0;
  border-width: 0px 0px 18px 16px;
  border-color:transparent transparent white transparent;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM