4

I'm struggling to vertically align text within a Bootstrap (3) <a> when it works properly in a <button>

The 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>

The 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);
}

And the output :

Screen shot of unaligned images

And a codepen of the problem : http://codepen.io/anon/pen/vyOaao

How do I align the text and icon centrally using an <a> tag?

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
dwkns
  • 2,369
  • 4
  • 22
  • 35

4 Answers4

3

Prevents the specified value line-height: enter image description here

Alex
  • 409
  • 3
  • 18
  • This aligns the text, but not the icon. http://codepen.io/anon/pen/ObVoRp – dwkns Nov 08 '16 at 13:41
  • In this case, add the vertical-align for icons: .glyphicon {display: inline-block; vertical-align: middle} – Alex Nov 08 '16 at 13:55
  • That doesn't work. It stays aligned to the top. http://codepen.io/anon/pen/eBNLPo – dwkns Nov 08 '16 at 14:05
2

Use this:

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

See the fiddle: https://jsfiddle.net/ck0s48ab/

Here, like above you can use display: flex;

Jim Fahad
  • 635
  • 1
  • 8
  • 21
0

Not sure if this will work for your use cas but I just ran into this issue and I went with:

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

Just wrapping the original button with an anchor tag.

John
  • 1,466
  • 3
  • 21
  • 38
-2

why do you use </br></br></br> ? vertical-align with Bootstrap 3 might help you

Community
  • 1
  • 1
Burak Topal
  • 143
  • 1
  • 12
  • 1
    -1: The OP is just trying to demonstrate their problem as clearly as they can,
    tags aren't the cause of the text alignment issue inside of the element.
    – John Aug 20 '20 at 10:51