html中控制select的寬度

來源:互聯網
上載者:User
Controlling the width of SELECT lists
= Index DOT Html by Brian Wilson =

Main Index | Element Index | Element Tree | HTML Support History
The Issue | Possible Solutions | Recommendation

The Issue
The default width of a SELECT form control is usually dependent on the width of the widest OPTION item in the list; the width of the SELECT list will basically be not much greater (but no less) than this width.

The HTML standards don't allow for a way to control this width. CSS, on the other hand, would be perfect for controlling this, but historical support for CSS on form controls is rather weak.

Solution 1: Using   to pad OPTION element content
Support Key: [IE1|M2A1|N1|O2.1]

This solution can work rather well - non-breaking spaces are treated as displayable content, so these can be used to add crude right and left "padding" widths to the SELECT control. The one downside of this method is that it is difficult to achieve a precise width for the control.

Solution 2: Using a WIDTH attribute on the SELECT control
Support Key: [IE|M|N4-4.x|O]

This proprietary attribute was added in Netscape 4.x, and has been retired in Netscape 6.x, but this attribute takes as a value a positive integer representing a pixel width for the control. The disadvantage here is that only Netscape 4.x supports it.

Solution 3: CSS on the SELECT control
Support Key: [IE4|M|N6B1|O5]

This is by far the best solution, using the 'width' CSS property - and it has the widest support.

Recommendation

Since Netscape 4 has only the proprietary attribute that will give the desired effect, it will not hurt anything to combine the use of CSS and the WIDTH attribute.

Example
< select NAME="foo" WIDTH="300" STYLE="width: 300px">
    < option>one
    < option>two
    < option>three
</ select>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.