如何在 IE 7 中正确清除它?
我有一个带有一系列 p 标签的 div 容器。每个 p 标签都会向左浮动。 我想要每行两个 p 标签,因此请考虑字段/值。
标题:一些标题
作者:一些作者
<div id="container">
<p class="field">Title</p><p>Some Title</p>
<p class="field">Author</p><p>Some Author</p>
</div>
如果我将“field”类设置为clear:both,我会在大多数浏览器中获得所需的功能,除了IE 7(不担心<7)。然而,在 IE 7 中,如果包含的 div 足够宽,那么clear: 两者似乎都会被忽略,我会得到类似这样的内容:
Title: Some title Author:
一些作者的
一些想法:
- 我可以监视包含 div 的宽度,以便只有两个 p 标签可以位于一行上,但这看起来非常脆弱。
- 我可以通过在每两个 p 标签后面放置清除 div 来混淆标记。它会起作用,但让我感觉内心很脏。
我怎样才能克服这个问题?
I have a div container with a series of p tags. Each p tag will float to the left. I want two p tags per line, so think field/value.
Title: Some Title
Author: Some Author
<div id="container">
<p class="field">Title</p><p>Some Title</p>
<p class="field">Author</p><p>Some Author</p>
</div>
If I set the "field" class to clear: both, I get the desired functionality in most browsers except IE 7 (not worried < 7). However, in IE 7 if the containing div is wide enough, the clear: both seems to be ignored and I'll get something like this:
Title: Some title Author:
Some Author
A couple of thoughts:
- I can monitor the width of the containing div so that only two p tags can sit on one line but that seems very brittle.
- I can muddy up the markup by placing clearing divs after every two p tags. It would work but makes me feel dirty inside.
How can I overcome this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用此模式(跨度是可选的 - 如果需要,可用于其他样式)。列表比重新使用错误的标签更具有语义意义。这是一个清单。 :)
CSS:
Use this pattern (span is optional - for additional styling if needed). Lists make more semantic sense than re-purposing the wrong tags. This is a list. :)
CSS:
http://jsfiddle.net/QUL9v/1/
使用 p 标签....
与 css :
这只是坚持使用 p 标签。就我个人而言,这就是我实现它的方式(http://jsfiddle.net/QUL9v/3/):
我推荐这样做的唯一原因是因为这更多的是一个布局问题,所以对我来说感觉更自然使用 div 而不是 p 元素。此外,无论您在 div 中放入什么(锚点、表单、表格等),它都会确保文本的位置。
您应该注意的另一件事是我使用清除作为最后一个兄弟而不是第一个(如您的示例中所示)。如果你要清理前面;那么由于您的尾随元素是浮动的和内联的,您可能会在以后遇到错误,尤其是在 IE7 中。很多时候,浮动规则会传递到您从未打算或认为会传递到的元素。最后清除可确保这种情况不会发生。
http://jsfiddle.net/QUL9v/1/
Using the p tags....
with css:
This is just sticking to the use of the p tag. Personally, this is how I would accomplish it (http://jsfiddle.net/QUL9v/3/):
The only reason I'm recommending this is because since this is more of a layout issue, it feels more natural to me to use the div as opposed to p element. Also, it will ensure the position of the text, regardless of what you put inside the divs (anchors, forms, tables, etc).
Another thing you should pay attention to is I'm using the clear as the last sibling instead of the first (as in your examples). If you're clearing the front; then its possible that since your trailing elements are floated and inline, you're going to potentially run into errors down the road, especially with IE7. A lot of the times, the floating rule will get passed on to elements you never intended or thought it would be passed to. Clearing at the end ensures that this doesn't happen.