Center Align in Div

Discussion in 'CSS' started by umlove, Apr 29, 2012.

  1. #1
    Hello guys,

    I know this question may have been asked in several times in several places. I kept on finding the solution for this problem but could not understand. I want to know step by step. First of all I will tell how I do center align my website.
    i use a table and a cell i give width="985" and height= (As per I need) to both in table and in td I mean in cell. In that cell I use div with the id = main and in css I make # main as position absolute, z-index : 1 , width and height (same as in table and in cell), top = 0 and. That's it and my site goes centered. I do not give left px otherwise it would be called as for browser. Now I want to learn step by step how we can make div centered. I use fake method or else it is not professional. I want to learn the real method. Can anybody teach here? please I need this. Thanks
     
    umlove, Apr 29, 2012 IP
  2. umlove

    umlove Active Member

    Messages:
    271
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    83
    #2
    I want to learn that codes which make a div align center on every browser. Please tell me.
     
    umlove, Apr 29, 2012 IP
  3. j_o

    j_o Well-Known Member

    Messages:
    516
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    113
    #3
    To center a div I use

    
    margin: 0px auto;
    
    Code (markup):
    To center text I do:
    
    text-align:center;
    
    Code (markup):
    I generally avoid tables and use inline divs to move everything to its correct location.
     
    j_o, Apr 29, 2012 IP
  4. singh1720

    singh1720 Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Could give me a sample page. That will help me to understand.
     
    singh1720, Apr 29, 2012 IP
  5. webcosmo

    webcosmo Notable Member

    Messages:
    5,840
    Likes Received:
    153
    Best Answers:
    2
    Trophy Points:
    255
    #5
    thats very old style, you should learn css well.

    here is how i would do this.

    CSS code:
    html, body {margin:0;padding:0;height:100%;}
    #main {width:985px;margin:0 auto;overflow:hidden;}

    HTML code:

    <div id="main">
    content..................
    </div>
     
    webcosmo, Apr 29, 2012 IP
  6. umlove

    umlove Active Member

    Messages:
    271
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    83
    #6
    What if I want to insert more div in main so then I can use top and left px? so that I can move that to anywhere? But that will be in the main div ?
     
    umlove, Apr 29, 2012 IP
  7. umlove

    umlove Active Member

    Messages:
    271
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    83
    #7
    I did this, my second div does not work in main div. any solution? Or I didn't get it properly?
     
    umlove, Apr 29, 2012 IP
  8. simoncrequer

    simoncrequer Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Make a wrapper class:
    .wrapper {
      width: 895px;
      margin: 0 auto;
    }
    Code (markup):
    Put everything you want centered inside a wrapper div:
    <div class="wrapper">
    .
    .
    .
    </div>
    Code (markup):
     
    simoncrequer, May 3, 2012 IP