How does flex-wrap work with align-self, align-items and align-content?

后端 未结 2 1698
别跟我提以往
别跟我提以往 2020-11-21 23:33

align-self

In the following code, align-self works with flex-wrap: nowrap.

2条回答
  •  孤城傲影
    2020-11-21 23:52

    First of all, align-content is useless if there is no wrap:

    w3c doc

    The align-content property aligns a flex container’s lines within the flex container when there is extra space in the cross-axis, similar to how justify-content aligns individual items within the main-axis. Note, this property has no effect on a single-line flex container.

    Also, in your second case, align-self is not failing. It's useless because the inner lines have been packed. Let's create an space so that the style can work:

    flex-container {
      display: flex;
      flex-wrap: wrap;
      align-content: flex-start;
      width: 250px;
      height: 250px;
      background-color: silver;
    }
    flex-item {
      flex: 0 0 50px;
      height: 50px;
      margin: 5px;
      background-color: lightgreen;
    }
    flex-item:last-child {
      align-self: flex-end;
      background-color: crimson;
    }
    flex-item:nth-child(5) {
      height: 90px;  /* added */
    }
    
      
      
      
      
      
      
    

    Similarly, in your 4th example, the style is working, if you set the conditions where it can be seen

    flex-container {
      display: flex;
      flex-wrap: wrap;
      align-items: flex-end;
      align-content: flex-start;
      width: 250px;
      height: 250px;
      background-color: silver;
    }
    
    flex-item {
      flex: 0 0 50px;
      height: 50px;
      margin: 5px;
      background-color: lightgreen;
    }
    flex-item:nth-child(2),flex-item:nth-child(5) {
      height: 90px;  /* added */
    }
    
      
      
      
      
      
      
    

    Finally, when you comment the align-content, it reverts to stretch, that's why the lines increase the size to fill up the container

    stretch Lines stretch to take up the remaining space. If the leftover free-space is negative, this value is identical to flex-start. Otherwise, the free-space is split equally between all of the lines, increasing their cross size.

提交回复
热议问题