1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Hod Do I Center Div Tag?

Discussion in 'CSS' started by gobbly2100, Aug 1, 2007.

  1. #1
    How do I center a Div tag? I thought I just used something like the following:


    HTML
    <div class="main">
    <h1>Title</h1>
    </div>
    Code (markup):
    CSS
    div.main {
    float:center:
    }
    Code (markup):
     
    gobbly2100, Aug 1, 2007 IP
  2. Magnus

    Magnus Peon

    Messages:
    164
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    body {
    text-align: center; /* for IE */
    }

    div.main {
    text-align: left; /* text align is inherited */
    width: 500px;
    margin-left: auto;
    margin-right: auto;
    }
     
    Magnus, Aug 1, 2007 IP
  3. gobbly2100

    gobbly2100 Banned

    Messages:
    906
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Will that also center the rest of my site for any content within those div tags?
     
    gobbly2100, Aug 1, 2007 IP
  4. Magnus

    Magnus Peon

    Messages:
    164
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    No. That won't center the stuff in the div tags, but you can do that by setting the text align to center and the left and right margins to auto.

    Maybe if you have an example or something, and I can explain better. Maybe even code it for you :)
     
    Magnus, Aug 1, 2007 IP
  5. vahsi000

    vahsi000 Well-Known Member

    Messages:
    706
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    140
    #5
    There's no such attribute as center for flow...
    try assiging margin:auto; to the div tag and setting the width of the div layer...
    
    .main {
    margin:auto;
    width:500px;
    text-aligh:center;
    }
    
    Code (markup):
     
    vahsi000, Aug 1, 2007 IP
  6. Dan Schulz

    Dan Schulz Peon

    Messages:
    6,032
    Likes Received:
    436
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Ok. Rule number one. If you're using a complete and proper DOCTYPE, IE 6/7 will play by the rules and all that bloat won't be necessary.

    Rule number two. Set a width on the DIV and then set the margins to 0 auto; (like so)

    
    .main {
        margin: 0 auto;
        width: 750px;
    }
    
    Code (markup):
    Problem solved. The trick that Magnus suggested is ONLY if you have to support IE 5 and 5.5 (unless you don't have a complete and proper DOCTYPE, in whcih 6 as well - but that can be cured by using one in the first place).
     
    Dan Schulz, Aug 1, 2007 IP
  7. Crazymik3

    Crazymik3 Peon

    Messages:
    55
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    margin: 0 auto; as people have said is the way to go.
    Also, a set width must be in place.
     
    Crazymik3, Aug 3, 2007 IP