链接和输入控件中的垂直中心文本仅限CSS

时间:2015-11-05 16:54:16

标签: html css

有没有办法如何将控件垂直和水平中的白色文本居中,以便在橙色画布的中间放置文本? Center

演示:



<p>
    <input value="Login" class="ButtonOrange" type="submit">
<a class="ButtonOrange" href="#">Register</a>

</p>
&#13;
{{1}}
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

你可以使用line-height:160px;(160px =元素的高度)并删除填充,

.ButtonOrange {
    line-height:160px;             
    width: 161px;
    height: 160px;
    float: left;
    border-style: hidden;
    border-width: 0px;
    border-color: inherit;
    margin: 5px;
    background-color: #F66907;
    color: #FFF;
    border-radius: 6px;
    text-align: center;
    font-weight: bold;
    font-size: 12px;
    font-family: Arial, Helvetica, sans-serif;
    font-style: normal;
    text-decoration: none;
    position: relative;
}
<p>
    <input value="Login" class="ButtonOrange" type="submit">
<a class="ButtonOrange" href="#">Register</a>

</p>

答案 1 :(得分:2)

我创建了一个类(.centerVerticalHorizontal),您可以使用flex

将其添加到您希望水平和垂直居中的元素中

.ButtonOrange {
  width: 161px;
  height: 160px;
  float: left;
  display: inline-block;
  border-style: hidden;
  border-width: 0px;
  border-color: inherit;
  padding: 12px;
  margin: 5px;
  background-color: #F66907;
  color: #FFF;
  border-radius: 6px;
  text-align: center;
  font-weight: bold;
  font-size: 12px;
  font-family: Arial, Helvetica, sans-serif;
  font-style: normal;
  text-decoration: none;
  position: relative;
}
.centerVerticalHorizontal {
  display: flex;
  justify-content: center;
  align-items: center;
}
<p>
  <input value="Login" class="ButtonOrange centerVerticalHorizontal" type="submit">
  <a class="ButtonOrange centerVerticalHorizontal" href="#">Register</a>
  <div class="ButtonOrange centerVerticalHorizontal">Test div text</div>
</p>