图例标签和 Chrome
我到处都找过了,但毫无结果。
我在表单中得到了一个 ,它可以在除 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是 webkit 浏览器中
legend
元素的一个已知问题。图例元素本身没有干净的解决方法,但您可以将边距添加到图例后面的第一个元素。此外,您还必须在该元素上显式设置
-webkit-margin-collapse:separate
才能使其正常工作。尝试使用这个:http://jsfiddle.net/JLsPs/1/
(答案在这里找到: 无法将 `margin` 添加到 `
)
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:http://jsfiddle.net/JLsPs/1/
(answer found here: Cannot add `margin` to `<legend>` element in Safari & Chrome (WebKit))
我多次与这个问题作斗争,最终导致我放弃图例标签,直到最近我才开始再次使用它来为我的标记添加更多语义。
这是我设计的一个修复程序,用于控制图例标签与其同级标签相关的布局的外观:
标记:
样式:
从我上面提供的示例中,为了实现
如果您没有明确设置图例的高度,您可以在 Firebug 或 Chrome Developer 工具的“公制”选项卡中查看它,因为字体大小会影响它的高度。
但是,是的,非常简单的解决方案,几天前我在处理客户项目时再次遇到了它。然后遇到这个问题,因为我今天试图对此进行更多研究。
编辑:我在发布这个答案后意识到,在我最初的修复中,我将填充应用于父
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:
Styles:
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
Stephan Muller 提出的方法仅在 后面的 HTML 元素可见时才有效。就我而言,如果没有对 HTML 代码进行潜在的大规模重组,这并不总是可能的。因此,除了他的 CSS 代码之外
,只需应用以下 jQuery 命令,该命令基本上只是插入一个空 div(高度为 0 px),但现在匹配在每种情况下添加边距的 CSS 选择器:
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
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:
如果无法更新模板,您可以使用此脚本,只需将图例标签包装在 div 标签内
希望这会有所帮助!享受编码..
If updating the templates is not possible, you can use this script, just wrap the legend tag inside a div tag
Hope this helps! Enjoy coding..