简体   繁体   English

右浮动范围似乎不在父div中

[英]right floated span does not appear to be in parent div

I'm trying to float a span right in my message bar. 我正在尝试在消息栏中浮动范围。 For some reason on in my webpage the right floated span appears below the div visually. 由于某些原因,在我的网页中,右浮动范围在视觉上显示在div下面。 In JS fiddle it looks ok. 用JS提琴看起来还可以。 What could be pushing it down? 有什么可能会推低它?

Example: http://jsfiddle.net/g6wWB/ 示例: http//jsfiddle.net/g6wWB/

Html: HTML:

<div class="msg">
    Some thext here
    <span class="goright">Close</span>
</div>

Css: CSS:

.msg {
    background-color: green;
}

.goright {
    float:right;
}

I believe this is an issue only in IE7 and below where the content itself is pushing the floated element down. 我相信这仅在IE7及以下版本中是一个问题,在该版本中,内容本身会将浮动元素向下推。 In modern browsers it should not be pushed down. 在现代浏览器中,不应将其下推。 But to fix the issue, putting the floated span before the content should work. 但是要解决此问题,请将浮动范围放在内容应该起作用的前面。

<div class="msg">
    <span class="goright">Close</span>
    Some thext here
</div>

Another possible solution, if you want full control over the placement of the span is to use absolute positioning. 如果要完全控制span的位置,另一种可能的解决方案是使用绝对定位。

.msg {
    background-color: green;
    position: relative;
}

.goright {
    position: absolute;
    right: 0;
    top: 0;
}​

This way, it won't be pushed down, no matter how much content, or the order of the elements inside the div. 这样,无论有多少内容或div中元素的顺序,它都不会被下推。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM