创建一个尖头的div

时间:2016-06-04 22:41:34

标签: html css

我想创建一个带有图像和文本的div,看起来像这样。

enter image description here

我设法在这里找到了这样的东西:

JSFiddle of pointed div

  .triangle-down {
    background: white;
    display: inline-block;
    height: 125px;
    margin-left: 20px;
    margin-bottom: 55px;
    position: relative;
    width: 200px;
    cursor: pointer;
    border: red solid 2px;
  }
  img {
    margin: 10px;
  }
  .triangle-down:before {
    border-top: 20px solid red;
    border-left: 101px solid transparent;
    border-right: 101px solid transparent;
    content: "";
    height: 0;
    left: -1px;
    position: absolute;
    top: 127px;
    width: 0;
  }
  .triangle-down:after {
    border-top: 20px solid white;
    border-left: 100px solid transparent;
    border-right: 100px solid transparent;
    content: "";
    height: 0;
    left: 0;
    position: absolute;
    top: 125px;
    width: 0;
  }
<div class="triangle-down">
  <img src="http://placehold.it/180x105">
</div>

我遇到的问题是:

(1)当光标穿过有助于创建点的透明边框时,光标会转到形状外部的指针。如果指针只出现在形状的可见轮廓内,我更喜欢它。

(2)有更好的方法吗?我看着尝试旋转div来创建点,因为我认为这将解决指针问题,但我无法弄清楚如何以这种方式创建具有正确比例的等腰三角形形状。这也允许我应用边框来创建轮廓,而不是像JSFiddle中那样覆盖两个三角形。有关详情,请参阅此帖子 - Speech bubble with arrow

1 个答案:

答案 0 :(得分:1)

以下是使用transform: rotate

的版本

/*Down pointing*/
.triangle-down {
  background: white;
  display: inline-block;
  height: 125px;
  margin-left: 20px;
  margin-bottom: 55px;
  position: relative;
  width: 200px;
  cursor: pointer;
  border: red solid 2px;
}
img {
  position: relative;
  margin: 10px;
  z-index: 1
}
.triangle-down:before,
.triangle-down:after {
  border-bottom: 2px solid red;
  background: white;
  content: '';
  height: 50px;
  left: 5px;
  position: absolute;
  top: 98px;
  width: 54%;
  transform: rotate(22deg);
  z-index: 0;
}
.triangle-down:after {
  left: auto;
  right: 5px;
  transform: rotate(-22deg);
}
<div class="triangle-down">
  <img src="http://placehold.it/180x105">
</div>