The default display for a fieldset is a square cornered border. In certain browsers (Firefox and Safari and perhaps others) you can use CSS to make rounded corners on the border around the fieldset and around the legend.
Look at this simple form:
<form id="example" name="example" method="post" action="send.php">
<fieldset>
<legend>A Fieldset</legend>
<label for="ex1">A text field</label>
<input type="text" name="ex1" id="ex1" />
</fieldset>
</form>
Here’s how that looks in Safari, with no CSS styling.
I applied these rules to the fieldset selector in the CSS
fieldset {
-webkit-border-radius: 8px;
-moz-border-radius: 8px;
border-radius: 8px;
}
Again in Safari, here’s what you see now.
The property used to create rounded corners is the border property. The rule is repeated three times, a redundancy meant to create the effect in as many browsers as possible. The border-radius rule is CSS3, not yet in standard adoption by all browsers. The webkit-border-radius rule is understood by all webkit browsers such as Safari. The moz-border-radius is understood by all mozilla based browsers such as Firefox.
A border can be added to the legend as well. That element can also display the rounded borders. Here’s the CSS.
legend {
border: solid 1px black;
-webkit-border-radius: 8px;
-moz-border-radius: 8px;
border-radius: 8px;
}
This is how it displays in Safari.
A couple of changes to show how the legend’s appearance might be improved can be made in the CSS.
legend {
background: #FF9;
border: solid 1px black;
-webkit-border-radius: 8px;
-moz-border-radius: 8px;
border-radius: 8px;
padding: 6px;
}
Now the legend displays like this.
Additional articles at Web Teacher about styling fieldsets with CSS:
This is one of the things students always ask for, so keep sharing the knowledge.
I will give one additional tip. Put experimental features first and the standard rule last. Firefox current implementation differs from the W3C spec. When they get the real thing out, you do not want to have that overridden.
I have written about that at
http://itpastorn.blogspot.com/2009/06/do-not-put-experimental-features-last.html
.-= Lars Gunther´s last blog ..Högre kvalitet på webb-utbildningen på gymnasiet =-.
Thanks for the great tip, Lars. To bring what Lars is saying down to the level of the cascade, the rules should be ordered thus:
fieldset {
-webkit-border-radius: 8px;
-moz-border-radius: 8px;
border-radius: 8px;
}
which makes the W3C rule for CSS3 fall last in the cascade.
this css display only mozilla not an IE7
It displays as a normal square cornered box, which is still usable and functional, right?
Thanks very usefull trick. It looks that broder-radius doesn’t work for fieldset, but works for legend. I already reported problem on broken fieldset border radius support.
Winka, It does work for fieldset in some browsers.
thanks a lot sir, things like this are not found every where and one thing i would like to add here that code should be downloadable that becomes easier
hi,
Is it possible to change the rectangle border height and width, which appears when we use legend.It occupies full page , but i Just want it to be around my text box
Just add a width property to your rule.
Thanks it works for me and you saved my a lot of time…