【问题标题】:<hr> line with a arrow in between pointing down<hr> 线之间有一个箭头指向下方
【发布时间】:2014-12-15 19:51:22
【问题描述】:

我正在尝试用一个指向下方的小箭头画一条线,如附图所示。

我能够在使用伪标签之前和之后(使用help from Stack overflow)让它在小提琴上工作。

<hr class="line">

http://jsfiddle.net/4eL39sm1/6/

但我现在需要在这样的 div 标签中使用它

<div class="hr"></div>

我已经相应地编辑了我的 css

div.hr{
width:70%;
}

div.hr:after {
content:'';
position: absolute;
border-style: solid;
border-width: 15px 15px 0;
border-color: #FFFFFF transparent;
display: block;
width: 0;
z-index: 1;
top: 8px;
left: 45%;
}

div.hr:before {
content:'';
position: absolute;
border-style: solid;
border-width: 15px 15px 0;
border-color: #7F7F7F transparent;
display: block;
width: 0;
z-index: 1;
top: 9px;
left: 45%;
}

我做了这两个观察:

  1. 箭头部分看起来是一个实心三角形。
  2. 箭头(三角形错位在顶部)。我从 css 中删除了顶部值,它对齐得很好,但它仍然看起来像一个三角形。

有什么办法可以解决这个问题吗?

谢谢。

【问题讨论】:

标签: html css


【解决方案1】:

你可以这样修改:

div.hr {
    width:70%;
    height: 1px;
    background: #7F7F7F;
}
div.hr:after {
    content:'';
    position: absolute;
    border-style: solid;
    border-width: 15px 15px 0;
    border-color: #FFFFFF transparent;
    display: block;
    width: 0;
    z-index: 1;
    top: 8px;
    left: 35%;
}
div.hr:before {
    content:'';
    position: absolute;
    border-style: solid;
    border-width: 15px 15px 0;
    border-color: #7F7F7F transparent;
    display: block;
    width: 0;
    z-index: 1;
    top: 9px;
    left: 35%;
}
&lt;div class="hr"&gt;&lt;/div&gt;

如您所见,您不必从pseudo-elements 中删除top。您唯一需要添加的是height: 1px,背景颜色与第二个三角形相同。

另外,如果你想在另一个元素中使用它并对齐到中心,你可以使用这个:

div.hr {
    width:70%;
    height: 1px;
    background: #7F7F7F;
    position: relative;
    margin: 0 auto;
}
div.hr:after {
    content:'';
    position: absolute;
    border-style: solid;
    border-width: 15px 15px 0;
    border-color: #FFFFFF transparent;
    display: block;
    width: 0;
    z-index: 1;
    left: 50%;
}
div.hr:before {
    content:'';
    position: absolute;
    border-style: solid;
    border-width: 15px 15px 0;
    border-color: #7F7F7F transparent;
    display: block;
    width: 0;
    z-index: 1;
    left: 50%;
}
<div>
    <div class="hr"></div>
</div>

附言顺便说一句,我是在你的第一篇文章中回答的人:)

在与@user3861559 交谈后,我针对他的情况创建了一种方法。而不是pseudo-elements,我使用嵌套的div,结果相同:

div.hr {
    width:70%;
    height: 1px;
    background: #7F7F7F;
}
div.after {
    position: absolute;
    border-style: solid;
    border-width: 15px 15px 0;
    border-color: #FFFFFF transparent;
    display: block;
    width: 0;
    z-index: 1;
    top: 8px;
    left: 35%;
}
div.before {
    position: absolute;
    border-style: solid;
    border-width: 15px 15px 0;
    border-color: #7F7F7F transparent;
    display: block;
    width: 0;
    z-index: 1;
    top: 9px;
    left: 35%;
}
<div class="hr">
    <div class="before"></div>
    <div class="after"></div>
</div>

【讨论】:

  • 谢谢你们两个! :) 令我困惑的是,它在小提琴中运行良好,但我在我的页面中遇到了同样的问题。我检查了元素,前后 CSS 值是一样的。
  • 我还有其他方法吗?
  • 请描述一下您尝试实现的目标以及为什么这种方法不适合您?
  • 我明白了。我正在寻找一种不使用伪元素以纯 HTML 和 CSS 呈现图像的方法。无论如何感谢您的帮助! :)
  • 这太棒了!谢谢分享。
猜你喜欢
  • 2018-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-30
  • 2013-01-17
  • 1970-01-01
相关资源
最近更新 更多