【问题标题】:How to center Text vertically?如何垂直居中文本?
【发布时间】:2015-01-16 20:17:49
【问题描述】:

如何使文本Test Text 垂直居中?见here

我想让文本和边界底部之间的距离等于文本和边界顶部之间的距离。 `

【问题讨论】:

  • 我们可以看看你的代码吗?
  • 是单行文字吗?如果是,则使用与高度相同的行高
  • 是的,只有一个字
  • 也许这会有所帮助:stackoverflow.com/questions/396145/…
  • line-height 在这里没有帮助,因为元素由不同大小的边框组成,整个方法应该不同。

标签: html css text-align


【解决方案1】:

对于单行文本,使用等于height(或四舍五入为偶数)的line-height。

#a {
  display: inline-block;
  padding-right: 20px;
  padding-left: 15px;
  font-family: 'Roboto';
  font-size: 12px;
  font-weight: bold;
  font-stretch: condensed;
  COLOR: white;
  border-left: 10px solid transparent;
  border-bottom: 25px solid;
  border-bottom-color: #FF3300;
  height: 0;
  line-height: 26px;
  top: 50%;
}
<span id="a">TEST TEXT</span>

【讨论】:

    【解决方案2】:

    使用line-height: 25px(仅适用于单行文本)。

    #a {
      display: inline-block;
      padding-right: 20px;
      padding-left: 15px;
      font-family: 'Roboto';
      font-size: 12px;
      line-height: 25px;
      font-weight: bold;
      font-stretch: condensed;
      COLOR: white;
      border-left: 10px solid transparent;
      border-bottom: 25px solid;
      border-bottom-color: #FF3300;
      height: 0;
      top: 50%;
    }
    <span id="a">TEST TEXT</span>

    【讨论】:

      【解决方案3】:

      对左侧部分使用:before 伪元素,行高不适用于bottom-border。我更新了你的小提琴:

      http://jsfiddle.net/wfkd6Lbn/8/

      【讨论】:

        【解决方案4】:

        .area { 
          width: 300px; 
          height: 300px;  
          position: relative;
        }
        
        .bubble { 
          position: absolute; 
          left: 93px; 
          top: 21px; 
          width: 135px; 
          height: 84px; 
          display: table; 
        }
        
        .bubble p {
          display: table-cell; 
          vertical-align: middle; 
          text-align: center; 
        <div class="area">
              <div class="bubble">
                  <p>To look best, text should really be centered inside this bubble both vertically and horizontally.</p>
              </div>
        </div>

        【讨论】:

          猜你喜欢
          • 2013-10-15
          • 2011-07-23
          • 1970-01-01
          • 2015-09-16
          • 2017-10-04
          • 2015-08-25
          • 1970-01-01
          相关资源
          最近更新 更多