Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Margin Auto and Center to center Float Left Div

Tags:

css

I know this question had been asked many times.

Align a div to center

However, I follow their suggestion :

<center>
  <div style="margin : auto; text-align: center">
    <a href="#" style="float: left; margin-right: 10px;">Menu Item 1</a>
    <a href="#" style="float: left; margin-right: 10px;">Menu Item 2</a>
    <a href="#" style="float: left; margin-right: 10px;">Menu Item 3</a>
  </div>
</center>

alt text
(source: google.com)

By using "Center" and "Margin Auto", "Text Align Center" ... I still unable to center the menu item.

like image 869
Cheok Yan Cheng Avatar asked Mar 13 '10 17:03

Cheok Yan Cheng


2 Answers

use inline-block instead of float left.

<center>
  <div style="margin : auto; text-align: center">
    <a href="#" style="display: -moz-inline-box; display: inline-block; left; margin-right: 10px;">Menu Item 1</a>
    <a href="#" style="display: -moz-inline-box; display: inline-block; margin-right: 10px;">Menu Item 2</a>
    <a href="#" style="display: -moz-inline-box; display: inline-block; margin-right: 10px;">Menu Item 3</a>
  </div>
</center>
like image 156
Cheok Yan Cheng Avatar answered Oct 10 '22 09:10

Cheok Yan Cheng


Why not use an unordered list? After all, you are creating a list of links.

<ul>
 <li><a href="#">Menu Item 1</a></li>
 <li><a href="#">Menu Item 2</a></li>
 <li><a href="#">Menu Item 3</a></li>
</ul>

li {
  display: inline;
}

ul {
  width: 50%;
  margin: 0 auto;
}
like image 40
Chris Avatar answered Oct 10 '22 09:10

Chris