为什么给head设置定位为fixed后,会覆盖下一个div的内容呢?

为什么给head设置定位为fixed后,会覆盖下一个div的内容呢?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<!DOCTYPE html>
<html>
<head>
    <title>2-3编程练习</title>
    <meta charset="utf-8">
    <style type="text/css">
    *{
        margin: 0;
        padding: 0;
        color: #fff;
        font-family: "微软雅黑";
    }
    a{
        text-decoration:none;
    }
    .head{
        width: 100%;
        height: 100px;
        margin: 0 auto;
        background-color: #000;
        position: fixed;
        top:0;
         
    }
    .logo{
 
        height: 100px;
        float: left;
    }
    .dh{
        float: right;
        height: 100px;
        margin: 0 auto;
        line-height: 100px;
    }
    .content{
        width: 100%;
        height: auto;
    }
    .content img{
        width: 100%;
    }
    .foot{
        width: 100%;
        height: 50px;
        background-color: #000;
        text-align: center;
        line-height: 50px;
        position: fixed;
        left: 0;
        bottom: 0;
    }
 
    </style>
</head>
<body>
    <div class="head">
        <div class="logo"><a href="#"><img src="http://img1.sycdn.imooc.com/climg//58c0d2d900016ce303000100.png"></a></div>
        <div class="dh">
            <a href="#"><span>课程</span></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <a href="#"><span>职业路径</span></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <a href="#"><span>实战</span></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <a href="#"><span>猿问</span></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <a href="#"><span>手记</span></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        </div>
    </div>
    <div class="content"><a href="#"><img src="http://img1.sycdn.imooc.com/climg//58c0eda50001e12416000480.jpg" alt="加载失败"></a></div>
    <div class="content"><a href="#"><img src="http://img1.sycdn.imooc.com/climg//58c0edb80001c9f216000480.jpg" alt="加载失败"></a></div>
    <div class="content"><a href="#"><img src="http://img1.sycdn.imooc.com/climg//58c0edc9000100d516000480.jpg" alt="加载失败"></a></div>
    <div class="foot">
        <a href="#"><span>网站首页</span></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <a href="#"><span>企业合作</span></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <a href="#"><span>人才招聘</span></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <a href="#"><span>联系我们</span></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <a href="#"><span>常见问题</span></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <a href="#"><span>友情链接</span></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    </div>
</body>
</html>


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

4回答
麦麦zzz 2017-05-08 08:27:39

head设置fixed后脱离文档流,下面的div不知道head存在,于是向上移动了head的高度,然后head就遮住了一部分;所以需要将下面的div设置一个margin-top值为head高度,把head的位置留出来。

可以这样说head只知道自己固定了脱离了文档流,div不知道head在哪,但是你知道,你需要把他们的位置都安排好,大概我是这样理解的

  • margin-top我设置了以后为什么head的div位置也发生了变化?
    2017-05-10 13:33:37
  • 我把你的.content选择器中加了margin-top:100px;之后试了试,head中的div没有变,你是不是加错地方了
    2017-05-11 11:11:58
只若初见_0001 2017-05-05 14:34:05

添加一个special类的样式

.special{padding-top:100px}

然后给第一个类为content的div再加一个类 <div class="content special">

你试一下看看

提问者 vickyas 2017-05-01 20:06:22

好的。但是像实现2-6编程练习那样,页头和页脚都是固定的,可它的第一张图片也没有被覆盖掉,这该怎么解决?谢谢老师

卡布琦诺 2017-05-01 16:58:53

position: fixed;是固定定位,也叫坐标定位,意思是不随页面滚动,永远显示在页面的固定位置,因此会覆盖住页面的其他内容,建议亲这里可以将position: fixed;替换成position:absolute;祝学习愉快~

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码