On the positional relationship between inline block elements and floating elements

Boss, if there is a floating element and an inline block element in the parent box, why does the inline block element avoid the space of the floating element? Instead of occupying its space (the block element will occupy its space)
code is as follows:

<body>
    <div class="box">
        <div class="c1"></div>
        <div class="c2"></div>
    </div>
</body>


.box {
    width: 500px;
    height: 300px;
}

.c1 {
    width: 100px;
    height: 100px;
    background-color: red;
    float: left;
}

.c2{
    width: 200px;
    height: 200px;
    background-color: blue;
    display: inline-block;
}

the effect is as follows:

Oct.16,2021

Let's first take a look at the differences between the different values in the display attribute:

  • block causes the elements to be placed on a new line
  • inline will display elements inline with the current paragraph
  • inline-block you can use width and height to restrict width and height, while placing elements next to the content in front of it

Let's take a look at the float layout description: the
float attribute allows the element to move around so that other elements can surround it.

go back to the landlord's code and take a look:

  1. C1 float:left indicates that the element moves as far to the left as possible, so that it moves to the left boundary. At the same time, div defaults to display:block so that it can maintain the width and height setting.
  2. In c2 , display:inline-block does not start at the beginning of the line, wrapping the C1 around, but placing the element next to the C1 element.
  3. c2 if set to display:block , it starts at the beginning of the line and surrounds C1
  4. .

therefore, occupies the space of C1 , which is not appropriate, and should be said to surround C1 .

Menu