Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to horizontally center the buttons in a fixed sized div?

Tags:

html

css

Below is the code, two buttons in one div.

<div style="position:relative; width: 300px; height: 30px; border: 1px solid;">
<input type="button" value="ok" style="position:relative; width: 70px; height: 30px;">
<input type="button" value="ok" style="position:relative; width: 70px; height: 30px;">
</div>

How to horizontally center the buttons in fixed sized did ?

like image 781
mahesh Avatar asked Apr 16 '12 11:04

mahesh


2 Answers

Adding text-align:center; CSS to the <div> will center the buttons. You should also consider separating the style from the content, which amongst other reasons, reduces the duplication. For example

CSS

div {
    position:relative;
    width:300px;
    height:30px;
    border:1px solid;
    text-align:center;
}

input {
    position:relative;
    width:70px;
    height:30px;
}

HTML

<div>
    <input type="button" value="ok"/>
    <input type="button" value="ok"/>
</div>

Edit: The official definition for text-align states:

The text-align property describes how inline-level content of a block container is aligned

so it will centre all inline level elements and <input> is an inline element.

like image 115
andyb Avatar answered Sep 30 '22 20:09

andyb


Try this:

<div style="position:relative; width: 300px; height: 30px; border: 1px solid; text-align:center;">
<input type="button" value="ok" style="position:relative; width: 70px; height: 30px;">
<input type="button" value="ok" style="position:relative; width: 70px; height: 30px;">
</div>
like image 33
dimmat Avatar answered Sep 30 '22 19:09

dimmat