Getting select to properly size width

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RichardR
    New Member
    • Jul 2008
    • 1

    Getting select to properly size width

    I have a webpage which has a table that contains a column with several drop down boxes (<SELECT>). The contents of the drop down boxes are dynamically populated so I have no idea what the actually width will be necessary.

    How do I tell the dropdown (<SELECT>) and/or column (<TD>) to automatically size itself to the greater of widest item in the dropdown, and then get the smaller dropdowns to fill the width of the column?

    If I specify in the SELECT tag, STYLE="{width: 100%;}" the dropdown boxes will expand to the column, but does not cause the column itself to expand to the width necessary to display the items in the dropdowns. When I specify STYLE="{width: auto}" in the SELECT tag, the dropdowns will expand to widest item in the dropdown, and causes the column to expand to hold the dropdown. But, the smaller dropdowns do not expand to fill the column width.

    I tried specifying "margin-left: 0px; margin-right: 0px;" and "margin-left: 0px; margin-right: 0px; display: table-cell;" in the SELECT's style but they didn't make any difference.

    Does anyone have any suggestions?

    Below is a plain HTML example with shows the issue. (BTW, I've been testing the code in IE 6.)

    Thanks,

    Richard

    Code:
    <html>
       <header>
       </header>
    
       <body>
          <table width="auto" border="1" cellspacing="0" cellpadding="0" >
             <tr height="auto">
                <td width="2">   <!-- just add a little spacing to the left edge -->
                </td>
          
                <td width="99%" align="left" valign="top" class="OrderImageName" nowrap>
                   <table>
                      <tr>
                         <td nowrap align="center">
                            some data goes here
                         </td>
                      </tr>
                   </table>
                </td>
    
                <td valign="top">
                   <table width="auto" border="1" cellspacing="0" cellpadding="0">
                      <tr>
                         <td>
                            &nbsp;
                         </td>
    
                         <td align="center" >
                            Types
                         </td>
    
                         <td width="2">   <!-- just add a little spacing to the right edge -->
                            &nbsp;
                         </td>
                      </tr>
    
                      <tr>
                         <td align="left" nowrap >
                            &nbsp;Item #1
                         </td>
    
                         <td nowrap>
                            <select style="{width: auto}" >
                               <option>&lt;none&gt;</option>
                               <option>aaaaa aaaaaa</option>
                            </select>
                         </td>
                      </tr>
    
                      <tr>
                         <td align="left" nowrap >
                            &nbsp;Item #2
                         </td>
    
                         <td nowrap>
                            <select style="{width: auto}" >
                               <option>&lt;none&gt;</option>
                               <option>aaaaaaa aaaa aaaaaa</option>
                               <option>aaaaaaa aaaa aaaaaa</option>
                               <option>aaaa</option>
                            </select>
                         </td>
                      </tr>
    
                      <tr>
                         <td align="left" nowrap >
                            &nbsp;Item #3
                         </td>
    
                         <td nowrap>
                            <select style="{width: auto}" >
                               <option>&lt;none&gt;</option>
                               <option>aaaa aaaaaa aa aaaaaa</option>
                               <option>aaaa aaaaaa aa aaaaaaa</option>
                               <option>aaaa aaaaaa aa aaaaaa</option>
                               <option>aaaa aaaaaa aaaa</option>
                               <option>aaaaaaaa aa aaaaaa</option>
                               <option>aaaaaaaa aa aaaaaaa</option>
                               <option>aaaaaaaa aa aaaaaa</option>
                               <option>aaaaaaaa aaaa</option>
                               <option>aaaaaaaa aa aaaaaa, aaaa</option>
                               <option>aaaaaaaa aa aaaaaaa, aaaa</option>
                               <option>aaaaaaaa aa aaaaaa, aaaa</option>
                               <option>aaaaaaa aaaaaaaa aa aaaaaa</option>
                               <option>aaaaaaa aaaaaaaa aa aaaaaaa</option>
                               <option>aaaaaaa aaaaaaaa aa aaaaaa</option>
                               <option>aaaaaaa aaaaaaaa aaaa</option>
                               <option>aaaaaaa aaaaaaaa aa aaaaaa, aaaa</option>
                               <option>aaaaaaa aaaaaaaa aa aaaaaaa, aaaa</option>
                               <option>aaaaaaa aaaaaaaa aa aaaaaa, aaaa</option>
                               <option>aaaaaaa</option>
                            </select>
                         </td>
                      </tr>
                   </table>
                </td>
    
                <td>
                   <table height="auto" >
                      <tr height="50%" valign="top" >
                         <td valign="top" align="right" >
                             XXXXXX
                         </td>
                      </tr>
    
                      <tr height="50%" >
                         <td valign="bottom" align="right" >
                             YYYYYY
                         </td>
                      </tr>
                   </table>
                </td>
             </tr>
          </table>
       </body>
    </html>
  • henryrhenryr
    New Member
    • Jun 2007
    • 103

    #2
    How are you generating your page?

    If you are just coding the HTML, then specify width:100% on the smaller ones and width:auto on the biggest one.

    Probably it's generated dynamically. So make the standard rule width:100% and then in whatever you use to generate the HTML, determine which select will be longest (for example, count the characters of the <option> elements) and specify that one only to be width:auto. That way you will always have one select which is width:auto, forcing the td size to fit it, while the others simple expand to the td width.

    ie. you're trying to get this (assuming the third one is the biggest):

    [code=html]
    <table width="" cellspacing="0" cellpadding="0" border="1">
    <tbody>
    <tr>
    </tr>
    <tr>
    <td nowrap="" align="left"> Item #1 </td>
    <td nowrap="">
    <select style="width: 100%;">
    </select>
    </td>
    </tr>
    <tr>
    <td nowrap="" align="left"> Item #2 </td>
    <td nowrap="">
    <select style="width: 100%;">
    </select>
    </td>
    </tr>
    <tr>
    <td nowrap="" align="left"> Item #3 </td>
    <td nowrap="">
    <select style="width: auto;">
    </select>
    </td>
    </tr>
    </tbody>
    </table>
    [/code]
    Last edited by henryrhenryr; Jul 4 '08, 02:36 PM. Reason: extra detail

    Comment

    Working...