代码之家  ›  专栏  ›  技术社区  ›  London804

我是否可以使设置为对齐内容的元素:换行时间距向右对齐?

  •  1
  • London804  · 技术社区  · 7 年前

    我在一个容器中有两个元素,分别设置为“display flex”和“justify content space between”这使它们显示在容器中彼此相对的一侧。如果容器收缩,我希望左侧的元素堆叠在右侧元素的顶部,并与右侧对齐。我现在经历的行为是,它们会自动换行,但左侧的项目位于右侧元素上方,并与左侧对齐,而右侧的元素则与右侧对齐。如何使它们都对齐?

    请记住,我不能使用媒体查询,因为我有多个单元格,其中包含不同的数据。因此,项目将需要包装在多种媒体大小,我不会事先知道。请查看我的 CodePen 了解更多详细信息。

    .u-relative {
      position: relative;
    }
    
    .u-centerY {
      display: flex;
      align-items: center;
    }
    
    .u-fillRemaining {
      flex: 1;
    }
    
    .u-spaceBetweenX {
      justify-content: space-between;
    }
    
    .u-wrap {
      flex-wrap: wrap;
    }
    
    .Rtable-cell {
      padding: 0 0.625rem;
      max-width: 12rem;
      min-width: 6.25rem;
      border: 1px solid black;
    }
    
    .table-sum {
      // visibility: hidden;
      text-transform: uppercase;
      color: Grey;
      font-size: 0.75rem;
    }
    
    .t-alignRight {
      text-align: right;
    }
    
    @media only screen and (max-width: 400px) {
      .u-spaceBetweenX {
        justify-content: flex-end;
      }
    }
    <div class="u-relative Rtable-cell">
      <div class="u-centerY u-fillRemaining u-spaceBetweenX u-wrap">
        <span class="table-sum">sum</span>
        <div class="u-fillRemaining t-alignRight">
          <p>$4,000,000.00</p>
        </div>
      </div>
    </div>
    2 回复  |  直到 7 年前
        1
  •  2
  •   Thatkookooguy eglasius    7 年前

    justify-content: flex-end 使flex容器内的元素向右对齐。

    背景 flex: 1 0 6rem 意味着价值领域可以增长, 无法收缩 ,其基本大小是父级大小的一半。

    一旦容器的大小缩小到超过两个项目可以容纳在同一行中的程度,第二个项目将包装到第二行,这使得第一个元素粘在右侧(因为 对齐内容:柔性端

    如果希望在没有足够的文本空间时产生效果,可以将 value 通过将第二个参数设置为 1 。它不会收缩到超出其内容:

    flex: 1 1 6rem; /* now the minimum size can be less than 6rem if the text is shorter */
    

    setInterval(() => {
      $('.width-change').toggleClass('short');
    }, 2000);
    .kb-container {
      padding: 0 0.625rem;
      max-width: 12rem;
      min-width: 6.25rem;
      border: 1px solid black;
      display: flex;
      flex-wrap: wrap;
      justify-content: flex-end;
      background: white;
    }
    
    .kb-value {
      flex: 1 0 6rem;
      text-align: right;
    }
    
    
    /* not necessary. just for this demo */
    
    .width-change {
      width: 15rem;
      height: 7rem;
      transition: all 250ms;
      background: lightgrey;
    }
    
    .width-change.short {
      width: 9rem;
    }
    
    h3 {
      margin-bottom: 0;
    }
    
    .sub {
      margin: 3px 0;
    }
    
    body {
      overflow: hidden;
    }
    
    html {
      box-sizing: border-box;
    }
    
    html,
    body {
      margin: 0;
      padding: 0;
    }
    
    *,
    *:before,
    *:after {
      box-sizing: inherit;
    }
    
    .kb-example {
      width: 45vw;
      display: inline-block;
      height: 100vh;
    }
    
    .kb-example.kb-colorized .kb-text {
    background-color: #ccffcc;
    }
    
    .kb-example.kb-colorized .kb-value {
    background-color: #ccffff;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="kb-example">
      <h3>align items to the right using flex</h3>
      <div class="sub">(container will resize to demo the behavior)</div>
      <div class="width-change">
        <!-- YOU ONLY NEED THIS -->
        <div class="kb-container">
          <span class="kb-text">sum</span>
          <div class="kb-value">
            $4,000,000.00
          </div>
        </div>
        <!-- END -->
      </div>
    </div>
    
    <!-- with colorized elements -->
    <div class="kb-example kb-colorized">
      <div class="sub">and now with x-ray:</div>
      <div class="width-change">
        <!-- YOU ONLY NEED THIS -->
        <div class="kb-container">
          <span class="kb-text">sum</span>
          <div class="kb-value">
            $4,000,000.00
          </div>
        </div>
        <!-- END -->
      </div>
    </div>

    这里有一个 codepen 同样(它只包含此行为所需的代码,而不包含演示所需的额外代码)

    尽管这是一个 flex-end 容器,其行为类似于 space-between flex容器,因为第二个项目会增长,并且它的文本会向右对齐

        2
  •  0
  •   Adam ZBWR    7 年前

    将此添加到您的代码中,并将400px更改为您想要的任何值

    @media only screen and (max-width: 400px) {
      .u-spaceBetweenX {
      justify-content: flex-end;
     }
    } 
    

    如果您想让项目左对齐,请更改 justify-content: flex-start