Why does space-around allow flex items to overflow on the left side?

前端 未结 2 1587
误落风尘
误落风尘 2021-01-05 19:40

It seems that Chrome doesn\'t handle justify-content: space-around correctly when the content overflows the flex container, and the container is not set up to a

2条回答
  •  自闭症患者
    2021-01-05 20:14

    That's because when there isn't enough space, space-around behaves like center:

    If the leftover free-space is negative or there is only a single flex item on the line, this value is identical to center.

    And center behaves like you describe:

    If the leftover free-space is negative, the flex items will overflow equally in both directions.

    Instead, you can use space-between:

    If the leftover free-space is negative or there is only a single flex item on the line, this value is identical to flex-start.

    Of course, then you won't have half-size spaces on neither end of the flex line. You can insert pseudo-element to have full-size spaces, though.

    #container {
      display: flex;
      justify-content: space-between; /* Instead of space-around */
    }
    #container::before, #container::after {
      content: ''; /* Insert space before the first item and after the last one */
    }
    

    .container {
      display: flex;
      width: 500px;
      border: solid black;
      justify-content: space-between;
      overflow: auto;
      margin: 10px 0;
    }
    .container::before, .container::after {
      content: '';
    }
    .item {
      margin: 10px;
      background-color: red;
      display: table;
      font-size: 48pt;
      text-align: center;
    }
    .big > .item {
      min-width: 200px;
    }
    1
    2
    3
    4
    5
    6
    1
    2
    3
    4
    5
    6

提交回复
热议问题