图例标签和 Chrome

发布于 2024-10-23 13:07:28 字数 1248 浏览 6 评论 0原文

我到处都找过了,但毫无结果。

我在表单中得到了一个 ,它可以在除 Chrome 之外的所有浏览器中按我想要的方式显示。就像它位于字段集之外,或者就像它位于下一个元素的顶部。这很烦人。我什至不能给它加边距。

为什么会显示成这样呢?

有解决方法吗?

HTML:

  <fieldset class="col-12-box-bottom add-extras">
    <legend class="plus">Add Promotion Code</legend>
    <ul id="promo-fields">
      <li><input class="field-small" type="text" /></li> 
      <li><button class="but-sec" type="submit">Apply</button></li>
    </ul>
  </fieldset>

CSS:

.add-extras legend{
    width: 260px;
    height: 0px;
    border: 1px solid red;
    display: block;
    margin-top: 10px;
}
.add-extras fieldset{
    position: relative;
}
.add-extras ul{
    padding: 0 0 20px 0 !important;
    overflow: hidden;
}
.add-extras li{
    list-style-type: none;
    float: left;
    margin: 0 18px 0 0;
}
.add-extras li:last-child a{
    color: #afafaf;
    display: block;
    margin: 27px 0px 0 0;
}
fieldset.add-extras{
    margin: 0px 0 23px 0;
}
.add-extras label{
    float: none;
    display: block;
    text-align: left;
    width: 110px;
    font-weight: bold;
    margin: 0 0 5px 0;
}

I've looked everywhere but to no avail.

I got a <legend> in a form, which displays as I want in every browsers, except in Chrome. It's like it sits outside of the fieldset, or it's like it goes on top of the next element. And it's very annoying. I can't even put margins on it.

Why does it display like in that way?

And is there a workaround?

HTML:

  <fieldset class="col-12-box-bottom add-extras">
    <legend class="plus">Add Promotion Code</legend>
    <ul id="promo-fields">
      <li><input class="field-small" type="text" /></li> 
      <li><button class="but-sec" type="submit">Apply</button></li>
    </ul>
  </fieldset>

CSS:

