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?
13 Answers
Use display: inline-block
, though you need to wrap it inside a DIV to keep it from actually displaying inline. Tested in Safari.
<style type="text/css">
.fieldset-auto-width {
display: inline-block;
}
</style>
<div>
<fieldset class="fieldset-auto-width">
<legend>Blah</legend>
...
</fieldset>
</div>

- 524,688
- 99
- 697
- 795
-
Yeah, this is a better/ simpler idea than my answer if it works across all the browsers you care about. – Tom Mar 22 '11 at 18:12
-
3FYI: `display: inline-block;` doesn't work in iE6 and IE7. Happy you if you don't have to care about them. :) – Diemo Jul 30 '11 at 16:06
-
Works great in IE8 and Firefox 10! – Ed Bayiates Feb 10 '12 at 04:40
-
What is the voodoo with the div. It appears necessary, but why? – superluminary Jan 06 '17 at 12:19
-
@superluminary Normally a `fieldset` will display as a block element the full width of the page. Making it inline, rather than block, allows the width to collapse to the content, but removes it's role as preserving the vertical flow of the page. Wrapping it in the `div`, another block element, compensates for that and preserves the vertical flow. – tvanfosson Jan 06 '17 at 13:04
-
1The solution didn't work for me. I had to set `fieldset{ min-width: auto; }` as fieldset uses min-width as -webkit-min-content, as mentioned in the following post - https://stackoverflow.com/questions/27660423/fieldset-width-100-of-parent. – Naren Feb 16 '18 at 10:21
fieldset {display:inline}
or fieldset {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.

- 47,466
- 33
- 109
- 111
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.

- 22,301
- 5
- 63
- 96
This also works.
fieldset {
width:0px;
}

- 4,433
- 13
- 54
- 92

- 21
- 4
-
1This doesn't work if you have multiple elements within the fieldset that should appear on the same line. – Matthew Schinckel Sep 21 '11 at 02:18
-
Sure it does, if you use a block element to wrap them or a combination of nowrap and explicit breaks. – dan.s.ward Sep 30 '11 at 15:39
Unfortunately neither display:inline-block
nor width: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 use clear:both
on the element that follows the form. Of course, it will be very obvious if you forget ;-)

- 30,738
- 21
- 105
- 131

- 11
- 1
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.

- 12,163
- 2
- 42
- 48
-
but i want to have it dynamic so the fieldset will "autosize" and not be a predetermined width – leora Feb 20 '10 at 14:46
fieldset {
min-width: 0;
max-width: 100%;
width: 100%;
}

- 627
- 5
- 9
-
3
-
Not even Tim Berners-Lee could tell you why this work but shit just works lol thanks! – lucasjohnson Apr 22 '19 at 00:33
try this
<fieldset style="max-width: max-content;" >
<legend>Blah</legend>
</fieldset>

- 8,948
- 5
- 48
- 51

- 1,056
- 9
- 10
-
I think you should apply style="max-width: max-content;" on the fieldset tag – Benoît Jun 30 '22 at 14:46
-
Yes, it works nicely, after applying @Benoît 's suggestion (which I edited into the answer). – András Aszódi Jun 30 '23 at 14:57
i fixed my issue by override legend style as Below
.ui-fieldset-legend
{
font-size: 1.2em;
font-weight: bold;
display: inline-block;
width: auto;`enter code here`
}

- 345
- 4
- 8
Going further of Mihai solution, cross-browser left aligned:
<TABLE>
<TR>
<TD>
<FORM>
<FIELDSET>
...
</FIELDSET>
</FORM>
</TD>
</TR>
</TABLE>
Cross-browser right aligned:
<TABLE>
<TR>
<TD WIDTH=100%></TD>
<TD>
<FORM>
<FIELDSET>
...
</FIELDSET>
</FORM>
</TD>
</TR>
</TABLE>

- 1
-
2While this might work, tables aren't usually considered good practice for layouts...http://stackoverflow.com/questions/83073/why-not-use-tables-for-layout-in-html – StackExchange What The Heck Dec 20 '13 at 17:41
-
1
-
1For any new devs coming here. Don't do this. You'll use 10 times more code than you need. – superluminary Jan 10 '17 at 14:02
<table style="position: relative; top: -0px; left: 0px;">
<tr>
<td>
<div>
<fieldset style="width:0px">
<legend>A legend</legend>
<br/>
<table cellspacing="0" align="left">
<tbody>
<tr>
<td align='left' style="white-space: nowrap;">
</td>
</tr>
</tbody>
</table>
</fieldset>
</div>
</td>
</tr>
</table>
You can also put the fieldset inside a table, like so:
<table>
<tr>
<td>
<fieldset>
.......
</fieldset>
</td>
</tr>
</table>

- 1