【发布时间】:2020-07-02 08:46:09
【问题描述】:
问题
出于可访问性原因,我有一个包含多个字段集和一个图例标签的表单。我希望图例标签位于输入字段下方。由于我将 CSS Grid 用于字段集,因此(在 firefox 上)使其正常工作的唯一方法是为图例标签提供绝对定位。
在其他浏览器中,例如 Chrome 和 Edge,这似乎无法正确呈现。我究竟做错了什么?为什么我似乎无法使用 CSS Grid 来定位图例标签?
fieldset {
display: grid;
grid-template-columns: minmax(max-content, 12vw) auto;
grid-template-rows: auto auto;
position: relative;
padding-bottom: 2em;
}
fieldset legend {
position: absolute;
margin: 0;
padding: 0;
/**
* Suggested by user Kamel
* AFAIK legend is styled a block element by default.
* It definitely is for Firefox
*/
display: block;
grid-column: 2 / 3;
grid-row: 2 / 3;
}
<fieldset>
<legend>An accessible description</legend>
<label>Field</label>
<input type="text">
</fieldset>
<fieldset>
<legend>An accessible description</legend>
<label>Field</label>
<input type="text">
</fieldset>
Screenshot in Firefox(这是我想要实现的)
到目前为止我的研究
MDN 说:
<legend>元素的样式可以,但控制它的位置可能有点棘手。默认情况下,它始终位于其<fieldset>父级的顶部边框上,靠近左上角。要将其定位在其他位置,例如在某个字段集中的某个位置,或者靠近左下角,您需要依靠定位。
https://developer.mozilla.org/en-US/docs/Learn/Forms/Styling_web_forms
但它似乎没有在任何地方提到 CSS Grid 说它不起作用。为什么不应该呢?它也适用于所有其他元素。
【问题讨论】: