Reputation: 1438
I have a button whose width is always 50px. How can I keep it center in the parent div horizontally?
Upvotes: 1
Views: 3341
Reputation: 903
What is positioned relative? The button(/div)?
It doesn't matter if it is a button or a div, just give the div class="button", and then you will get the styles.
When you know the exact width of the button, there is also the possibility to position it absolute, but that includes ripping it out of the normal flow (and that might not be what you want).
<div style="position:relative">
<button style="width:50px;position:absolute;left:50%;margin-left:-25px;">Btn</button>
</div>
Depending on what you are trying to obtain, I think I would go for the display:block;margin:auto;
I have forked Hirals fiddle here: http://jsfiddle.net/LRBvT/
Upvotes: 1
Reputation: 15709
Make alignment of parent div center
.
HTML:
<div class="parent">
<input type="button" class="button" value="Btn"/>
</div>
CSS:
.parent{text-align:center;}
.button{width:100px;}
Upvotes: 3