How make middle div to fill space between floating elements?

前端 未结 2 685
温柔的废话
温柔的废话 2020-12-23 20:58

I have three div elements: left, middle and right. Left and right are fixed and floating. What I want is the middle div to fill the gap in between them.

相关标签:
2条回答
  • 2020-12-23 21:47

    The key is to restructure your html to have middle last, remove the float from the middle and replace it with overflow: hidden.

    View fiddle example.

    HTML

    <div id="left"  >  left   </div>
    <div id="right" >  right  </div>
    <div id="middle">  middle </div>
    

    CSS

    #left {
        width: 200px;
        float: left;
    }
    #middle {
        overflow: hidden;
    }
    #right {
        width: 200px;
        float: right;
    }
    
    0 讨论(0)
  • 2020-12-23 21:48

    I was able to replicate the issue and fix it using percentages instead of absolute values. Since you are looking for something flexible between the two elements this should work.

    #left {
        width: 20%;
        float: left;
        background: #ccc;
    }
    #middle {
        width: 60%;
        float: left;
        display: block;
        background: #ffffd;
    }
    #right {
        width: 20%;
        float: right;
        background: #bbb;
    } 
    

    Here's a demo

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