如何垂直居中文字?

时间:2015-01-16 11:08:59

标签: html css text-align

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

我希望文本和边框底部之间的距离等于文本和边框顶部之间的距离。  `

4 个答案:

答案 0 :(得分:4)

对于单行文字,请使用等于line-height的{​​{1}}(或向上舍入到偶数)。



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%;
}




答案 1 :(得分: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>

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

&#13;
&#13;
.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; 
&#13;
<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>
&#13;
&#13;
&#13;