Aligning elements left and center with flexbox

前端 未结 7 488
眼角桃花
眼角桃花 2020-11-29 01:12

I\'m using flexbox to align my child elements. What I\'d like to do is center one element and leave the other aligned to the very left. Normally I would just set the left el

7条回答
  •  天命终不由人
    2020-11-29 01:49

    If you don't want to rely on positioning, the only way I've found that makes it truly centered is to use a combination of auto margin and negative margin prevent the centered element to getting pushed over by the left aligned element. This requires that you know the exact width of the left aligned element though.

    .container {
      height: 100px;
      border: solid 10px skyblue;
      
      display: flex;
      justify-content: center;
    }
    
    .block {
      width: 120px;
      background: tomato;
    }
    
    .justify-start {
      margin-right: auto;
    }
    
    .justify-center {
      margin-right: auto;
      margin-left: -120px;
    }

提交回复
热议问题