html - flexbox CSS : how to center 2 images - top/bottom

标签 html css flexbox

我尝试以图片为中心 - 一张图片放在另一张图片上。

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
}

div.out {
  display: flex;
  justify-content: center;
}

div.in {
  display: flex;
  flex-direction: column;
  justify-content: center;
}

div.div1 {
  background-color: violet;
  width: 100%;
  height: 100%;
}

.top {
  width: 100px;
  height: 100px;
  position: absolute;
  z-index: 999;
}

.bottom {
  width: 200px;
  height: 200px;
  position: absolute;
}
<div class="out div1">
  <div class="in">
    <img class='top' src="./one.jpg" />
    <img class='bottom' src="./background.jpg" />
  </div>
</div>

代码笔在这里:https://codepen.io/anon/pen/MzLRaR

我无法通过居中对齐来处理这些图像,例如: enter image description here

任何提示或如何以最佳方式处理它?<​​/p>

最佳答案

当您从默认的 flex-direction 切换到 flex-direction: column 时,“轴”发生变化,因此 justify-content 变为垂直对齐并且 align -items 变成水平的

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
}

div.out {
  display: flex;
  justify-content: center;   /* This centers horizontally */
  align-items: center;       /* This centers vertically */
}

div.in {
  display: flex;
  flex-direction: column;
  justify-content: center;    /* This centers vertically */
  align-items: center;        /* This centers horizontally */
}

div.div1 {
  background-color: violet;
  width: 100%;
  height: 100%;
}

.top {
  width: 100px;
  height: 100px;
  position: absolute;
  z-index: 999;
}

.bottom {
  width: 200px;
  height: 200px;
  position: absolute;
}
<div class="out div1">
  <div class="in">
    <img class='top' src="./one.jpg" />
    <img class='bottom' src="./background.jpg" />
  </div>
</div>

关于html - flexbox CSS : how to center 2 images - top/bottom,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53581036/

相关文章:

html - 面包屑标记 - 主页和当前页面?

javascript - 如何在 Javascript 中克隆整个表?

css - HTML 输入表单 css

html - Asp.net 从后面的代码动态隐藏带有 for 循环的 div

html - 在 flexbox 元素中获取 100% 高度的 div

html - 垂直对齐标签

javascript - 使用脚本单击基于 div id 和值的按钮

css - Grails Assets 管道 @font-face src url

css - flex-shrink 如何影响 padding 和 border-box?

html - 使用flexbox,如何align-content :stretch work when combined with align-items?