45

I am now using Twitter Bootstrap 3 RC2 as well as Twitter Bootstrap which has moved into a separate repository. I have noticed, if used in a button with text the icon is not centered very well:

enter image description here

The icon and the text have the same bottom line, but I believe for a good looking button the icon should be centered based on the text, shouldn't it? Any idea how to achieve this?

http://bootply.com/74652

Mahoni
  • 7,088
  • 17
  • 58
  • 115
  • I can't see the glyph in the example you provided. Do you mean you want the glyph and text both centered within the button? – Mister Epic Aug 16 '13 at 12:55

10 Answers10

58

Move the text out of the <span> with the glyphicon and apply vertical-align: middle; to the <span>

  <button class="btn btn-default">
    <span class="glyphicon glyphicon-th" style="vertical-align: middle;"></span> Centered
  </button>

Demo

This works in Bootstrap 4 with Font Awesome icons like so

<button class="btn btn-default" style="vertical-align: middle;">
    <i class="fa fa-times"></i> Centered
</button>
Ryan Knutson
  • 108
  • 2
  • 14
Russ Cam
  • 124,184
  • 33
  • 204
  • 266
  • 3
    I had a problem where the button wasn't shown with the same height as an input text paired inside an `input-group`. That problem was fixed with this solution, although the icon first appeared too low on the button. Using `vertical-align:top` fixed that. – Bjørn Stenfeldt Nov 11 '13 at 11:01
  • 1
    Note: vertical-align middle may not always fix it, especially if your word has only ascenders (`f`, `t`) or descenders (`g`, `y`, `q`), as the middle of the vertical box of the word will not exactly align with the middle of the icon's vertical box. – kumarharsh Jan 19 '15 at 06:39
  • Was about to go the hacky route and use an image instead (*yuck!*). Didn't know that the `vertical-align` property could be used for glyphs like this! Thanks! – Priidu Neemre Sep 16 '15 at 13:35
27

Apply a vertical-align: -{N}px; style on the .glyphicon-th to shift it N pixels up/down.

kumarharsh
  • 18,961
  • 8
  • 72
  • 100
3

For some vertical-align might not work due to positioning: If you look into .glyphicon class you'll see it is set to relative, try setting it to 'inherit' e.g:

.glyphicon.v-centered {
    position: inherit;
    vertical-align: middle;
}

This should also work for icons not in buttons, e.g. tabs.

Vilius
  • 77
  • 3
1

Try vertical-align: middle; on .glyphicon-th:before

Mihai Alex
  • 678
  • 6
  • 13
  • With middle the text goes up and lines with the glyph icon bottom border. Sad, because it sounds reasonable to me. – Mahoni Aug 16 '13 at 13:09
1

None of the methods above worked well for me by themselves, but using display: inline-block; vertical-align: middle on the elements did. It's the inline-block that seems to be key.

.vcenter * {
    vertical-align: middle;
    display: inline-block;
}

.btn i {
    margin-left: 10px;
    font-size: 20px;
}

with

<div class="vcenter">
  <button class="btn btn-default btn-lg"><span>Centered</span><i class="glyphicon glyphicon-ok"></i></button>
  <button class="btn btn-default"><span>Centered</span><i class="glyphicon glyphicon-ok"></i></button>
  <button class="btn btn-default btn-sm"><span>Centered</span><i class="glyphicon glyphicon-ok"></i></button>
</div>

See http://www.bootply.com/UbY78zM8pD for live example.

Alastair Maw
  • 5,373
  • 1
  • 38
  • 50
1

I managed to do it like this:

.glyphicon.center:before {
    vertical-align: middle;
}

and use <span class="glyphicon center glyphicon-th"></span>.

Timo Türschmann
  • 4,388
  • 1
  • 22
  • 30
1

Problem: Glyph icon was 2 px too high above the button text. Fixed it as follows using examples here. Thank you (Alastair Maw).

I managed to get mine working as follows:

HTML

<div class="col-xs-12">
    <a title="" class="btn btn-block btn-info oa-btn-spacer_homepage oa-ellipsis" role="button" href="default.aspx?action=page&name=creditcard">
        <span class="glyphicon glyphicon-credit-card glyph_Credit"></span> Update Your Credit Card and Billing Information
    </a>
</div>

CSS

#panelTopic  .glyph_Credit {
    vertical-align: middle;
    display: inline-block;
    padding-bottom:6px;
}
Sébastien
  • 11,860
  • 11
  • 58
  • 78
0
<button class="btn btn-default">
          <span class="glyphicon glyphicon-th" style="font-size: 14px;"> </span>
          <span style="font-size: 17px;">Not Centered</span>
    </button>

please increase or decrease the font size according to your requirement

rajeshpanwar
  • 1,223
  • 1
  • 11
  • 25
0

Other way

<button class="btn default" id="IdBack">
  <i class="glyphicon glyphicon-hand-left"></i> 
  <b>Back</b>
</button>
Shalini
  • 219
  • 1
  • 5
  • 16
0

Add a margin of the specific pixels or percentage. In my app, this worked beautifully, based on the dimensions of the area.

CSS:

.someWrapperClass button span {
    margin-top: 120%;
}

HTML:

<div class="someWrapperClass">
    <button type="button" ng-click="SomeMethod()">
        <span class="glyphicon glyphicon-chevron-left"></span>
    </button>
</div>
Will Strohl
  • 1,646
  • 2
  • 15
  • 32