Set the width of children to fill the parent

后端 未结 7 1078
日久生厌
日久生厌 2020-12-19 11:46

I want the children of the div fill its width.

now am using a code like this:

7条回答
  •  天涯浪人
    2020-12-19 12:15

    You can use flexbox to achieve this.

    The demo below shows how it works with more child nodes and also with nodes with zero height.

    I have also changed the margin property for the child items to work properly with flexbox.

    .parent {
      width: 100%;
      display: inline-flex; /*used inline-flex here, to mirrior your inline-block setting, but you can use flex*/
      height: 120px;
      background: #000;
      padding: 10px;
      box-sizing: border-box;
    }
    
    .child {
      display: inline-flex;
      flex-grow: 1;
      margin: 0 1%;
      height: 100px;
      background: #ffffd;
    }
    
    /*demontration for zero-height child elements*/
    .child:nth-child(2) {
      height: 0;
    }

提交回复
热议问题