为什么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回答

小于飞飞

2018-03-15

注意 top 是定位到哪了, margin-top 上边距多少。

1. margin-top:-178px;   top:50%;

定位到一半的位置,然后因为margin-top设置,向上移动178px.

2. margin-top:50%;   top:-178px;

先定位到-178px位置 , 然后在这个位置上,在移动50%的位置(margin-top:50%; 是针对父元素的宽度计算)。

希望对你有帮助,欢迎采纳,祝学习愉快。

0
hD_35
h 非常感谢!
h018-03-15
共1条回复

小于飞飞

2018-03-15

代码问题:

http://img.mukewang.com/climg/5aa9c8d00001435702360170.jpg

应该是:

http://img.mukewang.com/climg/5aa9c90f0001ebe503050164.jpg

希望解答你的疑惑,欢迎采纳,祝学习愉快。

0
hD_35
h 谢谢,我知道该这样写,可是为什么那样写没效呢,理解起来不是一样的么,margin-top:60%:距上边距是50% 然后再top:-178px,向上移动相应的距离,这样理解不对么
h018-03-15
共1条回复

0 学习 · 36712 问题

查看课程