显示flex,将div对准其容器的右下角

时间:2018-10-16 06:52:25

标签: html css css3 flexbox

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

我的脚本出了什么问题?

.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 个答案:

答案 0 :(得分:4)

您可以使用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>

答案 1 :(得分:1)

.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>

添加.root元素

position: relative;

答案 2 :(得分:1)

在HTML结构中,您展示的.icon元素不在内部 .container元素内。因此它不能与此相关。如果您更改结构并赋予.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>