3

I have a single div in a page. Now, when I specify its position as absolute and give its margin-left == 20px:

I understand that. The div element should shift right by 20 px so that thee exists a margin 0f 20px between the div and the body.

Now when I provide margin of 20px from right side, shouldn't the whole div move towards right side with a gap of 20px from the body.

http://jsfiddle.net/2Jfpj/

.container{
position: absolute;
background-color:gray;
margin-right: 50px; 
height: 200px;
}

I know I can position the div giving values of left and right. But the real question is margin left works but margin-right doesnt! WHy is it so? Help appreciated!

4

2 回答 2

8

它不是margin-right你在寻找它只是right

请从我的小提琴更新中查看: JSFIDDLE 对

如果您希望它位于屏幕底部,请添加bottom

试试这个更新的小提琴:JSFIDDLE 底部

Margin-right不起作用,因为它添加margin到 div 区域本身,whileright是一个位置值。

如果您查看盒子模型,它会显示添加边距的位置。所以 Margin 真的被用来为盒子周围的其他物体提供空间。

于 2013-05-09T13:58:40.637 回答
0

margin-left 有效,因为默认情况下该框与左侧对齐:;财产。设置正确:;属性比你可以设置一个右边距。但到目前为止,我只能在绝对定位的元素上设置左边距或右边距。因此,您可以只使用左或右,不需要边距。如果您知道如何同时设置左右边距,请发布如何!

于 2015-01-28T06:29:14.110 回答