Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Div not collapsing to fit contents

  1. #1
    Join Date
    Jul 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Div not collapsing to fit contents

    I have a slight problem with a div in Opera. Here is the HTML:

    HTML Code:
    <div id="sidebarcol1">
      <div id="side1row1">Account value</div>
      <div id="side1row2">Account funds</div>
      <div id="side1row3">Open P/L</div>
      <div id="side1row4">Today's P/L</div>
    </div>
    Here is the CSS:

    Code:
    *{
      float:left;
      display:inline;
    }
    
    ...
    
    div#sidebarcol1{
      color:#898989;
      position:relative;
      margin:12px 0px 0px 10px;
      border:1px solid #f00; /* For Testing */
    }
    
    div#sidebarcol1 *{
      clear:left;
      text-align:right;
      font-weight:bold;
      margin:3px 10px 0px 0px;
      border:1px solid #00f; /* For Testing */
    }
    As you can see, this is a column of data with four rows inside. I put a border around the rows and I can see that the column expands in width to fill its container instead of contracting to fit the rows. Is there any way for me to get around this? What am I doing wrong?

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Code:
    body * {
      float:left;
    }
    
    div#sidebarcol1 {
      color:#898989;
      margin:12px 0px 0px 10px;
      border:1px solid #f00; /* For Testing */
    }
    
    div#sidebarcol1 * {
      float:none;
      text-align:right;
      font-weight:bold;
      margin:3px 10px 0px 0px;
      border:1px solid #00f; /* For Testing */
    }
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jul 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hmm... I guess I'll try star html hacking it instead of deleting it entirely.

    EDIT: Nope, that doesn't seem to be the problem. I star-html hacked it - no change. I tried commenting it - no change. Is there a way to explicitly tell it to collapse?

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Why hack? The above css, coupled with your HTML code looks identical in IE 7 and 6, Opera and FF when used on a valid page with at least a:

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
    DOCTYPE.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Jul 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I only use the display:inline; for IE 6 anyway, which is why I star html hacked it to prevent other browsers, namely Opera, to pick up on it.

    Getting rid of the position:relative; (it was a relic from when I tried to use absolute positioning on its child elements) didn't affect the bug either.

    Btw, I'm working with a XHTML 1.1 doctype.

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    XHTML has its own issues that I will leave to others to expound upon (both pro and con), but as long as it puts all of the browsers in standards or 'near standards' mode, it should be fine for this.

    There was no * html hack in your original posted css.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #7
    Join Date
    Jul 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I'm in standards compliant mode, and Opera is still giving me this bug.

    And yea, I said I would try star html hacking it to hide it from Opera, because you said that deleting it would fix it, so instead I star html hacked it.
    Last edited by sneamia; 07-26-2007 at 06:20 PM.

  8. #8
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    You cannot get any more standards compliant than this:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <style type="text/css">
    body * {
      float:left;
    }
    
    div#sidebarcol1 {
      color:#898989;
      margin:12px 0px 0px 10px;
      border:1px solid #f00; /* For Testing */
    }
    
    div#sidebarcol1 * {
      float:none;
      text-align:right;
      font-weight:bold;
      margin:3px 10px 0px 0px;
      border:1px solid #00f; /* For Testing */
    }
    </style>
    </head>
    <body>
    <div id="sidebarcol1">
      <div id="side1row1">Account value</div>
      <div id="side1row2">Account funds</div>
      <div id="side1row3">Open P/L</div>
      <div id="side1row4">Today's P/L</div>
    </div>
    </body>
    </html>
    It is identical in all of the browsers I mentioned.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #9
    Join Date
    Jul 2007
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    My bad, I didn't see that you changed it to float:none;. Why does it do that? I don't see why floating it makes it push the next div off the row.

  10. #10
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I can't really say for sure. The way I approached this and approach most things like this is to just get rid of everything that looks like it either does nothing and/or that might be a problem.

    The position:relative; didn't do anything one way or the other. You don't want to display:inline; if you want your lines to wrap after each div, but, you cannot float if you want to wrap after each div. Clearing only takes care of getting past a previous float, it doesn't prevent a subsequent float.

    You don't need to float:left; the body * selector, unless that is used by the rest of the page. You could apply that style just to the div#sidebarcol1 selector. Since you are selecting by id, you do not need to (if you have a valid page) specify the div element. Since it is really only the inner div's that the second style block below applies to, it would be good to specify that, in case you decided to nest another element, like a span in there someplace:

    The entire css could then be simplified:

    Code:
    #sidebarcol1 {
      float:left;
      color:#898989;
      margin:12px 0px 0px 10px;
      border:1px solid #f00; /* For Testing */
    }
    
    #sidebarcol1 div {
      text-align:right;
      font-weight:bold;
      margin:3px 10px 0px 0px;
      border:1px solid #00f; /* For Testing */
    }
    Opera is generally thought to be more compliant than either FF or IE, but it could have this one thing wrong. As I say, I cannot be sure. I don't code the browsers. I just code for them. BTW, I checked in Safari 3 Win, and the code I wrote before, and this simplified version looks the same there as in the rest of them.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •