How to Align a DIV Tag to Center

Discussion in 'HTML & Website Design' started by eyashwant, Jun 24, 2010.

  1. #1
    Hello Guys,

    I have made some page with nested div's. I made it's width to 80%. I want that to be completely aligned to center of the page. I tried with css and with html and with parent div. But some thing makes it not to work. Can you see the below code and please suggest me to get that to center.

    <style type="text/css">
    .nm-left { text-align:left; vertical-align:text-top; }
    .nm-left .nm-video, .nm-right .nm-video-reverse, .nm-left .nm-avatar,  .nm-left .nm-avatar-wrapper, .nm-left span.nm-button, .nm-left #user-form input#submit { float:left; }
    .nm-right { text-align:right; vertical-align:text-top; }
    .nm-right .nm-video, .nm-left .nm-video-reverse, .nm-right .nm-avatar, .nm-right .nm-avatar-wrapper, .nm-right span.nm-button, .nm-right #user-form input#submit { float:right; }
    .nm-wrapper { clear:both; font-size:14px; line-height:21px; margin:20px 0; padding:0px; }
    .nm-wrapper div { background-position:99% 10px; background-repeat:no-repeat; margin:0; padding:0; }
    .nm-wrapper div div { }
    .nm-wrapper div div div { overflow:hidden; }
    #ninjamonials .nm-wrapper div div div div { background:transparent; padding:20px;}
    #ninjamonials .nm-wrapper div div div div div { padding:0; }
    blockquote.nm-summary div, blockquote.nm-testimonial div, blockquote.nm-summary, blockquote.nm-testimonial, blockquote.nm-summary p, blockquote.nm-testimonial p { background:transparent; border:none; padding:0; margin:0; text-decoration:none; font-style:normal; }
    span.nm-button, #user-form input#submit { margin:5px; }
    #user-form { background-position:left bottom; background-repeat:repeat-x; clear:both; font-size:14px; line-height:21px; margin-top:20px; padding:5px; }
    .nm-testimonial, .nm-summary, .nm-author { padding-left:140px }
    .nm-summary { font-weight:bold; }
    .nm-author { display:block; font-size:12px; }
    .nm-name { font-weight:bold; padding-right:5px; }
    .nm-video, div.nm-video-reverse { margin:10px !important; }
    .nm-audio, .nm-author { padding:5px !important; }
    .nm-audio { display:block; }
    .nm-name { font-style:italic; }
    /* Oceanic Avatar-Specific CSS */
    .nm-avatar { }
    .nm-avatar img { background-color:#F7F7F7; border:1px solid #DBDBDB; margin:10px; padding:5px; }
    /* Oceanic Module-Specific CSS */
    .nm-module { overflow:hidden; margin: 5px; padding:4px!important; }
    .nm-module div.nm-avatar img { }
    .nm-module div.nm-avatar { }
    /* Blue Style */
    .nm-right.nm-oceanic-blue.nm-module, .nm-right.nm-oceanic-blue .nm-module { background:#EFF8FF url(oceanic_blue_tl.png) no-repeat -10px -10px; border:1px solid #D5ECFF; -moz-border-radius:5px; -webkit-border-radius:5px; }
    .nm-left.nm-oceanic-blue.nm-module, .nm-left.nm-oceanic-blue .nm-module { background:#EFF8FF url(oceanic_blue_nq.png) no-repeat -10px -10px; border:1px solid #D5ECFF; -moz-border-radius:5px; -webkit-border-radius:5px; }
    .nm-right.nm-oceanic-blue .nm-wrapper { background:#EFF8FF url(oceanic_blue_tl.png) no-repeat; color:#666; }
    .nm-left.nm-oceanic-blue .nm-wrapper { background:#EFF8FF url(oceanic_blue_nq.png) no-repeat; color:#666; }
    .nm-oceanic-blue .nm-wrapper div { background:url(oceanic_blue_tr.png) no-repeat 100% 0; }
    .nm-oceanic-blue .nm-wrapper div div { background:url(oceanic_blue_bl.png) no-repeat 0 100%; }
    .nm-oceanic-blue .nm-wrapper div div div { background:url(oceanic_blue_br.png) no-repeat 100% 100%; }
    .nm-oceanic-blue #user-form { background-color:#EFF8FF; border:1px solid #D5ECFF; -moz-border-radius:5px; -webkit-border-radius:5px; }
    .nm-left.nm-oceanic-blue .nm-avatar-wrapper { margin:0 10px 10px 0; min-height:70px; min-width:90px; background: url(oceanic_blue_quote.png) no-repeat 10px 10px !important; }
    /* Green Style */
    .nm-right.nm-oceanic-green.nm-module, .nm-right.nm-oceanic-green .nm-module { background:#EBF4D7 url(oceanic_green_tl.png) no-repeat -10px -10px; border:1px solid #9AC740; -moz-border-radius:5px; -webkit-border-radius:5px; }
    .nm-left.nm-oceanic-green.nm-module, .nm-left.nm-oceanic-green .nm-module { background:#EBF4D7 url(oceanic_green_nq.png) no-repeat -10px -10px; border:1px solid #9AC740; -moz-border-radius:5px; -webkit-border-radius:5px; }
    .nm-right.nm-oceanic-green .nm-wrapper { background:#EBF4D7 url(oceanic_green_tl.png) no-repeat; color:#666; }
    .nm-left.nm-oceanic-green .nm-wrapper { background:#EBF4D7 url(oceanic_green_nq.png) no-repeat; color:#666; }
    .nm-oceanic-green .nm-wrapper div { background:url(oceanic_green_tr.png) no-repeat 100% 0; }
    .nm-oceanic-green .nm-wrapper div div { background:url(oceanic_green_bl.png) no-repeat 0 100%; }
    .nm-oceanic-green .nm-wrapper div div div { background:url(oceanic_green_br.png) no-repeat 100% 100%; }
    .nm-oceanic-green #user-form { background-color:#EBF4D7; border:1px solid #9AC740; -moz-border-radius:5px; -webkit-border-radius:5px; }
    .nm-left.nm-oceanic-green .nm-avatar-wrapper { margin:0 10px 10px 0; min-height:70px; min-width:90px; background: url(oceanic_green_quote.png) no-repeat 10px 10px !important; }
    /* Grey Style */
    .nm-right.nm-oceanic-grey.nm-module, .nm-right.nm-oceanic-grey .nm-module { background:#EFEFEF url(oceanic_grey_tl.png) no-repeat -10px -10px; border:1px solid #999; -moz-border-radius:5px; -webkit-border-radius:5px; }
    .nm-left.nm-oceanic-grey.nm-module, .nm-left.nm-oceanic-grey .nm-module { background:#EFEFEF url(oceanic_grey_nq.png) no-repeat -10px -10px; border:1px solid #999; -moz-border-radius:5px; -webkit-border-radius:5px; }
    .nm-right.nm-oceanic-grey .nm-wrapper { background:#EFEFEF url(oceanic_grey_tl.png) no-repeat; color:#666; }
    .nm-left.nm-oceanic-grey .nm-wrapper { background:#EFEFEF url(oceanic_grey_nq.png) no-repeat; color:#666; }
    .nm-oceanic-grey .nm-wrapper div { background:url(oceanic_grey_tr.png) no-repeat 100% 0; }
    .nm-oceanic-grey .nm-wrapper div div { background:url(oceanic_grey_bl.png) no-repeat 0 100%; }
    .nm-oceanic-grey .nm-wrapper div div div { background:url(./images/oceanic_grey_br.png) no-repeat 100% 100%; }
    .nm-oceanic-grey #user-form { background-color:#EFEFEF; border:1px solid #999; -moz-border-radius:5px; -webkit-border-radius:5px; }
    .nm-left.nm-oceanic-grey .nm-avatar-wrapper { margin:0 10px 10px 0; min-height:70px; min-width:90px; background: url(./images/oceanic_grey_quote.png) no-repeat 10px 10px !important; }
    /* White Style */
    .nm-right.nm-oceanic-white.nm-module, .nm-right.nm-oceanic-white .nm-module { background:#FFF url(oceanic_white_tl.png) no-repeat -10px -10px; border:1px solid #E8E8E8; -moz-border-radius:5px; -webkit-border-radius:5px; }
    .nm-left.nm-oceanic-white.nm-module, .nm-left.nm-oceanic-white .nm-module { background:#FFF url(oceanic_white_nq.png) no-repeat -10px -10px; border:1px solid #E8E8E8; -moz-border-radius:5px; -webkit-border-radius:5px; }
    .nm-right.nm-oceanic-white .nm-wrapper { background:#FFF url(oceanic_white_tl.png) no-repeat; color:#666; border:1px solid #E8E8E8; -moz-border-radius:5px; -webkit-border-radius:5px; }
    .nm-left.nm-oceanic-white .nm-wrapper { background:#FFF url(oceanic_white_nq.png) no-repeat; color:#666; border:1px solid #E8E8E8; -moz-border-radius:5px; -webkit-border-radius:5px; }
    .nm-oceanic-white .nm-wrapper div { background:url(oceanic_white_tr.png) no-repeat 100% 0; }
    .nm-oceanic-white .nm-wrapper div div { background:url(oceanic_white_bl.png) no-repeat 0 100%; }
    .nm-oceanic-white .nm-wrapper div div div { background:url(oceanic_white_br.png) no-repeat 100% 100%; }
    .nm-oceanic-white #user-form { background-color:#FFF; border:1px solid #E8E8E8; -moz-border-radius:5px; -webkit-border-radius:5px; }
    .nm-left.nm-oceanic-white .nm-avatar-wrapper { margin:0 10px 10px 0; min-height:70px; min-width:90px; background: url(oceanic_white_quote.png) no-repeat 10px 10px !important; }
    .nm-audio1 {padding:5px !important; }
    .nm-summary1 {padding-left:140px }
    </style>                                   
    <div id="ninjamonials" class="nm-oceanic-grey nm-left" style="width:80%">
              <!--bof nm-wrapper -->
      <div class="nm-wrapper">
        <div>
          <div>
            <div>
                                  <div class="nm-content">
                <div class="nm-avatar-wrapper">
                              </div>
                                                    <blockquote 
    class="nm-testimonial">
                  <p>This is a testimonial test. This is a testimonial test.   This is a testimonial test. This is a testimonial test. This is a   testimonial test. This is a testimonial test. </p>
                  <p>This is a testimonial test. This is a testimonial test. This is a   testimonial test. This is a testimonial test. This is a testimonial   test. This is a testimonial test. </p>
                                    </blockquote>
                                        <div class="nm-author">
                                <span class="nm-name">Name</span>
                   (Website<span class="nm-title"></span>
                   ~               <span class="nm-auth-url"><a 
    href="http://www.google.com/">www.google.com</a></span>
                  ,               <span class="nm-location">Mine</span>
                   )                           </div>
                <!--eof nm-author --> 
              </div>
              <!--eof nm-content -->
                        <!--eof wrapper --> 
            </div>
          </div>
        </div>
      </div>
      <!--eof foreach -->
        <!--bof nm-wrapper -->
      <div class="nm-wrapper">
        <div>
          <div>
            <div>
                                  <div class="nm-content">
                <div class="nm-avatar-wrapper">
                                <!--<div class="nm-avatar">
                    <img src="./images/oceanic_grey_quote.png" alt="" height="100">
                  </div>  -->
                              </div>
                                                    <blockquote 
    class="nm-testimonial">
                  <p>This is a testimonial test. This is a testimonial test.   This is a testimonial test. This is a testimonial test. This is a   testimonial test. This is a testimonial test. </p>
                  <p>This is a testimonial test. This is a testimonial test. This is a   testimonial test. This is a testimonial test. This is a testimonial   test. This is a testimonial test. This is a testimonial test. This is a testimonial test.   This is a testimonial test. This is a testimonial test. This is a   testimonial test. This is a testimonial test. </p>
                  <p>This is a testimonial test. This is a testimonial test. This is a   testimonial test. This is a testimonial test. This is a testimonial   test. This is a testimonial test. This is a testimonial test. This is a testimonial test.   This is a testimonial test. This is a testimonial test. This is a   testimonial test. This is a testimonial test. </p>
                  <p>This is a testimonial test. This is a testimonial test. This is a   testimonial test. This is a testimonial test. This is a testimonial   test. This is a testimonial test. </p>
                                    </blockquote>
                                        <div class="nm-author"><span class="nm-name">Name</span> (Website<span class="nm-title"></span> ~ <span class="nm-auth-url"><a 
    href="http://www.google.com/">www.google.com</a></span> , <span class="nm-location">Mine</span> ) </div>
                <!--eof nm-author --> 
              </div>
              <!--eof nm-content -->
                        <!--eof wrapper --> 
            </div>
          </div>
        </div>
      </div>
      <!--eof foreach -->
        <!--bof nm-wrapper -->
      <div class="nm-wrapper">
        <div>
          <div>
            <div>
                                  <div class="nm-content">
                <div class="nm-avatar-wrapper">
                                <!--<div class="nm-avatar">
                    <img name="Christopher Cody" 
    src="index.php_files/20091202033758_face3.jpg" alt="" height="100">     
             </div> -->
                              </div>
                                                    <blockquote 
    class="nm-testimonial">
                  <p>This is a testimonial test. This is a testimonial test.   This is a testimonial test. This is a testimonial test. This is a   testimonial test. This is a testimonial test. </p>
                  <p>This is a testimonial test. This is a testimonial test. This is a   testimonial test. This is a testimonial test. This is a testimonial   test. This is a testimonial test. This is a testimonial test. This is a testimonial test.   This is a testimonial test. This is a testimonial test. This is a   testimonial test. This is a testimonial test. </p>
                  <p>This is a testimonial test. This is a testimonial test. This is a   testimonial test. This is a testimonial test. This is a testimonial   test. This is a testimonial test. </p>
                                    </blockquote>
                                        <div class="nm-author"><span class="nm-name">Name</span> (Website<span class="nm-title"></span> ~ <span class="nm-auth-url"><a 
    href="http://www.google.com/">www.google.com</a></span> , <span class="nm-location">Mine</span> ) </div>
                <!--eof nm-author --> 
              </div>
              <!--eof nm-content -->
                        <!--eof wrapper --> 
            </div>
          </div>
        </div>
      </div>
      <!--eof foreach -->
        <!--bof nm-wrapper -->
      <div class="nm-wrapper">
        <div>
          <div>
            <div>
                                  <div class="nm-content">
                <div class="nm-avatar-wrapper">
                               <!-- <div class="nm-avatar">
                    <img name="Joe Donnely" 
    src="index.php_files/20091202025022_face2.jpg" alt="" height="100">     
             </div> -->
                              </div>
                                                    <blockquote 
    class="nm-testimonial">
                  <p>This is a testimonial test. This is a testimonial test.   This is a testimonial test. This is a testimonial test. This is a   testimonial test. This is a testimonial test. </p>
                  <p>This is a testimonial test. This is a testimonial test. This is a   testimonial test. This is a testimonial test. This is a testimonial   test. This is a testimonial test. </p>
                                    </blockquote>
                                        <div class="nm-author"><span class="nm-name">Name</span> (Website<span class="nm-title"></span> ~ <span class="nm-auth-url"><a 
    href="http://www.google.com/">www.google.com</a></span> , <span class="nm-location">Mine</span> ) </div>
                <!--eof nm-author --> 
              </div>
              <!--eof nm-content -->
                        <!--eof wrapper --> 
            </div>
          </div>
        </div>
      </div>
      <!--eof foreach -->
        <!--bof nm-wrapper -->
      <div class="nm-wrapper">
        <div>
          <div>
            <div>
                                  <div class="nm-content">
                <div class="nm-avatar-wrapper">
                              </div>
                                                    <blockquote 
    class="nm-testimonial">
                  <p>This is a testimonial test. This is a testimonial test.   This is a testimonial test. This is a testimonial test. This is a   testimonial test. This is a testimonial test. </p>
                  <p>This is a testimonial test. This is a testimonial test. This is a   testimonial test. This is a testimonial test. This is a testimonial   test. This is a testimonial test. This is a testimonial test. This is a testimonial test.   This is a testimonial test. This is a testimonial test. This is a   testimonial test. This is a testimonial test. </p>
                  <p>This is a testimonial test. This is a testimonial test. This is a   testimonial test. This is a testimonial test. This is a testimonial   test. This is a testimonial test. </p>
                                    </blockquote>
                                        <div class="nm-author"><span class="nm-name">Name</span> (Website<span class="nm-title"></span> ~ <span class="nm-auth-url"><a 
    href="http://www.google.com/">www.google.com</a></span> , <span class="nm-location">Mine</span> ) </div>
                <!--eof nm-author --> 
              </div>
              <!--eof nm-content -->
                        <!--eof wrapper --> 
            </div>
          </div>
        </div>
      </div>
      <!--eof foreach -->
        <!--bof nm-wrapper -->
      <div class="nm-wrapper">
        <div>
          <div>
            <div>
                                  <div class="nm-content">
                <div class="nm-avatar-wrapper">
                              </div>
                                                    <blockquote 
    class="nm-testimonial">
                  <p>This is a testimonial test. This is a testimonial test.
     This is a testimonial test. This is a testimonial test. This is a 
    testimonial test. This is a testimonial test. </p>
    <p>This is a testimonial test. This is a testimonial test. This is a 
    testimonial test. This is a testimonial test. This is a testimonial 
    test. This is a testimonial test. <br>
    </p>
                </blockquote>
                                        <div class="nm-author"><span class="nm-name">Name</span> (Website<span class="nm-title"></span> ~ <span class="nm-auth-url"><a 
    href="http://www.google.com/">www.google.com</a></span> , <span class="nm-location">Mine</span> ) </div></div></div></div></div></div></div>
    HTML:
    Waiting for your solution.
    Thanks in advance!
     
    eyashwant, Jun 24, 2010 IP
  2. obehi

    obehi Member

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #2
    That block of code is really ugly :)
    I didnt read it but in general, to center a div you style it like this:

    div{
    margin: 0 auto;
    }

    you could also something like this(if memory serves me right):

    div{
    position: absolute;
    width: 400px;
    left: 50%;
    margin-left: 200px; (div width/2)
    }

    The first method is the best tho.
     
    obehi, Jun 24, 2010 IP
  3. designrichs

    designrichs Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi,

    By using the css margin:0 auto; or margin: auto; works to center.

    Thank you!

    Best Regards,
    DesignRichs
     
    designrichs, Jun 24, 2010 IP
  4. eyashwant

    eyashwant Active Member

    Messages:
    885
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #4
    Hello, I have fixed it myself with a much simpler margin params :)
     
    eyashwant, Jun 25, 2010 IP