不,这真的不可能。甚至浏览器制造商自己也在为此苦苦挣扎。
当然,无论如何,我都忍不住要试一试。我在这上面花了很多时间,以至于Anonymous同时想出了一个与我的(他的test1
)非常相似的“解决方案”。但我的不需要固定宽度的“图例”。
不过,这段代码显然有点小技巧,而且我不知道它在复杂网站中的表现如何。我也同意 Anonymous 的观点,即使用字段集进行分组并不像使用表格进行布局那么糟糕。字段集是为对元素进行分组而设计的,尽管在 HTML 中它们实际上只应该用于对表单控件进行分组。
.fieldset {
border: 2px groove threedface;
border-top: none;
padding: 0.5em;
margin: 1em 2px;
}
.fieldset>h1 {
font: 1em normal;
margin: -1em -0.5em 0;
}
.fieldset>h1>span {
float: left;
}
.fieldset>h1:before {
border-top: 2px groove threedface;
content: ' ';
float: left;
margin: 0.5em 2px 0 -1px;
width: 0.75em;
}
.fieldset>h1:after {
border-top: 2px groove threedface;
content: ' ';
display: block;
height: 1.5em;
left: 2px;
margin: 0 1px 0 0;
overflow: hidden;
position: relative;
top: 0.5em;
}
<fieldset>
<legend>Legend</legend> Fieldset
</fieldset>
<div class="fieldset">
<h1><span>Legend</span></h1> Fieldset
</div>
作为旁注,您可能还想看看 HTML5figure
和figcaption
元素。这些似乎是您的示例中使用的最佳元素。
不过,这仅适用于问题的语义部分,因为我认为这些元素的呈现方式与字段集/图例不同。更不用说当前的浏览器可能还不支持这些元素。