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.

div, table, width, center ?

Discussion in 'CSS' started by atlas, Jul 19, 2005.

  1. #1
    Hello everybody

    I have a question about CSS of course.

    Fisrt of all, I drew a little picture to explain what i would like to do.

    [​IMG]

    In red: a div. I would like to center it on the page (horizontally)

    In purple: Another div inside the first one. I would like that it take all width of the first div automatically.

    In blue: A table with a random width (because the width is variable with the text in the cell)


    What I would like:

    I would like that my first div (the red one) take the same width than my table.
    I've already try to add a style like this: "width:1px" in the first div. It's ok for the first one, it has the same size as my table but the second div (the purple one) is only 1px :(

    So I've replaced my width:1px by a float:left. It's better but the first div (and it's child) are not center anymore (of course because it's a LEFT float.)
    I need a float: center :) but it dosen't exist.


    Thx a lot!

    Atlas
     
    atlas, Jul 19, 2005 IP
  2. Dji-man

    Dji-man Peon

    Messages:
    185
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Since you are using a table to set your body width, why don't you simply move your child div in your table and align it center?
     
    Dji-man, Jul 19, 2005 IP
  3. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #3
    See this thread - 7061
     
    J.D., Jul 19, 2005 IP
  4. atlas

    atlas Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    In fact, I don't want to do it because I want use the THEAD, TBODY and TFOOT tags in my table, in order to be able to scroll it without the header and the footer (a similar stuff to that: http://www.imaputz.com/cssStuff/bigFourVersion.html"]http://www.imaputz.com/cssStuff/bigFourVersion.html
    (sorry, i can't add hyperlink linkable yet on this forum)

    So i just want to have ONE "header" row in my table.
    Thx Dji-man.

    And thx you J.D. for the thread, but I can't find the solution inside.
     
    atlas, Jul 20, 2005 IP
  5. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #5
    It does describe how to center a block-level element isn't this what you are looking for?
     
    J.D., Jul 20, 2005 IP
  6. atlas

    atlas Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6

    Yes, but not just that :)

    In your example, the first div have a static width and mine not.
    I would like that my div grow up or shrink like the table that's inside the div.

    I know that I can put a width of 1px for the div and if the table will be larger (100px for exemple), the div will have the same size too.
    But the problem is the second div (the purple one). Cause this one won't have the 100px but just 1px (like the first div)

    So that's my problem :(
     
    atlas, Jul 20, 2005 IP
  7. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #7
    It's just an example. Make width in %'s and it will do what you need. The rule is that if any block-level element is shorter than the containing element's width anf its margin-left and margin-right are set to auto, it should be centered.

    J.D.
     
    J.D., Jul 20, 2005 IP
  8. atlas

    atlas Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Ok, so here's what I understand:

    
    <div style="border: 3px solid red; width:20%; margin:auto;">
    
    <div style="border: 5px solid purple; width:100%; margin:auto;">2nd div</div>
    
    <table border="1" cellspacing="0" cellpadding="0">
      <tr>
        <td>0000</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>444444444444</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>11111</td>
        <td>&nbsp;</td>
        <td>33333</td>
        <td>&nbsp;</td>
      </tr>
    
    </table>
    
    </div>
    
    Code (markup):

    demo here: http://back2web.com/css.html

    :eek:
     
    atlas, Jul 20, 2005 IP
  9. atlas

    atlas Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    So i guess that it's impossible :confused: ?
     
    atlas, Jul 21, 2005 IP
  10. J.D.

    J.D. Peon

    Messages:
    1,198
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Lack of a reply doesn't mean it's impossible ;) Look at your test in IE and FF to see the diffeernce between how these two handle div's. IE will expand the div with width or height set to a non-auto value and FF will overflow it. On top of that IE and FF treat borders differently. Tables never overflow. So, a quick solution is to use tables for what you need. You can also experiment with setting margin-left/right to, say 20% and see if this will help you (the outer div will still overflow, though, when the width is too small).

    J.D.
     
    J.D., Jul 21, 2005 IP
  11. atlas

    atlas Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Thx a lot, i will try that
     
    atlas, Jul 22, 2005 IP