4-3这样写可以吗?为什么定位的时候不可以用position:absolute

来源:4-3 编程练习

weixin_慕田峪7033080

2020-04-15 17:13:10

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

<title>document</title>

<style type="text/css">

.nav{

    width:100%;

    height:3000px;

    background:url("http://climg.mukewang.com/59c9f7ce0001839219034033.png") center no-repeat ;

    background-size:100% 100%;

  

  

}

.qianduan{

    width:220px;

    height:360px;

    background:url("http://climg.mukewang.com/5a3383d00001a3dd02240364.png");

    top:50%;

    left:0;

    margin-top:-180px;

    position:fixed;

}

.houduan{

    width:220px;

    height:360px;

    background:url("http://climg.mukewang.com/5a3383c70001f1b702240364.png");

    top:50%;

    right:0;

    margin-top:-180px;

    position:fixed;

}

    

</style>

</head>

<body>

    <div class="nav">

        <div class="qianduan"></div>

        <div class="houduan"></div>

    </div>


</body>

</html>


写回答

1回答

好帮手慕夭夭

2020-04-15

同学你好,这样做可以,代码实现正确。另外,  position: absolute;是绝对定位,它相对已定位祖先元素定位。如果没有祖先元素,默认相对页面定位。也就是说页面滚动,绝对定位元素也是会滚动的。这里要求的效果是左右的内容固定在窗口两侧,所以需要使用固定定位。

如果我的回答帮助到了你,欢迎采纳,祝学习愉快~

0

0 学习 · 40143 问题

查看课程