How to position the legend inside a fieldset with

2019-03-14 08:05发布

This question already has an answer here:

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.

10条回答
劳资没心,怎么记你
2楼-- · 2019-03-14 08:50

This seems more straight forward to me.

fieldset legend { 
    position:relative;
    left:20px;
}
查看更多
Fickle 薄情
3楼-- · 2019-03-14 08:54

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

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

查看更多
仙女界的扛把子
4楼-- · 2019-03-14 08:54

Found this the most consistent, tested in IE7, Firefox and Chrome

fieldset legend { text-align:left; }

查看更多
Fickle 薄情
5楼-- · 2019-03-14 09:07

Set margins for positioning. It works without problems.

legend {margin-left:50px;} /* 50px from Left of the Fieldset */
查看更多
登录 后发表回答