0

I have this piece of html code:

<div id="main" style=" margin:0 auto; ">
    <div style="float: left">
        <a style="display: block" href="#"><img src="test.gif"></a>
        <a style="display: block" href="#"><img src="test.gif"></a>
        <a style="display: block" href="#"><img src="test.gif"></a>
    </div>
    <div style="float: left">
        <a style="display: block;" href="#"><img src="ursus.gif"></a>
    </div>
    <div style="float: left">
        <a style="display: block" href="#"><img src="test.gif"></a>
        <a style="display: block" href="#"><img src="test.gif"></a>
        <a style="display: block" href="#"><img src="test.gif"></a>
    </div>
</div>

And I want the #main div to be align in the center of the page. How cand I do this? Thanks!

zuzuleinen
  • 2,604
  • 5
  • 38
  • 49

3 Answers3

5

You must set a width for the div:

<div id="main" style="margin:0 auto; width:700px;">

I would also strongly suggest that you don't use inline css. Add something like this in a external stylesheet instead:

#main {
   margin:0 auto;
   width:700px;
}
#main a {
   display:block;
}
#main > div {
   float:left;
}
Cyclonecode
  • 29,115
  • 11
  • 72
  • 93
2
<body>
<div id="main" style=" margin:0 auto; width: 960px;">
    <div style="float: left">
        <a style="display: block" href="#"><img src="test.gif"></a>
        <a style="display: block" href="#"><img src="test.gif"></a>
        <a style="display: block" href="#"><img src="test.gif"></a>
    </div>
    <div style="float: left">
        <a style="display: block;" href="#"><img src="ursus.gif"></a>
    </div>
    <div style="float: left">
        <a style="display: block" href="#"><img src="test.gif"></a>
        <a style="display: block" href="#"><img src="test.gif"></a>
        <a style="display: block" href="#"><img src="test.gif"></a>
    </div>
</div>
</body>
Cyclonecode
  • 29,115
  • 11
  • 72
  • 93
danludwig
  • 46,965
  • 25
  • 159
  • 237
2

You mentioned in a comment to Krister Andersson you want the div to center no matter the width. But you have to give some kind of width info. If you want the div to be flexible width but centered then you need to set specific equal margins:

#main {
    margin: 0 50px; /* or whatever px, em, or % you want */
}

See http://jsfiddle.net/jBYZq/.

The point is, you have to either set the width of the element and use margin: 0 auto or set the margins of the element equal. But something has to be set to get it centered.

ScottS
  • 71,703
  • 13
  • 126
  • 146