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

显示flex,对齐容器右下角的div

  •  1
  • Radex  · 技术社区  · 8 年前

    我需要在它的红色容器“content”的右下角显示黄色div“icon”。

    .root {
        display: inline-flex;
        background-color: red;
    }
    
    .content {
        display: flex;
        align-items: centerl;
        justify-content: centerl;
        height: 80px;
        width: 100%;
        background-color: red;
    }
    
    .icon {
        position: absolute;
        right: 0;
        bottom:0;
        background-color: yellow;
    }
    <div class="root">
        <div class="content">
            content
        </div>
        <div class="icon">
          icon
        </div>
    </div>
    3 回复  |  直到 8 年前
        1
  •  7
  •   Vikas Jadhav    8 年前

    而不是使用flex end属性可以实现的位置 align-self: flex-end;

    请参见下面的代码段:

    .root {
      display: inline-flex;
      background-color: red;
    }
    
    .content {
      display: flex;
      align-items: center;
      justify-content: center;
      height: 80px;
      width: 100%;
      background-color: red;
    }
    
    .icon {
       align-self: flex-end;
       background-color: yellow;
    }
    <div class="root">
      <div class="content">
        content
      </div>
      <div class="icon">
        icon
      </div>
    </div>
        2
  •  1
  •   Lucas Paz    8 年前

    .root {
      display: inline-flex;
      background-color: red;
      position: relative;
    }
    
    .content {
      display: flex;
      align-items: centerl;
      justify-content: centerl;
      height: 80px;
      width: 100%;
      background-color: red;
    }
    
    .icon {
      position: absolute;
      right: 0;
      bottom:0;
      background-color: yellow;
    }
    <div class="root">
      <div class="content">
        content
      </div>
      <div class="icon">
        icon
      </div>
    </div>

    position: relative;
    
        3
  •  1
  •   yunzen    8 年前

    在您呈现的HTML结构中 .icon 元素不是 这个 .container 元素。所以这和它没有关系。如果你改变结构 .容器 要素 position: relative ,它会起作用的。

    .root {
      display: inline-flex;
      background-color: red;
    }
    
    .content {
      display: flex;
      align-items: centerl;
      justify-content: centerl;
      height: 80px;
      width: 100%;
      background-color: red;
      position: relative;
    }
    
    .icon {
      position: absolute;
      right: 0;
      bottom:0;
      background-color: yellow;
    }
    <div class="root">
      <div class="content">
        content
        <div class="icon">
          icon
        </div>
      </div>
    </div>