当我减小浏览器窗口的宽度时,尽管有以下命令,但select
内部元素的fieldset
大小不会减小max-width
:
<fieldset style="background:blue;">
<select name=countries style="max-width:90%;">
<option value=gs>South Georgia and the South Sandwich Islands</option>
</select>
</fieldset>
Run Code Online (Sandbox Code Playgroud)
但是,这在fieldset
元素之外完美地起作用.如何使select
元素缩小到max-width
字段集中的(百分比)?
注意:我测试了Firefox 12.0和Google Chrome.我现在确定这是一个跨浏览器的问题.
澄清:请参考这个例子并注意一个select
元素在a内部fieldset
和另一个元素外部的行为之间的区别fieldset
.我想要实现的是,其中的select
元素fieldset
表现得像fieldset
元素之外的元素.
Zet*_*eta 64
这是不可能的,至少如果你只使用max-width
(见下面的解决方案).对于样式来说,它们<select>
总是有点棘手,因为它们是交互式内容元素 和表单控件元素.因此,他们必须遵循一些隐含的规则.例如,使用时,您不能选择宽于其中一个选项的选项max-width
.考虑以下情况:
+------------------------------------+-+ |Another entry |v| +------------------------------------+-+ |Another entry | |Select box, select anything, please | |Another entry | |Another entry | +------------------------------------+-+
让我们说你要挤压它<select>
- 会发生什么?选择的宽度会变小,直到......
+------------------------------------+-+ +-----------------------------------+-+ |Another entry |v| |Another entry |v| +------------------------------------+-+ +-----------------------------------+-+ |Another entry | |Another entry | |Select box, select anything, please |-->|Select box, select anything, please | |Another entry | |Another entry | |Another entry | |Another entry | +------------------------------------+-+ +-----------------------------------+-+ | +---------------------------------------+ v +----------------------------------+-+ +---------------------------------+-+ |Another entry |v| |Another entry |v| +----------------------------------+-+ +---------------------------------+-+ |Another entry | |Another entry | |Select box, select anything, please |-->|Select box, select anything, please| |Another entry | |Another entry | |Another entry | |Another entry | +----------------------------------+-+ +---------------------------------+-+
然后过程将停止,因为<option>
s不再适合.请记住,如果<option>s
没有一些令人讨厌的怪癖,你不能设置样式或至少只有一点(颜色,字体变体).但是,如果正确准备了选择,则可以更改边框:
使用上的width
值select
.是的,这很容易:
<fieldset style="background:blue;">
<select name=countries style="width:100%;max-width:90%;">
<option value=gs>South Georgia and the South Sandwich Islands</option>
</select>
</fieldset>
Run Code Online (Sandbox Code Playgroud)
为什么这样做?由于option
意志都承认width
的select
正确,并不会迫使select
有一个隐含的min-width
.请注意,这width
是荒谬的,因为它不仅仅是max-width
.max-width
在这种情况下,大多数浏览器都不会关心和使用它,因为它提供了一个上限.
JSFiddle演示(适用于FF12,Chrome 18,IE9,Opera 11.60)
基于包装器的解决方案,这不会改变原始宽度:
<fieldset style="background:blue;">
<div style="display:inline-block;max-width:90%;"> <!-- additional wrapper -->
<select name=countries style="width:100%">
<option value=gs>South Georgia and the South Sandwich Islands</option>
</select>
</div>
</fieldset>
Run Code Online (Sandbox Code Playgroud)
JSFiddle演示(适用于上面列出的浏览器)