开发者

CSS, Centering an absolute <div> withing a 100% width <div>

This shouldn't be hard...I have a menu and some content wrapped in a centered, liquid div. The content is positioned absolute. All I want to do is center #content-container. What am I missing here?

    #wrapper {
        max-width:100%;
        min-width:600px;
        min-height:100%;
        margin:0 auto;
    }

    #header {
     -moz-background-clip:border;
     -moz-background-origin:padding;
     -moz-background-size:auto auto;
     background-attachment:scroll;
     background-color:transparent;
     background-image:url(images/KMIAFS_banner.jpg);
     background-position:center top;
     background-repeat:no-repeat;
     height:150px;
    }

    #menu {
     margin-top:150px;
     clear:left;
     float:left;
     padding:0;
     border-top:6px solid #336699;
     width:100%;
     overflow:hidden;
    }

    #menu ul {
     float:left;
     margin:0;
     padding:0;
     list-style:none;
     position:relative;
     left:50%;
     text-align:center;
    }

    #menu ul li {
     display:block;
     float:left;
     list-style:none;
     margin:0;
     padding:0;
     position:relative;
     right:50%;
    }

    #menu u开发者_如何学编程l li a {
     display:block;
     float:left;
     margin:0 1px 0 0;
     padding:30px 10px 6px 10px;
     background:#336699;
     text-decoration:none;
     color:#fff;
    }

    #menu ul li a:hover {
     padding:35px 10px 6px 10px;
    }

    #menu ul li.active a, #menu ul li.active a:hover {
     padding:40px 10px 6px 10px;
    }

    #content-container {
     top:225px;
     position:absolute;
     margin:0 auto;
     width:1000px;
     background-color:#fff;
    }

    #content {
     clear:left;
     float:left;
     width:610px;
     padding:20px 0;
     margin:0 0 0 30px;
     display:inline;
    }

    #content h2 {
     margin:0;
    }

    #aside {
     float:right;
     width:290px;
     padding:20px 0;
     margin:0 20px 0 0;
     display:inline;
    }

    #aside h3 {
     margin:0;
    }


<div id="wrapper">
<div id="header">
 <a id="box-link" href="index.html"></a>
    <div id="menu">
       <ul>
          <li><a href="" title="Link01">Link01</a></li>
          <li><a href="" title="Link02">Link02</a></li>
          <li><a href="" title="Link03">Link03</a></li>
          <li><a href="" title="Link04">Link04</a></li>
          <li><a href="" title="Link05">Link05</a></li>
          <li><a href="" title="Link06">Link06</a></li>
          <li><a href="" title="Link07">Link07</a></li>
          <li><a href="" title="Link08">Link08</a></li>
       </ul>        
    </div>
  <div id="content-container">
  <div id="content">
   <h2>
    Page heading
   </h2>
   <p>
    Lorem ipsum dolor sit amet consect etuer adipi scing elit sed diam nonummy nibh euismod tinunt ut laoreet dolore magna aliquam erat volut. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.
   </p>
   <p>
    Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.
   </p>
   <p>
    Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat.
   </p>
  </div>
  <div id="aside">
   <h3>
    Aside heading
   </h3>
   <p>
    Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan.
   </p>
  </div>
 </div>
</div>
</div>
</div>


There are a couple of solutions to this problem.

The easiest is to remove the absolute positioning from the #content-container. That will allow the margin: 0 auto to center the div. In order to fix the issue with the vertical movement when you hover over the menu (which I suspect is why you made it absolute positioned) just set the height of the menu div (100px seems like enough, but you can play with it). So these are your modifications:

 #menu {
      margin-top:150px;
      clear:left;
      float:left;
      padding:0;
      border-top:6px solid #336699;
      width:100%;
      overflow:hidden;
      height: 100px;
  }

  #content-container {
      margin:0 auto;
      width:1000px;
      background-color:#fff;
      left: 100px;
  }

The other solution, if you really want to keep the #content-container absolute positioned, is to wrap the #content-container div in another div, say #inner-wrapper, and set that div's position to relative. That will make #content-container position itself relative to #inner-wrapper, rather than the window. That CSS might look like this:

  #inner-wrapper {
    position: relative;
    margin: 0 auto;
    width: 1000px;
  }

This trick alone won't keep the menu aligned, however. To do that you'd have to wrap the menu in this new div, and modify your CSS/HTML to have the blue top border still extend the width of the page. Not too difficult, but it's more work.

Personally, I'd just do the first solution and call it a day.


Just remove position:absolute; from #content-container class. Otherwise margin:0 auto; is ignored. Also, looks like you are missing two closing div tags above.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