Centering div with unknown width

前端 未结 4 617
后悔当初
后悔当初 2020-12-14 15:48

i have div

which contains all page content including header footer etc.

So i would like to center this div to t

相关标签:
4条回答
  • 2020-12-14 15:59

    One way is to create a wrapper around your #page element, let's call it #wrapper:

    #wrapper {
       position: relative;
       left: 50%;
       float: left;
    }
    #page {
       position: relative;
       left: -50%;
       float: left;
    }
    

    This will allow the #page div to remain a variable width.

    0 讨论(0)
  • 2020-12-14 16:06

    See: http://jsfiddle.net/thirtydot/rjY7F/

    HTML:

    <div id="container">
        i'm as wide as my content
    </div>
    

    CSS:

    body {
        text-align: center;
    }
    #container {
        text-align: left;
        border: 1px solid red;
        display: inline-block;
        /* for ie6/7: */
        *display: inline;
        zoom: 1;
    }
    
    0 讨论(0)
  • 2020-12-14 16:06

    how about this?

    body {
      text-align: center;
    }
    #container {
      text-align: left;
    }
    

    Assuming <body><div id="container"></div></body>

    0 讨论(0)
  • 2020-12-14 16:07

    This will give you a dinamically-sized div that stays at the center of the body and is the smallest size needed to fit the content:

    body { text-align: center; }
    div.container { display: inline-block; text-align: left; }
    
    0 讨论(0)
提交回复
热议问题