Skip to content

Style a fieldset with rounded corners using CSS

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.

An unstyled fieldset

I applied these rules to the fieldset selector in the CSS

fieldset {
border-radius: 8px;
-webkit-border-radius: 8px;
-moz-border-radius: 8px;
}

Again in Safari, here’s what you see now.

CSS border rules created rounded corners in the fieldset

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;
border-radius: 8px;
-webkit-border-radius: 8px;
-moz-border-radius: 8px;
}

This is how it displays in Safari.

A border with rounded corners is used on the legend

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;
border-radius: 8px;
-webkit-border-radius: 8px;
-moz-border-radius: 8px;
padding: 6px;
}

Now the legend displays like this.

The legend with a background color and some padding

Additional articles at Web Teacher about styling fieldsets with CSS:

AddThis Social Bookmark Button   add to kirtsy

Stumble it!


6 Comments

  1. 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 My ComLuv Profile

    Saturday, February 27, 2010 at 1:32 pm | Permalink
  2. vdebolt wrote:

    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.

    Saturday, February 27, 2010 at 2:44 pm | Permalink
  3. Sudhir wrote:

    this css display only mozilla not an IE7

    Wednesday, April 7, 2010 at 10:17 pm | Permalink
  4. vdebolt wrote:

    It displays as a normal square cornered box, which is still usable and functional, right?

    Thursday, April 8, 2010 at 5:16 am | Permalink
  5. winka wrote:

    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.

    Wednesday, June 23, 2010 at 1:01 pm | Permalink
  6. vdebolt wrote:

    Winka, It does work for fieldset in some browsers.

    Wednesday, June 23, 2010 at 1:36 pm | Permalink

What can you contribute?

Your email is never published nor shared. Required fields are marked *
*
*
CommentLuv Enabled