Forum Moderators: not2easy

Message Too Old, No Replies

Centering a block element in firefox

Trying to center a button in Firefox

         

Adam5000

2:38 pm on Jan 16, 2009 (gmt 0)

10+ Year Member



I'm trying to center an anchor/button/block element inside a table cell and the code I'm using is shown below.

<html>
<head>
<title>Test</title>
</head>

<body>
<table style="width: 100%;">
<tr>

<td style="width:100%; margin:0px auto; text-align: center;">

<a id="start" style="display: block; width:160px; height:75px;" href="#"

onClick="startClock(); return false;">

<img id="start_button" src="start.jpg" alt="Start button">

</a>

</td>

</tr>
</table>
</body>
</html>

It works with IE but not with Firefox.

Help!

simonuk

2:49 pm on Jan 16, 2009 (gmt 0)

10+ Year Member



Remove the display:block

rocknbil

3:00 pm on Jan 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A number of ways. Since you're using a table, it messes with you a bit, but just apply margin:0 auto 0 auto to the block element itself.

<td> <!-- note none of these TD attributes are needed -->
<a id="start" style="display: block; width:160px; height:75px; margin: 0 auto 0 auto;" href="#"
onClick="startClock(); return false;"><img id="start_button" src="start.jpg" alt="Start button"></a>
</td>

You don't need a margin attribute on a cell, unless you want the cell to have a margin from adjoining cells or the parent table. :-) (I am kidding, margin on a table cell has no effect.)

simonuk

3:06 pm on Jan 16, 2009 (gmt 0)

10+ Year Member



This will also work:

margin: 0 auto;

Can't help it, excess code niggles me :-)

Adam5000

1:55 am on Jan 19, 2009 (gmt 0)

10+ Year Member



Thanks rocknbil and simonuk. That code centers up the button keeps it centered when the window is resized.

I definitely don't want to leave out the Firefox users. I want to make sure they can view the site too, the way it was intended to be seen.

Making this site is a big job and I've got it figured to take about another five weeks.

Soon I'll probably have some questions for the graphics board as I start to pretty it up a bit.

Thanks again for your help.