Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to position the legend inside a fieldset with a border? [duplicate]

According to the several references on the web, it is not possible to position a legend. So it is suggested to wrap it with span:

<legend><span>Foo</span></legend> 

Then we can position the span inside the fieldset. but then when I want to add a border on top of the fieldset, there is a gap for the legend. Luckily, I've found that adding border to the legend also fixes this little, tiny gap but that's ugly solution (as everything else with css). Do you have any more valid solutions to this problem?

note: I've found the solution concurrently, after I've started writing this question, so I want to still ask it.

like image 834
nocss guy Avatar asked Feb 01 '11 09:02

nocss guy


People also ask

How do you center align a legend?

We can use CSS to align <legend> tag element to center. The CSS margin-left property will play the vital role here to center the legend element.

Can a Fieldset have multiple legends?

Every fieldset element must contain exactly one legend element. Multiple legend elements contained in the same fieldset may result in the improper calculation of labels for assistive technologies.

What is the difference between Fieldset and legend?

The fieldset element provides a container for related form controls and the legend element acts as a heading to identify the group. All of the related fields go inside the fieldset element and the legend element is used to represent the overarching label for that particular group.


2 Answers

I found that simple float:left for LEGEND will do the job.

Codepen sample: http://codepen.io/vkjgr/pen/oFdBa

like image 76
Veiko Jääger Avatar answered Sep 29 '22 18:09

Veiko Jääger


I know this is an old question, but I got this page when I googled "fieldset legend position", and I really couldn't find a good answer.

The legend just won't behave!, so the best solution I found was to position it absolute and have a padding top on the fieldset. JSFildle example: http://jsfiddle.net/carlosmartinezt/gtNnT/356/

fieldset {     position:relative;     padding-top:50px; }  legend {     position:absolute;     top:20px;     left:18px; }​ 
like image 31
Carlos Martinez T Avatar answered Sep 29 '22 20:09

Carlos Martinez T