Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertical alignment of text and icon in button

I'm having trouble vertically aligning a font-awesome icon with text within a button under the Bootstrap framework. I've tried so many things including setting the line-height, but nothing is working.

<button id="edit-listing-form-house_Continue"          class="btn btn-large btn-primary"         style=""         value=""         name="Continue"         type="submit">     Continue     <i class="icon-ok" style="font-size:40px;"></i> </button> 

http://jsfiddle.net/fPXFY/

How can I get this to work?

like image 693
Shane Avatar asked Jul 04 '13 22:07

Shane


People also ask

How do I align text vertically to icons?

Use line-height for vertical centering with a fixed height To vertically center a single line of text or an icon within its container, we can use the line-height property. This property controls the height of a line in a run of text and adds an equal amount of space above and below the line-box of inline elements.

How do I align text and icon on same line?

If you want to make a text appear vertically aligned next to a Font Awesome icon, you can use the CSS vertical-align property set to “middle” and also, specify the line-height property. Change the size of the icon with the font-size property.

How do you align a button vertically?

Other than flexbox property, we can also center align the button horizontally and vertically using a set of CSS properties. Add position: relative to the container class and position: absolute to the class containing the button. Now use left:50% and top:50% to position the button to the center of the container.

How do I align text inside a button?

We can align the buttons horizontally as well as vertically. We can center the button by using the following methods: text-align: center - By setting the value of text-align property of parent div tag to the center. margin: auto - By setting the value of margin property to auto.


2 Answers

There is one rule that is set by font-awesome.css, which you need to override.

You should set overrides in your CSS files rather than inline, but essentially, the icon-ok class is being set to vertical-align: baseline; by default and which I've corrected here:

<button id="whatever" class="btn btn-large btn-primary" name="Continue" type="submit">     <span>Continue</span>     <i class="icon-ok" style="font-size:30px; vertical-align: middle;"></i> </button> 

Example here: http://jsfiddle.net/fPXFY/4/ and the output of which is:

enter image description here

I've downsized the font-size of the icon above in this instance to 30px, as it feels too big at 40px for the size of the button, but this is purely a personal viewpoint. You could increase the padding on the button to compensate if required:

<button id="whaever" class="btn btn-large btn-primary" style="padding: 20px;" name="Continue" type="submit">     <span>Continue</span>     <i class="icon-ok" style="font-size:30px; vertical-align: middle;"></i> </button> 

Producing: http://jsfiddle.net/fPXFY/5/ the output of which is:

enter image description here

like image 186
nickhar Avatar answered Oct 11 '22 04:10

nickhar


Alternativly if your using bootstrap then you can just add align-middle to vertical align the element.

<button id="whaever" class="btn btn-large btn-primary" style="padding: 20px;" name="Continue" type="submit">Continue     <i class="icon-ok align-middle" style="font-size:40px;"></i> </button> 
like image 35
Ali Kazai Avatar answered Oct 11 '22 04:10

Ali Kazai