Equal width using flex and border-box

前端 未结 5 1403
悲&欢浪女
悲&欢浪女 2020-12-21 01:35

I am using box-sizing: border-box; with varying border thicknesses within a flexbox. I want the elements within the flexbox to have equal widths, b

5条回答
  •  北荒
    北荒 (楼主)
    2020-12-21 01:54

    Instead of setting the flex to one, you can set the flex-basis to 20% and then the width will be divided equally:

    .container {
      display: flex;
      flex-direction: row;
      align-items: center;
      width: 100px;
    }
    
    .container .block {
      height: 28px;
      flex-basis: 20%;
      border: 1px solid black;
      box-sizing: border-box;
    }
    
    .container .block.selected {
      border: 3px solid blue;
    }
    0 1 2 3 4

提交回复
热议问题