tags:

views:

138

answers:

4

In Firefox 'A' shows in the middle, on Chrome/IE it doesn't:

<button type="button" style="width:24px; text-align:center; vertical-align:middle">A</button>

Note the following has the same results:

<button type="button" style="width:24px;">A</button>

Edit: Now seems to be fixed in Chrome 5.0

+1  A: 

What about:

<input type="button" style="width:24px;" value="A"/>
gmcalab
+2  A: 

Testing this in Chrome, you need to add

padding: 0px;

To the CSS.

Tejs
IE instills its own padding on buttons (thanks Windows). Button formatting is annoying because it takes on OS and browser specific formatting. It's the same reason you can adjust the size of `<input type="text">` or style buttons in browsers in Safari. You would do well to remember to always use a CSS reset.
dclowd9901
This didn't occur to me. Thank you, worked as expected for Chrome! Although it did unfortunately cause Firefox to now be a little off center.
gbhall
Actually if anyone else has this problem here is the fix that works consistent across IE, FF and Chrome:<pre>padding:0 3px 0 3px; /* Purely for IE, however doesn't affect Firefox. Chrome requires 0 */</pre><code>-webkit-padding-start: 0;</code>
gbhall
@gbhall - seems like a lot of work to just get some text centered on a button, when if you would use `<input type="button" style="width:24px;" value="A"/>`, it would just work.
gmcalab
@gmcalab - that actually wont work in Chrome. It's putting padding into the left side of the button, and why this particular CSS fix is required.
Tejs
@Tejs - `<input type="button" style="width:24px;" value="A"/>` works in all browsers. He was using `<button type="button" style="width:24px;" value="A"/>` where I am using `<input>`
gmcalab
A: 

Usualy, your code should work...

But here is a way to center text in css:

.text
{
    margin-left: auto;
    margin-right: auto;
}

This has proved to be bulletproof to me whenever I want to center text with css.

Zwik
A: 

The problem is that buttons render differently across browsers. In Firefox, 24px is sufficient to cover the default padding and space allowed for your "A" character and center it. In IE and Chrome, it does not, so it defaults to the minimum value needed to cover the left padding and the text without cutting it off, but without adding any additional width to the button.

You can either increase the width, or as suggested above, alter the padding. If you take away the explicit width, it should work too.

JN Web