Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to place a button in the center of div of dynamic size

I have this code:

<div class="outer">
    <ul class="list">
        <li class="inner">
            <div class="line">information1</div>
            <div class="line">hyperlink1</div>
        </li>
        <li>
            <div class="line">information2</div>
            <div class="line">hyperlink2</div>
        </li>
        <li>
            <div class="line">information3</div>
            <div class="line">hyperlink3</div>
        </li>
        <li>
            <div class="line">information4</div>
            <div class="line">hyperlink4</div>
        </li>
    </ul>
    <input type="submit" id="send" value="send" class="button"/>
</div>

and the css:

.outer
{
    display: table;
    border: 1px solid;
    padding: 5px;
}    
.list
{
    list-style: none;
    padding: 5px;
}    
.inner
{
    display: table-row;
    padding: 5px;
}    
.line
{
    display: table-cell;
    border: 1px solid;
    border-radius: 5px;
}    
.button
{
    top:50%; 
    left:50%;
}

the output is: this

Now i want to place the button in the center of the 'outer' div no matter what is the width.

example: i want the button to be in center even for: this. without having to change the css each time the div size changes. Even if the 'outer' div is dynamic, the button should be in the center.

thank you.

like image 760
s.mujahid8 Avatar asked Mar 19 '13 10:03

s.mujahid8


People also ask

How do you position a button to the center?

We can align the buttons horizontally as well as vertically. We can center the button by using the following methods: text-align: center - By setting the value of text-align property of parent div tag to the center. margin: auto - By setting the value of margin property to auto.

How do I move a button to the middle in HTML?

You can center a block level element by setting margin-left and margin-right to auto . We can use the shorthand margin: 0 auto to do this. (This also sets margin-top and margin-bottom to zero.)


2 Answers

This achieves what you're looking for in a simple, succinct, way:

.button {
    display:block;
    margin: 0 auto;
}
like image 164
Kyuuji Avatar answered Sep 25 '22 19:09

Kyuuji


You need to give the button a specific width and then you can use automatic margins

.button {
  display: block;
  margin: 0 auto;
}

http://fiddle.jshell.net/CrHyd/

like image 24
Raffael Avatar answered Sep 23 '22 19:09

Raffael