简体   繁体   English

字段集的默认 CSS 值<legend>

[英]Default CSS values for a fieldset <legend>

I'm trying to use a <legend> as a title inside a <fieldset> .我正在尝试使用<legend>作为<fieldset>内的标题。

In browsers other than IE, the <legend> is positioned on the top border of the <fieldset> , with the text perfectly centered on the line.在 IE 以外的浏览器中, <legend>位于<fieldset>的上边框,文本完全居中。

替代文字

I'm trying to reset it's position so that it sits just like any other element.我正在尝试重置它的位置,使其与任何其他元素一样。 ie an <h3> .即一个<h3>

Here's the CSS I have so far.这是我到目前为止的CSS。

fieldset legend {
    margin: 0;
    padding: 0;
    position: static;
    border: 0;
    top: auto; left: auto;
    float: none;
    display: block;
    font-size: 14px;
    line-height: 18px;
}

But the legend is still perfectly centered on the line.传说仍然完美地集中在这条线上。

Yes, I can add a margin/padding/top coordinate but I want to know if the browser has any default values for the element that trigger this layout.是的,我可以添加边距/填充/顶部坐标,但我想知道浏览器是否有触发此布局的元素的任何默认值。 I want to then, override these values.然后我想覆盖这些值。

Tested in Firefox (3.6.10), Chrome (6.0.472.63), Safari (5.0.2)在 Firefox (3.6.10)、Chrome (6.0.472.63)、Safari (5.0.2) 中测试

Update I'll leave this question open for another week just in case someone HAS been able to style <legend> elements.更新我将把这个问题留一个星期,以防有人能够设置<legend>元素的样式。 If no solutions are found I'll accept @jnpcl's answer.如果没有找到解决方案,我会接受@jnpcl 的回答。

This is enough :这就够了 :

 form legend{
    float: left;
    width: 100%;
 }

https://web.archive.org/web/20140209061351/http://tjkdesign.com/articles/how_to_position_the_legend_element.asp https://web.archive.org/web/20140209061351/http://tjkdesign.com/articles/how_to_position_the_legend_element.asp

Simply put, it is not possible across browsers to position the LEGEND element in a Fieldset.简单地说,跨浏览器不可能将 LEGEND 元素定位在 Fieldset 中。

Workaround: wrap the text from <legend> in a <span> , then reposition the <span> .解决方法:将<legend>中的文本包装在<span>中,然后重新定位<span>

I've just styled my <legend> 's by giving them a position: absolute; top: -25px;我刚刚通过给他们一个位置来设置我的<legend>的样式position: absolute; top: -25px; position: absolute; top: -25px; and the the parent <fieldset> with a position: relative; padding-top: 30px;和父<fieldset>position: relative; padding-top: 30px; position: relative; padding-top: 30px;

According to the specification, here is the default styling of the fieldset and legend elements.根据规范, 这里是fieldsetlegend元素的默认样式。 By resetting those properties, you can have a clean legend element to work with.通过重置这些属性,您可以使用干净的legend元素。

As per HTML - Living Standard , the below styles are working like a default:根据HTML - Living Standard ,以下样式就像默认样式一样工作:

fieldset {
    display: block;
    margin-inline-start: 2px;
    margin-inline-end: 2px;
    border: groove 2px ThreeDFace;
    padding-block-start: 0.35em;
    padding-inline-end: 0.75em;
    padding-block-end: 0.625em;
    padding-inline-start: 0.75em;
    min-inline-size: min-content;
}

legend {
    padding-inline-start: 2px; padding-inline-end: 2px;
}

According to the specification , the legend is only a "rendered legend" if it is float: none .根据规范,如果它是float: none ,则图例只是一个“渲染图例”。

This means that by doing:这意味着通过这样做:

<fieldset>
    <legend style='float: left'> Heading </legend>
    <div class='clearfix'></div>
    <!-- Your form elements here -->
</fieldset>

This makes the legend behave like a normal (if floated) element.这使得图例表现得像一个普通(如果浮动)元素。

Note: clearfix is the Bootstrap clearfix class:注意: clearfix是 Bootstrap clearfix 类:

.clearfix::after {
    clear: both;
}
.clearfix::before, .clearfix::after {
    display: table;
    content: " ";
}

(A similar answer was posted already, but this does not include the clearfix trick, and the reference to the specification which shows that this is not a random but, but specified behaviour that is reliable.) (已经发布了类似的答案,但这不包括 clearfix 技巧,以及对规范的引用,这表明这不是随机的,而是指定的可靠行为。)

This is a very old question, but still high in Google, so I'd like to share a solution that works for me (targeting only more modern browsers for the best experience).这是一个非常古老的问题,但在 Google 中仍然很高,所以我想分享一个适合我的解决方案(仅针对更现代的浏览器以获得最佳体验)。

fieldset: {all:unset};
legend:{all:unset};

this does the trick for me, unsetting all values to defaults.这对我有用,将所有值取消设置为默认值。 From there on I can happily style on a "clean-sheet".从那里开始,我可以愉快地在“干净的床单”上设计风格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM