【问题标题】:Triangle shaped pointer / border on a horizontal line水平线上的三角形指针/边框
【发布时间】:2015-01-06 09:39:58
【问题描述】:

我正在尝试在水平线上创建三角形指针/边框。

这是我想要实现的一个示例:

我尝试操作 div 的上边框,但到目前为止我所做的一切都不起作用。

【问题讨论】:

标签: css css-shapes


【解决方案1】:

有几种方法可以实现这一点,这可能取决于您的布局。一种解决方案是使用两侧有边框的旋转元素。

.triangle {
  background: green;
  border: 2px solid white;
  border-width: 2px 2px 0 0;
  transform: rotate(-45deg);
}

.box {
  background: green;
  width: 400px;
  height: 80px;
  position: relative;
}

.line {
  height: 39px;
  border-bottom: 2px solid white;
}

.triangle {
  background: green;
  border: 2px solid white;
  border-width: 2px 2px 0 0;
  transform: rotate(-45deg);
  position: relative;
  left: 300px;
  top: 28px;
  width: 20px;
  height: 20px;
}
<div class="box">
  <div class="line">
    <div class="triangle">
      </div>
    </div>
  </div>

【讨论】:

    【解决方案2】:

    您可以使用:before:after 伪元素:

    body{
        background:black;
    }
    div{
        border-top:1px solid green;
        position:relative;
        margin-top: 100px;
    }
    
    div:before{
        content: '';
        position:absolute;
        left: calc(90% - 20px);
        top: -9px;
        width: 0;
        height: 0;
        border-style: solid;
        border-width: 0 10px 10px 10px;
        border-color: transparent transparent black transparent;
        z-index: 10;
    }
    
    div:after{
        content: '';
        position:absolute;
        right:10%;
        top: -10px;
        width: 0;
        height: 0;
        border-style: solid;
        border-width: 0 10px 10px 10px;
        border-color: transparent transparent green transparent;
    }
    &lt;div&gt;&lt;/div&gt;

    基本上有两个三角形,一个是z-index较大的背景颜色,另一个是边框颜色。

    JSFiddle

    更新

    正如@misterManSam 提到的,你可以不使用z-index - fiddle 来实现它

    【讨论】:

    • 这个解决方案也很棒。如果我只能接受两个答案,非常感谢!
    • +1 表示没有额外的、不必要的标记。将样式元素保留在 CSS 中,就像@AlexanderLomov 的答案一样 :)
    • @Vucko - Here is an alternative 我是根据您的回答制作的。它更类似于问题中的图片,并且不包括实际上不需要的 calc() :) 尽管它确实依赖于转换。根本不需要 z-index。
    • @misterManSam 你的解决方案也不错,不需要添加额外的标记:)
    • @misterManSam 我已经用你的小提琴更新了我的答案:)
    【解决方案3】:

    .box {
      margin-top: 100px;
      height: 50px;
      background: green;
      position: relative;
    }
    
    .box .line {
      position: absolute;
      top: 50%;
      left: 5px;
      right: 5px;
      margin-top: -1px;
      height: 2px;
      background: white;
    }
    
    .box .triangle {
      position: absolute;
      width: 0;
      height: 0;
      border-bottom: 8px solid white;
      border-left: 8px solid transparent;
      border-right: 8px solid transparent;
      border-top: 8px solid transparent;
      top: 50%;
      right: 50px;
      margin-top: -15px;
    }
    
    .box .triangle .inner {
      position: absolute;
      top: -4px;
      left: -6px;
      width: 0;
      height: 0;
      border-bottom: 6px solid green;
      border-left: 6px solid transparent;
      border-right: 6px solid transparent;
      border-top: 6px solid transparent;
    }
    <div class="box">
      <div class="line"></div>
      <div class="triangle"><div class="inner"></div></div>
    </div>

    【讨论】:

      【解决方案4】:

      这可以使用单个 div 元素来实现。

      这是一个快速演示:

      html,body{background:green; height:100%;padding:0;margin:0;overflow:hidden;}
      .line {
        margin-top: 50px;
        height: 5px;
        width: 80%;
        background: #fff;
        position: relative;
        box-sizing: border-box;
      }
      .line:after,
      .line:before {
        content: "";
        position: absolute;
      }
      .line:after {
        left: calc(100% + 2px);
        height: 25px;
        width: 25px;
        top: -13px;
        border-top: 5px solid #fff;
        border-left: 5px solid #fff;
        transform: rotate(45deg);
      }
      .line:before {
        height: 100%;
        top: 0;
        left: calc(100% + 34px);
        width: 20%;
        background: inherit;
      }
      &lt;div class="line"&gt;&lt;/div&gt;

      【讨论】:

      • 边框之间的灰色有什么问题
      • @sdx11 你用的是什么浏览器?无法复制
      • 这里是屏幕截图imgur.com/a/vzjBx firefox 仅在应用 css 变换时:rotate(45deg)
      • 看看this example
      • 非常感谢这个例子,当我将它设置为内容框时,我找到了解决方案,它是背景剪辑属性,问题消失了imgur.com/a/VzT6o
      猜你喜欢
      • 2018-09-21
      • 1970-01-01
      • 2017-02-19
      • 1970-01-01
      • 1970-01-01
      • 2015-06-12
      • 1970-01-01
      • 2017-09-23
      • 2017-03-09
      相关资源
      最近更新 更多