有没有办法让字段集宽度仅与其中的控件一样宽?
看来 fieldset 默认为其容器的 100% 宽度。有没有什么方法可以让字段集与字段集中最宽的控件一样大?
It seems that fieldset defaults to 100% width of its container. Is there any way that you can have the field set just be as big as the widest control inside the fieldset?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
这也有效。
This also works.
不幸的是,
display:inline-block
和width:0px
在 Internet Explorer 8 及之前版本中均不起作用。我还没有尝试过 Internet Explorer 9。尽管我很想忽略 Internet Explorer,但我不能。适用于 Firefox 和 Internet Explorer 8 的唯一选项是
float:left
。唯一的小缺点是您必须记住在表单后面的元素上使用clear:both
。当然,如果你忘记了,那就很明显了;-)Unfortunately neither
display:inline-block
norwidth:0px
works in Internet Explorer up to version 8. I have not tried Internet Explorer 9. Much as I would like to ignore Internet Explorer, I can't.The only option that works on Firefox and Internet Explorer 8 is
float:left
. The only slight drawback is that you have to remember to useclear:both
on the element that follows the form. Of course, it will be very obvious if you forget ;-)试试这个
try this
您始终可以使用 CSS 来限制字段集的宽度,这也会限制内部的控件。
我发现我经常必须限制
select
控件的宽度,否则太长的选项文本将使其完全难以管理。You can always use CSS to constrain the width of the fieldset, which would also constrain the controls inside.
I find that I often have to constrain the width of
select
controls, or else really long option text will make it totally unmanageable.width: fit-content;
有效。width: fit-content;
works.我通过覆盖图例样式解决了我的问题,如下所示
i fixed my issue by override legend style as Below
进一步Mihai解决方案,跨浏览器左对齐:
跨浏览器右对齐:
Going further of Mihai solution, cross-browser left aligned:
Cross-browser right aligned:
您还可以将字段集放入表中,如下所示:
You can also put the fieldset inside a table, like so:
使用
display: inline-block
,但您需要将其包装在 DIV 中以防止其实际显示内联。在 Safari 中测试。Use
display: inline-block
, though you need to wrap it inside a DIV to keep it from actually displaying inline. Tested in Safari.fieldset {display:inline}
或fieldset {display:inline-block}
如果要垂直分隔两个字段集,请使用单个
他们之间。这在语义上是正确的,而且并不困难。fieldset {display:inline}
orfieldset {display:inline-block}
If you want to separate two fieldsets vertically, use a single
<br/>
between them. This is semantically correct and no harder than it has to be.您可以浮动它,然后它的宽度将仅与其内容一样宽,但您必须确保清除这些浮动。
You could float it, then it will only be as wide as its contents, but you'll have to make sure you clear those floats.