How to justify a single flexbox item (override justify-content)

后端 未结 6 1938
慢半拍i
慢半拍i 2020-12-07 07:11

You can override align-items with align-self for a flex item. I am looking for a way to override justify-content for a flex item.

6条回答
  •  暖寄归人
    2020-12-07 07:16

    To expand on Pavlo's answer https://stackoverflow.com/a/34063808/1069914, you can have multiple child items justify-content: flex-start in their behavior but have the last item justify-content: flex-end

    .container {
      height: 100px;
      border: solid 10px skyblue;
      display: flex;
      justify-content: flex-end;
    }
    
    .container > *:not(:last-child) {
        margin-right: 0;
        margin-left: 0;
    }
    
    /* set the second to last-child */
    .container > :nth-last-child(2) {
        margin-right: auto;
        margin-left: 0;
    }
    
    .block {
      width: 50px;
      background: tomato;
      border: 1px solid black;
    }
    I should be at the end of the flex container (i.e. justify-content: flex-end)

提交回复
热议问题