【问题标题】:Box with arrow without using pseudo-elements没有使用伪元素的带箭头的框
【发布时间】:2019-02-20 05:11:10
【问题描述】:

我需要为工具提示制作一个带箭头的框,但我不能使用伪元素,因为:

  1. 框背景有点透明
  2. 有边框

这是一个例子:

.box {
  margin: 60px 0 0 0;
  width: 250px;
  height: 50px;
  background-color: rgba(255, 144, 89, 0.5);
  border-radius: 5px;
  position: relative;
  border: 2px solid #ff6e26;
}

.box:after,
.box:before {
  bottom: 100%;
  left: 50%;
  border: solid transparent;
  content: " ";
  height: 0;
  width: 0;
  position: absolute;
  pointer-events: none;
}

.box:after {
  border-color: rgba(136, 183, 213, 0);
  border-bottom-color: rgba(255, 144, 89, 0.5);
  border-width: 10px;
  margin-left: -10px;
}

.box:before {
  border-color: rgba(194, 225, 245, 0);
  border-bottom-color: #ff6e26;
  border-width: 12px;
  margin-left: -12px;
}
<div class="box"></div>

https://codepen.io/Masoudm/pen/qgvJGX

正如您所见,当我使背景透明时,它不适用于箭头,因为我已经在它后面使用了 ::before 作为它的边框。我想知道是否有另一种方法可以让我保持盒子大小动态。

更新:

盒子应该是这样的(除了顶部的曲线)

【问题讨论】:

  • 你能添加任何你想要的箭头应该是什么样子的图像吗?
  • @AbhishekPandey 我更新了图片。

标签: html css


【解决方案1】:

基于previous answer,我将稍微调整代码以获得透明背景。有两个主要技巧。伪元素一半着色以避免与主元素相交,并在主元素上使用渐变来创建边框顶部并为伪元素创建孔:

body {
 margin:0;
 background-image:linear-gradient(to right,yellow,pink);
}

.box {
  border: 2px solid red;
  border-top:transparent; /*make border-top transparent*/
  margin: 50px;
  height: 50px;
  position:relative;
  /* Use gradient to mimic the border top with a transparent gap */
  background:
    linear-gradient(red,red) left top /calc(50% - 10px*1.414) 2px,
    linear-gradient(red,red) right top/calc(50% - 10px*1.414) 2px,
    rgba(0,255,0,0.4);
  background-repeat:no-repeat;
}

.box:before {
  content: "";
  position: absolute;
  width: 20px;
  height: 20px;
  border-top: 2px solid red;
  border-left: 2px solid red;
  top: -11px;
  left: calc(50% - 11px);
  transform: rotate(45deg);
  background:linear-gradient(-45deg,transparent 50%,rgba(0,255,0,0.4) 50%);
}
<div class="box">

</div>

【讨论】:

  • 谢谢,这正是我想要的。
【解决方案2】:

* {
    box-sizing: border-box;
    font-family: inherit;
}

html {
    font-size: 62.25%;

}

body {
    padding: 50px;
}


.outter {
    width: 200px;
    height: 100px;
    position: relative;
}

.box {
    padding: 20px;
    width: 100%;
    height: 100%;
    background: rgba(255, 68, 0, 0.568);
    border: 3px solid orangered;
    border-radius: 5px;
    clip-path: polygon(0 0,45% 0,45% 10px,calc(45% + 15px) 10px,calc(45% + 15px) 0,100% 0,100% 100%,0 100%,0 0)
}

.arrow {
    width: 15px;
    height: 8px;
    background: rgba(255, 68, 0, 0.568);
    transform: translate(-67%, 100%);
    position: absolute;
    left: 50%;
    bottom: 98%;
}

.arrow::after {
    border: 3px solid transparent;
    border-left-color: orangered;
    border-top-color: orangered;
    border-right: 0px solid transparent;
    border-bottom: 0px solid transparent;
    width: 11px;
    height: 11px;
    position: absolute;
    left: 0;
    bottom: 34%;
    content: '';
    transform: rotate(45deg);
    background: linear-gradient(134deg,rgba(255, 68, 0, 0.56) 0%,rgba(255, 68, 0, 0.56) 50%,transparent 50%, transparent 100%);
}
<div class="outter">
    <div class="arrow"></div>
    <div class="box"></div>
</div>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-24
    • 2019-06-02
    • 1970-01-01
    • 1970-01-01
    • 2013-04-17
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多