www.webdeveloper.com
Results 1 to 6 of 6

Thread: Percentile Width Problems

  1. #1
    Join Date
    Dec 2002
    Location
    Manhattan, KS
    Posts
    133

    Percentile Width Problems

    Test site to reference:
    http://www.ksu.edu/projects/carbon/james/

    Ok, in all browsers that use the mozilla code base are fine, however in IE6, the section below the tabs gets messed up. What happens is that the left side of the whole page gets mashed against the side of the viewport, though it should have a 20px gap between the content and the viewport. I've determined this is because my div, #centerContent, and my div, #rightContent, use %'s for widths, 80% and 20%, respectively. When I lower one of the percentages, to, say 79% or 19%, it looks fine until you start to shrink the window. Then, after a while - depending on how much I lowered the original percentage, the same problem occurs.

    Any way to fix / get around this in IE6?

    Thanks.

  2. #2
    Join Date
    Feb 2003
    Location
    Michigan, USA
    Posts
    5,755
    You're running into the "50% + 50% does not equal 100%" problem.

    When not defining widths in tables, the width of an element is taken in respect to ONLY it's parent element. Meaning that two DIVs have no relation to one another. Take this example:
    Code:
    <style type="text/css">
    <!--
    #container {
      width: 101px;
    }
    
    #left {
      width: 50%;
      float: left;
    }
    
    #right {
      width: 50%;
      float: left;
    }
    -->
    </style>
    .
    .
    .
    <body>
    <div id="container">
     <div id="left">&amp;nbsp;</div>
     <div id="right">&amp;nbsp;</div>
    </div>
    #left and #right have no relation to one another, other than their parent element is 101 pixels wide. When the browser calculates the width of #left, it simply does:

    .5 * 101 = 50.5

    The problem is, a computer screen cannot display 50.5 pixels, so the browser rounds it up to 51 pixels. It repeats the same calculation for #right. And gets the same pixel value: 51 pixels.

    #left is calculated to be 51 pixels wide. #right is ALSO calculated to be 51 pixels wide, for a total width of 102 pixels. The #container DIV that houses both #left and #right is only 101 pixels wide, and thus #left and #right do not fit side-by-side.

    This is a rather simple example of what's going on in your page. Somewhere along the line, the width of an element requires the browser to round up or down, the number of pixels wide the element is, making one element too wide to fit in the alotted space.

    Tables are different. The browser KNOWS that the pixel values of the cell widths MUST be equal to the total table width, and fudges the cell dimensions to get things to fit. This does not occur when sizing DIVs using percentages.

    The workaround is simple. Use floats and margins:
    Code:
    <style type="text/css">
    <!--
    #container {
      width: 80%;
      float: left;
    }
    
    #left {
      width: 180px;
      float: left;
    }
    
    #center {
      margin-left: 180px;
    }
    
    #right {
      margin-left: 80%;
    }
    
    #
    -->
    </style>
    .
    .
    .
    <body>
    <div id="container">
     <div id="left">&amp;nbsp;</div>
     <div id="center">&amp;nbsp;</div>
    </div>
    <div id="right">&amp;nbsp;</div>
    That untested code should work. Like I said, it should, but I don't have the time to fool around with it myself.

    Hope this helps out. Essentially, the workaround (for all browsers) is to float one DIV, give it a width. In the DIV you want to float beside it, DON'T float it, and DON'T give it a width. Give it a left or right margin equal to the width of the floated DIV.

  3. #3
    Join Date
    Mar 2003
    Location
    Escondido, CA
    Posts
    998
    toicontien,

    When you finish being a student, you should become a teacher. Seriously.
    Computer Zen:
    Program aborting
    Close all that you have worked on
    You ask far too much

  4. #4
    Join Date
    Feb 2003
    Location
    Michigan, USA
    Posts
    5,755

  5. #5
    Join Date
    Mar 2003
    Location
    Escondido, CA
    Posts
    998
    You're welcome. It's a very difficult thing to explain a technical issue in simple terms.

    Now you may have a problem; when I need something explained so I can understand it, I know who to ask!
    Computer Zen:
    Program aborting
    Close all that you have worked on
    You ask far too much

  6. #6
    Join Date
    Dec 2002
    Location
    Manhattan, KS
    Posts
    133
    I ended up getting rid of the percentages, as I needed a fixed width on the right side as well as the left. As such, this shored up the problem I was having, however this has oddly created a margin/padding problem for my center section. Instead of posting it here, I posted it as a new thread. The title is "Freaky Padding/Margin Problems", feel free to take a look and help me out.

    Thanks.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles