在Bootstrap锚点(按钮)中垂直对齐文本

4

我正在努力垂直对齐Bootstrap(3)中的<a>文本,而在<button>中它可以正常工作。

HTML代码:

<button class="btn">some text<span class="glyphicon glyphicon-play pull-right"></span></button>
</br>
</br>
</br>
<a href="#" class="btn">some text<span class="glyphicon glyphicon-play pull-right"></span></a>

CSS:

.btn {
  width: 315px;
  height: 40px;
  background-color: #ff38a4;
  color: #fff;
  display: inline-block;
  margin-bottom: 0;
  font-weight: normal;
  text-align: left;
  vertical-align: middle;
  -ms-touch-action: manipulation;
  touch-action: manipulation;
  cursor: pointer;
  background-image: none;
  border: 1px solid transparent;
  white-space: nowrap;
  padding: 4px 8px;
  font-size: 14px;
  line-height: 1.42857143;
  border-radius: 0;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  -webkit-box-shadow: 2px 2px 2px 0 rgba(0, 0, 0, 0.5);
  -moz-box-shadow: 2px 2px 2px 0 rgba(0, 0, 0, 0.5);
  box-shadow: 2px 2px 2px 0 rgba(0, 0, 0, 0.5);
}

并输出:

Screen shot of unaligned images

以下是问题的Codepen链接: http://codepen.io/anon/pen/vyOaao

如何使用<a>标签将文本和图标居中对齐?

4个回答

4
防止指定值的行高: enter image description here

这会使文本对齐,但不会对齐图标。http://codepen.io/anon/pen/ObVoRp - dwkns
在这种情况下,为图标添加垂直对齐: .glyphicon {display: inline-block; vertical-align: middle} - Alex
那个没用,它保持与顶部对齐。http://codepen.io/anon/pen/eBNLPo - dwkns

2

使用这个:

a.btn {
  background-color: #ff38a4;
  border-radius: 0;
  box-shadow: 2px 2px 2px 0 rgba(0, 0, 0, 0.5);
  color: #fff;
  cursor: pointer;
  display: flex;
  font-size: 14px;
  font-weight: normal;
  justify-content: space-between;
  margin-top: 1rem;
  padding: 14px 14px 14px;
  width: 315px;
}

请查看示例:https://jsfiddle.net/ck0s48ab/

与上面的示例类似,您可以使用display: flex;


0

不确定这是否适用于您的用例,但我刚遇到了这个问题,我选择了:

<a href="#" role="button"><button class="btn" type="button">some text<span class="glyphicon glyphicon-play pull-right"></span></button></a>

只需使用锚标签包装原始按钮即可。


-2

1
-1:OP只是尽可能清楚地展示他们的问题,<br/>标签不是<a>元素内文本对齐问题的原因。 - John

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接