Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a legend fill up the full width within the fieldset

I would like to have a background for a legend field within a fieldset. And I want it to take up the full width, but only within the fieldset. If I use legend {width: 100%} it will be wider than the fieldset.

Here is an example, runnable in JSFiddle:

<html>
<head>
<style>
fieldset {
    border:0;
    outline: 1px solid gray;
}

legend {
    font-weight:bold;
    background: orange;
    width: 100%
}
</style>
</head>
<body>
<fieldset>
    <legend>Legend</legend>
    Content of Fieldset
</fieldset>
</body>
</html>

Is there any way I can make the legend only fill up the width within the fieldset?

like image 743
Jonas Avatar asked Jun 10 '11 22:06

Jonas


2 Answers

See: http://jsfiddle.net/TAvRF/1/

You can use box-sizing: border-box:

legend {
    font-weight:bold;
    background: orange;
    width: 100%;

    -moz-box-sizing: border-box;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
}

Then you can add padding: http://jsfiddle.net/TAvRF/5/

Although, just setting padding: 0 and forgetting about box-sizing: border-box seems to work for me.. http://jsfiddle.net/TAvRF/6/

like image 190
thirtydot Avatar answered Nov 13 '22 01:11

thirtydot


It sounds like a simple padding problem, I think you can easily solve it using a reset or setting the padding in the legend:

legend {
    font-weight:bold;
    background: orange;
    width: 100%;
    margin: 0;       /* added just in case... */
    padding: 0;
}
like image 40
jeroen Avatar answered Nov 13 '22 00:11

jeroen