Explanation on CSS float overflow property

前端 未结 2 1603
陌清茗
陌清茗 2021-01-21 16:49

I am a newbie to CSS. I started exploring CSS float property. I have two

elements inside a container. The two divs have equal
相关标签:
2条回答
  • 2021-01-21 17:15

    What is happening here is because:

    .container{
      overflow:hidden;
    }
    

    What is happening? Your floating div wants to take up space before your other div. Because your non-floating div has display: block, it wants a whole line to render itself. The floating div is pushing down your non-floating div which is then hidden by the style above. If you change display to inline-block, it will take up only the space it needs using the width and height properties.

    Here is an example: http://codepen.io/anon/pen/LVpxZy

    0 讨论(0)
  • 2021-01-21 17:38

    The float CSS property specifies that an element should be taken from the normal flow. However the non-float element still takes all the available width including the floating element. But if you set overflow: auto; or hidden to it, it will then align next to the floating element. See the demo following, it should explain it clearly.

    .a1, .b1 {
        background: rgba(0,0,0,.5); /*pure black with alpha*/
        width: 50px;
        height: 50px;
        float: left;
    }
    .a2 {
        background: lime;
    }
    .b2 {
        background: lime;
        overflow: auto; /*let the browser decide whether to clip or not*/
    }
    <div class="a1">hello</div>
    <div class="a2">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu.</div>
    
    <br/>
    
    <div class="b1">hello</div>
    <div class="b2">Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu.</div>

    0 讨论(0)
提交回复
热议问题