【问题标题】:How to position text at the bottom of a triangle div?如何将文本放置在三角形 div 的底部?
【发布时间】:2015-03-25 01:02:06
【问题描述】:

我有以下 div:

div {
  width: 0px;
  height: 0px;	
  border-left: 	126px solid transparent;
  border-right: 	126px solid transparent;
  border-bottom: 	126px solid #D30000;
  position: relative;
  left: 55%;
  top: 40px;
  display: table-cell;
  text-align:center;
  vertical-align: bottom;
}
<div>Triangle</div>

如下对齐三角形上的文字:

                    _Triangle_
                   /          \
                  /            \
                 /              \
                /                \
               /________________  \

我想要完成的是:

                       /\
                      /  \
                     /    \
                    /      \
                   /        \
                  / Triangle \

如何将文本推送到三角形 div 的底部,而不必将其包装在标签中?使用 position: relative; 对我来说很重要。因为绝对没有用。但是,即使我删除了 position:relative;文本仍然没有到达 div 的底部。它停留在三角形的顶角被切断的顶部,使形状看起来像梯形。

【问题讨论】:

  • 文本应该至少在文本标签中。文本节点最差。
  • 我正在尝试在不使用任何标签的情况下完成此操作
  • 由于三角形仅由边框组成,您可能需要重新考虑您的结构/方法。

标签: javascript jquery html css


【解决方案1】:

您可以使用伪元素来制作三角形。

div {
  text-align: center;
  width: 126px;
  margin: 126px auto;
  position: relative;
  color: white;
}
div:after {
  content: '';
  position: absolute;
  width: 0;
  height: 0;
  left: 50%;
  bottom: 0;
  transform: translateX(-50%);
  z-index: -1;
  border-left: 126px solid transparent;
  border-right: 126px solid transparent;
  border-bottom: 126px solid #D30000;
}
<div>Triangle</div>

【讨论】:

    【解决方案2】:

    您可以在 DIV 内和用于绘制三角形的伪类之前使用额外的 SPAN 来实现。我使用 SPAN 将文本定位到 DIV 的底部。

    div{
        width: 252px;
        height: 126px;
        position: absolute;
    }
    div>span{
        position: absolute;
        bottom: 0px;
        text-align: center;
        width: 100%;
    }
    div:before{
        content: '';
        position: absolute;
        bottom: 0px;
        border-width: 126px;
        border-style: solid;
        border-color: transparent transparent #D30000  transparent;
        display: block;
        z-index: -1;
    }
    <div><span>Triangle</span></div>

    我在 chroma 和 IE 中都试过了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 2013-11-12
      • 1970-01-01
      • 2012-08-28
      • 1970-01-01
      • 2011-07-14
      • 2019-01-07
      相关资源
      最近更新 更多