css floats and its stack order

前端 未结 3 1180
后悔当初
后悔当初 2021-01-18 16:48

I am reviewing the floats property which i learned before,I found a simple issue about floated elements with its own stacking order, the code as:

Example 1:

3条回答
  •  -上瘾入骨i
    2021-01-18 17:37

    I believe I understand the issue now (somewhat). Because they have the same dimensions, and because float: left kind of acts like display: absolute while maintaining text space, it's pushed box-2's text to the bottom.

    You can get around this setting display: inline-block for box-2 and interestingly enough, putting an overflow: hidden or overflow: auto also fixes it.

    .box {
      width:100px;
      height:100px;
    }
    
    .box-1{
      float:left;
    }
    .box-2{
      background:blue;
      overflow: auto
    }
    box-1
    box-2

提交回复
热议问题