DOCTYPE Strict uses "correct" box model - so why is 100% width now useless?

Discussion in 'ASP .Net' started by Shadow Lynx, Mar 11, 2006.

  1. Shadow Lynx

    Shadow Lynx Guest

    Consider this simple HTML:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 STRICT//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
    <title>Strict kills my widths!</title>
    </head>
    <body>
    <table style="width:400px; table-layout:fixed;">
    <colgroup>
    <col style="width:50%;"/ >
    <col style="width:50%;"/ >
    </colgroup>
    <tr>
    <td>
    <input type="text" style="width:100%;" />
    </td>
    <td>
    <input type="text" style="width:100%;" />
    </td>
    </tr>
    </table>
    </body>
    </html>

    Both text boxes have their right edges clipped off. Sure, the "Proper"
    box model says that width is the width of the content, excluding the
    padding and border (and margin, of course.) Wonderful. So now, with
    all standard text boxes (and many other controls) having things like
    padding and borders, what good is a percentage width?

    I could (and do, for lack of a better workaround) use a slightly
    smaller width, like 98%, but that has to be adjusted depending on the
    width of the table and how many columns it has - smaller tables require
    even more reduction in percentage width for the text boxes to fit.

    I know the geniuses in the W3C think that their box model is the best,
    but if you draw a box (essentially a border) and then write some text
    in the middle of it with some space between the edge of the box and the
    text, then ask any kid to measure the width of the box, he'll measure
    from border to border, INCLUDING padding!

    I know, I should stop whining and just stop using STRICT, right? Well,
    I could, but that's a step backwards and there are other parts of the
    strict doctype that are very appealing.

    There's got to be a viable solution to this. Sure, 100% minus padding
    minus border is the real answer, but since that's not possible (without
    using dynamic properties) to put specify such a formula as width, how
    can I solve this issue?

    I FEEL LIKE I'M TAKING CRAZY PILLS!
    Shadow Lynx, Mar 11, 2006
    #1
    1. Advertising

  2. Shadow Lynx

    Shadow Lynx Guest

    I explored the Dynamic Properties avenue and came up with the following
    css/function combo that will do what I consider to be proper width
    calculations, although it's certainly not a perfect calculation since
    it doesn't take into account every scenario. It's a relatively
    intensive function when used/called by many different controls on a
    page.

    The real point is, why the heck should I need such a thing!!?? Please,
    someone tell me I wasted my time by doing this because there really IS
    a much better and less complex way of accomplishing this when using
    Strict!

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 STRICT//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
    <title>Strict kills my widths!</title>
    <style>
    .Auto100 {
    width: expression(autoWidth('100%', this));
    }
    </style>
    <script language="javascript">
    // relWidth should be a percentage or a pixel offset (ex. -10 or
    '100%')
    // TABLEs must have table-layout:fixed or this will result in
    infinite recursion!
    // Note: If the arguments were (obj, relWidth), this would silently
    fail to execute,
    // and I have no idea why - the object MUST be the last argument.
    function autoWidth(relWidth, obj) {
    var defaultCellPadding = 1;
    var cellPadding = 0;
    if (obj.parentNode.tagName == "TD") {
    var parentTable =
    obj.parentNode.parentNode.parentNode.parentNode;
    cellPadding = (parentTable.cellPadding) ?
    parentTable.cellPadding : defaultCellPadding;
    }
    var objDiff = (obj.offsetWidth - obj.style.pixelWidth) +
    (cellPadding * 2);
    var maxWidth = obj.parentNode.clientWidth - objDiff;
    if (typeof(relWidth) == "string" && relWidth.slice(-1) == "%") {
    var pctValue = relWidth.slice(0, -1) * .01;
    newWidth = parseInt(maxWidth * pctValue, 10) + "px";
    } else {
    newWidth = (maxWidth + parseInt(relWidth, 10)) + "px";
    }
    return newWidth;
    }
    </script>
    </head>
    <body>
    <table style="width:700px; table-layout:fixed;" cellspacing="10"
    cellpadding="3">
    <colgroup>
    <col style="width:50%;"/ >
    <col style="width:50%;"/ >
    </colgroup>
    <tr>
    <td colspan="2">With the workaround - everything fits as it
    should:</td>
    </tr>
    <tr>
    <td bgcolor="lightgreen">
    <input type="text" class="Auto100" value="TextBox" />
    </td>
    <td bgcolor="orange">
    <select style="width:100%;"><option>Drop Down
    List</option></select>
    <!-- SELECTs are windowed: Auto100 won't work and isn't needed
    -->
    </td>
    </tr>
    <tr>
    <td bgcolor="magenta">
    <textarea class="Auto100">TextArea</textarea>
    </td>
    <td bgcolor="tan">
    <div style="background-color:Window; padding:5px; border:1px
    solid red;" class="Auto100">DIV</div>
    </td>
    </tr>
    <tr>
    <td colspan="2"><br>
    Without the workaround (normal percentages) - everything but
    SELECTs are clipped:</td>
    </tr>
    <tr>
    <td bgcolor="lightgreen">
    <input type="text" style="width:100%;" value="TextBox" />
    </td>
    <td bgcolor="orange">
    <select style="width:100%;"><option>Drop Down
    List</option></select>
    <!-- SELECTs are windowed: Auto100 won't work and isn't needed
    -->
    </td>
    </tr>
    <tr>
    <td bgcolor="magenta">
    <textarea style="width:100%;">TextArea</textarea>
    </td>
    <td bgcolor="tan">
    <div style="width:100%; background-color:Window; padding:5px;
    border:1px solid red;" class="Auto100">DIV</div>
    </td>
    </tr>
    </table>
    </body>
    </html>

    --- Watch out for forced newsgroup word wrapping!
    Shadow Lynx, Mar 11, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,853
    Smokey Grindel
    Dec 2, 2006
  2. Daniel Nogradi
    Replies:
    0
    Views:
    383
    Daniel Nogradi
    Nov 15, 2006
  3. Replies:
    7
    Views:
    178
    Tad McClellan
    Jun 22, 2006
  4. fred
    Replies:
    3
    Views:
    278
    Zifud
    Mar 17, 2005
  5. Replies:
    5
    Views:
    886
Loading...

Share This Page