.add-extras legend{
    width: 260px;
    height: 0px;
    border: 1px solid red;
    display: block;
    margin-top: 10px;
}
.add-extras fieldset{
    position: relative;
}
.add-extras ul{
    padding: 0 0 20px 0 !important;
    overflow: hidden;
}
.add-extras li{
    list-style-type: none;
    float: left;
    margin: 0 18px 0 0;
}
.add-extras li:last-child a{
    color: #afafaf;
    display: block;
    margin: 27px 0px 0 0;
}
fieldset.add-extras{
    margin: 0px 0 23px 0;
}
.add-extras label{
    float: none;
    display: block;
    text-align: left;
    width: 110px;
    font-weight: bold;
    margin: 0 0 5px 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

┼── 2024-10-30 13:07:28

这是 webkit 浏览器中 legend 元素的一个已知问题。图例元素本身没有干净的解决方法,但您可以将边距添加到图例后面的第一个元素。

此外,您还必须在该元素上显式设置 -webkit-margin-collapse:separate 才能使其正常工作。尝试使用这个:

legend + * {
  -webkit-margin-top-collapse: separate;
  margin-top: 10px;
}

http://jsfiddle.net/JLsPs/1/

(答案在这里找到: 无法将 `margin` 添加到 `

;` Safari 和 Chrome (WebKit) 中的元素

)

This is a known issue with the legend element in webkit browsers. There are no clean workarounds for the legend element itself, but you could instead add the margin to the first element that follows the legend.

Also, you'll have to explicitly set -webkit-margin-collapse: separate on that element to make it work properly. Try using this:

legend + * {
  -webkit-margin-top-collapse: separate;
  margin-top: 10px;
}

http://jsfiddle.net/JLsPs/1/

(answer found here: Cannot add `margin` to `<legend>` element in Safari & Chrome (WebKit))

游魂 2024-10-30 13:07:28

我多次与这个问题作斗争,最终导致我放弃图例标签,直到最近我才开始再次使用它来为我的标记添加更多语义。

这是我设计的一个修复程序,用于控制图例标签与其同级标签相关的布局的外观:

标记:

<div class="fieldset">
  <fieldset>
    <legend>Form Section</legend>
    <div class="field_row">
      <label for="first_name">First Name</label>
      <input id="first_name" name="first_name" type="text">
    </div>
    <div class="field_row">
      <label for="last_name">Last Name</label>
      <input id="last_name" name="last_name" type="text">
    </div>
  </fieldset>
</div>

样式:

.fieldset {
  padding-top:48px; /*legend height(18px) + top value(15px) + bottom spacing(15px) */
  position:relative;
}
legend {
  height:18px; /* Default Height of non-styled legend element with default font-size of 16px as tested at time of this posting */
  left:15px;
  /*margin:15px 0;*/ /* Margins initially trying to achieve */
  position:absolute;
  top:15px; /* replaces top margin-top:15px; */
}

从我上面提供的示例中,为了实现

上的底部“边距”标签,您只需将顶部填充应用于字段集,该顶部填充等于您想要的顶部和底部边距的量加上图例标签的显式高度。这适当地压低了<传奇>的兄弟姐妹。

如果您没有明确设置图例的高度,您可以在 Firebug 或 Chrome Developer 工具的“公制”选项卡中查看它,因为字体大小会影响它的高度。

但是,是的,非常简单的解决方案,几天前我在处理客户项目时再次遇到了它。然后遇到这个问题,因为我今天试图对此进行更多研究。

编辑:我在发布这个答案后意识到,在我最初的修复中,我将填充应用于父

的因为出于某种原因 Firefox 启动了 top:15px;当填充应用于

时,从顶部填充的底部开始。放置padding-top和position:relative;在父 div 上允许

绝对定位在填充物上方,而不是被填充物向下推。我编辑了上面的代码以反映我的发现。这个解决方案一开始很简单,现在对我来说不太有吸引力,但它确实有效。这是我创建的一个页面,测试了两种定位

的方法。标签: 图例标签定位:http://dl.dropbox.com/u /37971131/css-testing/forms.html

I have struggled with this issue many times, eventually leading to my abandoning the legend tag until recent, where I have begun using it again to add more semantic meaning to my markup.

Here is a fix I have devised to control the appearance of the legend tag's layout in relation to it's siblings:

Markup:

<div class="fieldset">
  <fieldset>
    <legend>Form Section</legend>
    <div class="field_row">
      <label for="first_name">First Name</label>
      <input id="first_name" name="first_name" type="text">
    </div>
    <div class="field_row">
      <label for="last_name">Last Name</label>
      <input id="last_name" name="last_name" type="text">
    </div>
  </fieldset>
</div>

Styles:

.fieldset {
  padding-top:48px; /*legend height(18px) + top value(15px) + bottom spacing(15px) */
  position:relative;
}
legend {
  height:18px; /* Default Height of non-styled legend element with default font-size of 16px as tested at time of this posting */
  left:15px;
  /*margin:15px 0;*/ /* Margins initially trying to achieve */
  position:absolute;
  top:15px; /* replaces top margin-top:15px; */
}

From the example I provided above, in order to achieve the bottom "margin" on the <legend> tag that you desire, you'll just apply a top padding to the fieldset equal to the amount of top and bottom margin you desire plus the explicit height of the legend tag. This pushes down the <legend>'s siblings down appropriately.

If you haven't explicitly set the height of your legend, you can just check it out in the metric tab of either Firebug or Chrome Developer tools, as the font-size will affect the height of it.

But yeah, pretty simple solution, I just ran into it again a few days ago when working on a client project. Then came across this question, as I was trying to do more research on it today.

Edit: I realized after posting this answer that in my original fix, I applied the padding to a parent <div> of the <fieldset> because for some reason Firefox starts the top:15px; from the bottom of the top padding, when the padding is applied to the <fieldset>. Putting the padding-top and position:relative; on the parent div allowed the <legend> to position absolutely over the padding instead of being pushed down by the padding. I have edited the code above to reflect my findings. This solution which started out simple, is less attractive to me now, but it definitely works. Here is a page that I created, testing two methods of positioning the <legend> tag: Legend tag positioning: http://dl.dropbox.com/u/37971131/css-testing/forms.html

暗藏城府 2024-10-30 13:07:28

Stephan Muller 提出的方法仅在 后面的 HTML 元素可见时才有效。就我而言,如果没有对 HTML 代码进行潜在的大规模重组,这并不总是可能的。因此,除了他的 CSS 代码之外

legend + * {
  -webkit-margin-top-collapse: separate;
  margin-top: 10px;
}

,只需应用以下 jQuery 命令,该命令基本上只是插入一个空 div(高度为 0 px),但现在匹配在每种情况下添加边距的 CSS 选择器:

$('legend + *').not(':visible').each(function() {
  $('<div></div>').insertBefore($(this)); 
}

The method proposed by Stephan Muller only works if the HTML element following the is visible. As in my case, this is not always possible without potentially large restructuring of the HTML code. Thus, in addition to his CSS code

legend + * {
  -webkit-margin-top-collapse: separate;
  margin-top: 10px;
}

just apply the following jQuery command, which basically just inserts an empty div (having a height of 0 px) but now matches the CSS selector adding the margin in every case:

$('legend + *').not(':visible').each(function() {
  $('<div></div>').insertBefore($(this)); 
}
我也只是我 2024-10-30 13:07:28

如果无法更新模板,您可以使用此脚本,只需将图例标签包装在 div 标签内

jQuery('legend').each(function() {
 jQuery(this).wrap( "<div></div>" ); 
});

希望这会有所帮助!享受编码..

If updating the templates is not possible, you can use this script, just wrap the legend tag inside a div tag

jQuery('legend').each(function() {
 jQuery(this).wrap( "<div></div>" ); 
});

Hope this helps! Enjoy coding..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文