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

使用display:flex时将div向右对齐

  •  0
  • David  · 技术社区  · 8 年前

    display: flex 适用于所有分区。

    但这禁止我使用 float:right 在右侧分区。

    <div style="border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: gray;height:30px;">
      <div style="float:left;line-height:30px;">Contact Details</div>
      <button type="button" class="edit_button" style="float: right;">My Button</button>
    </div>

    这是小提琴: http://jsfiddle.net/xQgSm/

    2 回复  |  直到 8 年前
        1
  •  2
  •   Michael Benjamin William Falcon    8 年前

    .container {
      border-bottom-width: 1px;
      border-bottom-style: solid;
      border-bottom-color: gray;
      height: 30px;
      display: flex;
      align-items: center; /* center vertically */
      justify-content: space-between; /* maximum space between items*/
    }
    <div class="container">
      <div>Contact Details</div>
    
      <button type="button" class="edit_button">My Button</button>
    
    </div>

    .container {
      border-bottom-width: 1px;
      border-bottom-style: solid;
      border-bottom-color: gray;
      height: 30px;
      display: flex;
      align-items: center;
    }
    
    .edit_button {
      margin-left: auto;
      /* align to right */
    }
    <div class=“容器”>
    <div>联系方式</div>
    
    <按钮类型=“button”class=“edit_button”>我的按钮(</按钮(>);
    
        2
  •  0
  •   kyun    8 年前

    <div style="display:flex;justify-content:space-between;border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: gray;height:30px;">
       <div style="float:left;line-height:30px;">Contact Details</div>
    
        <button type="button" class="edit_button" style="">My Button</button>
    
    </div>https://stackoverflow.com/questions/45881495/align-div-to-the-right-while-using-displayflex#

    justify-content:space-between