简体   繁体   English

为什么我的div不会浮动到右侧

[英]Why my div wont float to the right edge

i have two div like this 我有两个这样的股利

<div id="pos-bar">
  <div id="pointer"></div>
</div>

and the css is like this CSS就是这样

#pos-bar {
 height: 10px;
 width: 960px;
 margin: 0 auto; }

#pointer{
 height: 4px;
 width: 180px;
 float: right;
 background-color: #57c5a0; }

i try to make "pointer" div to float to the right edge of "pos-bar" div, but with this code it only floated to the middle of "pos-bar" div. 我尝试使“指针” div浮动到“ pos-bar” div的右侧,但是使用此代码,它仅浮动到“ pos-bar” div的中间。 And whenever i try to add margin left to "pointer", the div go down, not in the "pos-bar div anymore. Why is this happening? 每当我尝试增加留给“指针”的边距时,div就会下降,而不是在“ pos-bar div”中。为什么会这样?

I fyou want #pointer to float all the way right, you need to expand the parent's width to the total width of window. 我想让#pointer完全浮动,您需要将父对象的宽度扩展到窗口的总宽度。

For this, you can remove width:960px; 为此,您可以删除width:960px; on #post-bar it will then expand by default to the total width of it's container and it's child ( #pointer ) will be able to float all the way right. #post-bar ,默认情况下它将扩展到其容器的总宽度,并且它的子级( #pointer )可以一直浮动到正确的位置。

See this FIDDLE 看到这个FIDDLE

Just add a float:right to pos-bar div. 只需在pos-bar div中添加一个float:right即可。 Child get out the parents if child and parent not share some important css characteristics. 如果孩子和父母不具有某些重要的css特征,则孩子要离开父母。 Like absolute/relative positions. 像绝对/相对位置。

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

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