南城& 2022-08-31 13:21 采纳率: 70.6%
浏览 59
已结题

想要让b距离顶部,为什么a会跟着一起

三个盒子a,b,c;b在a里面,我调了b的margin-top为300px,想要让b距离顶部300,但是为什么a会跟着一起动?
代码
  <style>
        *{
            padding: 0;
            margin: 0;
        }
        .c{
            position: fixed;
            width: 100%;
            height: 80px;
            top: 0px;
            background-color: green;
        }
        .a{
           
            width: 700px;
            height: 3000px;
            background-color: pink;
            margin: 0 auto;
        }
        .b{
            width: 200px;
            height: 200px;
            background-color: yellow;
            margin-top: 200px;
        }
       
    </style>
</head>
<body>
     <div class="c"></div>
     <div class="a">
        <div class="b"></div>

     </div>
     
</body>

运行结果及报错内容

img

  • 写回答

3条回答 默认 最新

  • fruge365 优质创作者: 前端开发技术领域 2022-09-01 16:26
    关注

    触发bfc了,b在a里面,所以margin-top向上传递了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 9月11日
  • 已采纳回答 9月3日
  • 创建了问题 8月31日