Css document flow, parent-son container package problem?

//css
.box2{
    width: 100%;
    height: 100px;
    background-color: black;
    position: fixed;
    top: 0;
    left: 0;
}
.box3{
    width: 100%;
    height: 100px;
    background-color: red;
    margin-top: 200px;
}

//html
<div class="box1">
    <div class="box2"></div>
    <div class="box3"></div>
</div>

according to the above code, you can see that box2 is not wrapped in box1 because of position:fixed :

clipboard.png

box1padding: 0.1px(>0.1px)box2box1:

clipboard.png

would like to ask why this is? Is there any way not to add padding to box1 and let box2 be wrapped in box1?? If not, how can box2 achieve the effect of fixing its head without using position:fixed ?

Sep.28,2021

actually you are mistaken. box2 has not been wrapped into box1 . What you see is that when box1 does not add padding is box3 margin-top: 200px will exceed from box1 and will not be counted in box1 , while box1 plus padding will make margin-top: 200px of box3 be counted into box1 . You can change the value of margin-top to find the problem


< H1 > reason < / H1 >

actually, box2 has never been wrapped in box1 .

The reason for this effect is that when box1 does not have padding-top , the margin-top of box3 will fold with the top margin of box1 and body . In this case, only the content area of box3 is counted in the content area of box1 .

box1 with padding-top (or border-top ), the top margin of box3 is also calculated in the content area of box1 .

< H1 > methods that do not use fixed < / H1 >

here is a method for reference


            twtw wereeetwtw wereeetwtw wereeetwtw wereeetwtw wereeetwtw wereeetwtw wereeetwtw wereeetwtw wereeetwtw wereeetwtw wereeetwtw wereeetwtw wereeetwtw wereeetwtw wereeetwtw wereeetwtw wereeetwtw wereeetwtw wereeetwtw wereeetwtw wereeetwtw wereeetwtw wereeetwtw wereeetwtw wereeetwtw wereeetwtw wereeetwtw wereeetwtw wereeetwtw wereeetwtw wereeetwtw wereeetwtw wereeetwtw wereeetwtw wereeetwtw wereeetwtw wereeetwtw wereeetwtw wereeetwtw wereeetwtw wereeetwtw wereee
        </div>
    </div>
</body>
</html>
Menu