如何防止浮动 div 继承其兄弟的 margin-top ?
我有一些类似于此示例的 html 代码:
<html>
<body>
<div id="A" style="float:right; Background-color: Red;">Alpha</div>
<div id="B" style="margin-top: 20px; Background-color: Green;">Bravo</div>
</body>
</html>
我想要实现的是将 A div 放在右上角,将 B div 与页面顶部左侧 20 像素对齐。如下所示,它在 IE8 中完全符合我的要求,但在 Chrome 和 Firefox 中却不太有效。
因为这种情况很常见,我认为 Chrome 和 FF 正确地以这种方式呈现它,而 IE8 则不然遵循标准。所以我的问题是如何在所有浏览器中获得所需的结果(IE 在 SS 中的作用)?
编辑1&2:我编辑了屏幕截图以表明我确实希望 Bravo div 位于 Alpha div 的左侧但低 20px,而不是像我简单地添加一个清晰的那样在它下面:就在布拉沃。
谢谢
I have some html code that look like this sample here :
<html>
<body>
<div id="A" style="float:right; Background-color: Red;">Alpha</div>
<div id="B" style="margin-top: 20px; Background-color: Green;">Bravo</div>
</body>
</html>
What I'd like to achieve is have the A div in the upper right corner and the B div aligned on the left 20 pixels from the top of the page. As you can see below, it does exactly what I want in IE8, but it doesn't quite works in Chrome and Firefox.
Because it's often the case, I suppose Chrome and FF are right to render it this way and IE8 is not following the standard. So my question is how can I get the desired result (What IE does in the SS) across all browser?
EDIT1&2: I edited the screenshot to show that I really want the Bravo div to be on the left of the Alpha div but 20px lower and not under it like it would if I simply add a clear: right on Bravo.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
编辑
回答新问题:
原始
(问题作者最初问这个,后来改了问题...)
在 B div 的样式中添加
clear: right;
。可以选择使用clear: Both;
。将 div A 包装到包装 div 中,如下所示:EDIT
Answer to the new question:
ORIGINAL
(The question author originally asked this and later changed the question...)
Add
clear: right;
into B div's style. Optionally useclear: both;
. And wrap div A into a wrapper div like this:试试这个:
添加:
float:right; clear:right;
和width:100%;
到#B
查看实际效果 - http://jsfiddle.net/SHubq/
Try this:
Added:
float:right; clear:right;
andwidth:100%;
to#B
See it in action - http://jsfiddle.net/SHubq/
将
clear
属性添加到 b 的 div 样式中,使用最适合相关网站的属性。例如
both
,这样两边都什么也得不到。Add a
clear
property onto b's div style, use the property that is most appropriate to the site in question.Such as
both
so you get nothing either side.您必须添加 clear CSS 规则。
在两个
div
之间添加clear: right
即可)或
div id="B"
样式中添加clear: Both
。 (注意:在您的情况下,只需clear: right
即可)编辑:查看此小提琴中的两个案例 http://jsfiddle.net/dyGyu/
You have to add the clear CSS rule.
add between your two
divs
<div style="clear: both" />
. (Note : in your case, justclear: right
works)or
clear: both
in thediv id="B"
style. (Note : in your case, justclear: right
works)Edit : See the two cases in this fiddle http://jsfiddle.net/dyGyu/