How to make a div fill a remaining horizontal space?

前端 未结 25 1420
野的像风
野的像风 2020-11-22 00:45

I have 2 divs: one in the left side and one in the right side of my page. The one in the left side has fixed width and I want the one of the right side to f

25条回答
  •  [愿得一人]
    2020-11-22 01:25

    Since this is a rather popular question, I'm inclined to share a nice solution using BFC.
    Codepen sample of the following here.

    .left {
      float: left;
      width: 100px;
    }
    .right {
      overflow: auto;
    }
    

    In this case, overflow: auto triggers context behavior and makes the right element expand only to the available remaining width and it will naturally expand to full width if .left disappears. A highly useful and clean trick for many UI layouts, but perhaps hard to understand the "why it works" at first.

提交回复
热议问题