为什么margin-top:50%; top:-178px;这样写没用,原理不是一样的么
来源:4-3 编程练习
KD_35
2018-03-14 20:10:06
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>00</title>
<style type="text/css">
*{margin: 0;padding:0;}
.bgimg{
background: url(http://climg.mukewang.com/59c9f7ce0001839219034033.png) center top no-repeat ;
width: 100%;
height: 4043px;
}
.leftbanner{
height:364px;
position: fixed;
left: 0;
top: 50%;
margin-top:-178px;
}
.rightbanner{
height: 364px;
position:fixed;
right: 0;
/*top:50%;
margin-top:-178px;*/
margin-top:50%;
top:-178px;
}
</style>
</head>
<body>
<div class="bgimg">
<div class="leftbanner"><img src="http://climg.mukewang.com/5a3383c70001f1b702240364.png"/></div>
<div class="rightbanner"><img src="http://climg.mukewang.com/5a3383d00001a3dd02240364.png"/></div>
</div>
</body>
</html>
2回答
注意 top 是定位到哪了, margin-top 上边距多少。
1. margin-top:-178px; top:50%;
定位到一半的位置,然后因为margin-top设置,向上移动178px.
2. margin-top:50%; top:-178px;
先定位到-178px位置 , 然后在这个位置上,在移动50%的位置(margin-top:50%; 是针对父元素的宽度计算)。
希望对你有帮助,欢迎采纳,祝学习愉快。
小于飞飞
2018-03-15
代码问题:
应该是:
希望解答你的疑惑,欢迎采纳,祝学习愉快。
相似问题
回答 5