How do you set a floating div's width to take up remaining space without pushing other divs down?

前端 未结 2 2087
攒了一身酷
攒了一身酷 2021-02-08 00:37

For part of a layout I want to make, I want to use three divs, all floating next to each other. The Left and Right have a max-width set, which works fine, but I want the middle

2条回答
  •  南方客
    南方客 (楼主)
    2021-02-08 01:20

    Classic Floats

    If you order it:

    and you float the left column left, and the right column right, the middle column should fill in the remaining space. You will have some issues with margins, borders and paddings though.


    Flexbox

    If you don't need to support older browsers, you can use flexbox. With flexbox, this sort of structure becomes much simpler, and the markup doesn't need to change.

    You will need to be able to select the parent element, so for the purposes of this demo, the code will be wrapped by

    .

    .wrapper {
      display: flex;
      flex-direction: row;
      height: 200px;
    }
    
    .left {
      background-color: red;
      width: 100px;
    }
    .middle {
      background-color: green;
      flex: 1;
    }
    .right {
      background-color: blue;
      width: 100px;
    }

    The height and widths are added explicitly so that the

    s are visible. With actual content, the columns would automatically adjust.

提交回复
热议问题