【问题标题】:Centering legend in Firefox在 Firefox 中居中图例
【发布时间】:2015-01-23 08:10:12
【问题描述】:

问题

问题是以下在 Chrome、Opera 和 IE 中有效 (!?) 但在 Firefox 中无效:

fieldset>legend {
  display: table;
  float: none;
  margin: 0 auto;
}
<fieldset>
  <legend>Legend</legend>
</fieldset>

Chrome 中的结果

Firefox 中的结果

解决方案黑客

有几个问题,但没有一个令人满意的答案:

/* intended styling */
fieldset>legend {
    display: table;
    float: none;
    margin: 0 auto;
}

fieldset.absolute-fix {
    position: relative;
}
fieldset.absolute-fix>legend {
    position: absolute;
    left: 50%;
}

fieldset.margin-fix>legend {
    margin-left: 50%;
    margin-right: 50%;
    width: auto;
    transform: translate(-50%, 0)
}

fieldset.width-fix>legend {
    width: 100%;
    text-align: center;
}
<fieldset class="absolute-fix">
    <legend>Fix with absolute</legend>
    <p>Not centered and inconsitant styling</p>
    <a href="http://stackoverflow.com/a/4006871/1185053">Source</a>
</fieldset>

<fieldset class="attribute-fix">
    <legend align="center">Fix with attribute</legend>
    <p>Requires use of depreciated HTML and strongly ties presentation to content.</p>
    <a href="http://stackoverflow.com/a/19969606/1185053">Source</a>
</fieldset>

<fieldset class="margin-fix">
    <legend>Fix with margin</legend>
    <p>This is unsatisfying because there is a mis-aligned gap in the border and long legends go over several lines.</p>
</fieldset>

<fieldset class="width-fix">
    <legend>Fix with width</legend>
    <p>This is unsatisfying because there is a gaping hole in the border.</p>
</fieldset>

问题

有没有办法在 Firefox 中不断地将 legend 居中,同时保持其他浏览器的样式?

【问题讨论】:

  • 更简单:legend { text-align: center; } 用于不同于 Firefox 的浏览器。

标签: html css firefox


【解决方案1】:

此解决方案使用 Firefox 特定的选择器,因此我们无需更改其他浏览器的样式。它使用绝对定位,但使用transform 属性适当居中。

/* indended styling for other browsers */
fieldset>legend {
  display: table;
  float: none;
  margin: 0 auto;
}

/* FF only */
@media screen and (-moz-images-in-menus: 0) {
  fieldset {
    position: relative;
  }
  fieldset>legend {
    position: absolute;
    left: 50%;
    top: -12px; /* depends on font size and border */
    background: white; /* depends on background */
    transform: translate(-50%, 0);
  }
}
<fieldset>
  <legend>Fix using absolute and transform</legend>
  <p>This seems to work a bit better.</p>
</fieldset>

Chrome 中的结果

Firefox 中的结果

【讨论】:

  • 更简单:legend { text-align: center; } 用于其他浏览器。顺便说一句,你的 Firefox 解决方案是跨浏览器的,所以你只需要这个。 (你也可以使用translateX(-50%)。)
  • 在 Firefox 中运行此代码 sn-p 并且似乎不再工作了。
猜你喜欢
  • 2016-03-09
  • 2014-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-25
  • 2011-04-06
  • 2013-07-31
  • 1970-01-01
相关资源
最近更新 更多