0

I have a problem with center positioning a button in a row in a table.I used width and margin but it don't work.Can you help me? Here's the code for that button(in css):

 .subm {
position:relative;
width:130px;
margin-left:auto;
margin-right:auto;
background-image:url('background.bmp');
border:none;
color:white;
opacity:1;
height:25px;
outline:0 none;
box-shadow:1px 1px 2px black

}
.subm:hover {
background-image:none;
background-color:darkgray;
box-shadow:10px 10px 10px black
}

.subm:active {
color:black ;
font-weight:bold;
width:128px;
height:24px;
background-image:none;
background-color:dimgray;
}
sodawillow
  • 12,497
  • 4
  • 34
  • 44

3 Answers3

2

simply try this

button{
    height:20px; 
    width:100px; 
    position:relative;
    top:50%; 
    left:50%;
}

for just horizontal alignment use either

button{
    margin: 0 auto;
}

DEMO

sanoj lawrence
  • 951
  • 5
  • 29
  • 69
0

To achieve this, I would use following code:

.subm {
  position:absolute;
  width:130px;
  height:25px;
  top: calc(50% - 13px); // 50% - 0.5 * height.
  left: calc(50% - 65px); // 50% - 0.5 * width.
}

And for the parent:

position: relative;

You can see a working JSFiddle here

JiFus
  • 959
  • 7
  • 19
0

If you used width and margin: auto's and it's still not centering you may just need to add display: block to your .sumb class like so:

.subm {
    display:block;
    position:relative;
    width:130px;
    margin-left:auto;
    margin-right:auto;
    background-image:url('background.bmp');
    border:none;
    color:white;
    opacity:1;
    height:25px;
    outline:0 none;
    box-shadow:1px 1px 2px black
}

Demo: http://jsfiddle.net/re079odr/

Andy Mardell
  • 1,132
  • 8
  • 13