图像背景颜色上的 flexbox 元素 css

flexbox element over image background color css

我一直在尝试将我的站点转换为 flexbox,但 运行 遇到了麻烦。我能够使用浮动实现的一个元素,我现在不能,因为使用 flexbox 时浮动不起作用。

基本上我有一张图片,当您将鼠标悬停在上面时,上面会弹出一些东西。我已经在工作了,但是背景颜色没有出现在图像上。我试图弄清楚如何让它出现在图像上,我已经尝试给元素一个非常高的 z-index 和一堆其他东西,但我空手而归。我希望我可以使用 css 背景图像而不是 html 图像,但我无法让它们在 css 中缩放和保持透视(必须保持背景图像的大小, 不是元素内容)

这是我的示例代码:

* {
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}

.dldoc p {
  -moz-transition: all 0.3s ease;
  -o-transition: all 0.3s ease;
  -webkit-transition: all 0.3s ease;
  -ms-transition: all 0.3s ease;
  transition: all 0.3s ease;
}

.docwrapper {
  display: flex;
  justify-content: space-between;
  max-width: 100%;
  height: 100%;
}

.dldoc {
  margin: .5em;
  display: block;
  text-decoration: none;
  overflow: hidden;
  min-width: 30vw;
  height: 55vw;
}

.dldoc p {
  height: 30vh;
  background: #f0f;
  padding: 0 8px;
  text-decoration: none;
  width: auto;
  z-index: 10000;
}

.dldoc strong {
  text-transform: uppercase;
  font-size: 7vw;
  font-weight: bold;
}

.dldoc em,
.dldoc p {
  font-size: 4.5vw;
}

.dldoc:hover>p {
  margin-top: -40vw;
}

.dlmap {
  padding: 0;
  height: 150px;
  margin-bottom: 1em;
}

.dlmap:hover,
.dlmap:active {
  top;
}

.dldoc img {
  z-index: 100;
  height: 55vw;
}

.dlmap img,
.dldoc img {
  width: 100%;
}

.dlmap strong {
  position: absolute;
  top: 0;
  left: 0;
  padding: 5px;
  color: #bbb;
  background: #233;
}

.dlmap a {
  text-transform: uppercase;
  left: 0;
  z-index: 1;
  position: absolute;
  top: 150;
  height: 75px;
  padding: 5px;
  background: #233;
  font-size: 1.5em;
  text-decoration: none;
  color: #7d1;
  width: 100%;
}

.dlmap a:hover {
  background: #7d1;
  color: #233;
}
<div class="docwrapper">
  <a href="#" class="dldoc" id="resumedl"><img src="https://i.imgur.com/eHu2WOp.png" alt="">
    <p><strong>download</strong><br>test1<br>PDF<br><br><em>test text</em></p>
  </a>
  <a href="#" class="dldoc" id="foliodl"><img src="https://i.imgur.com/eHu2WOp.png" alt="">
    <p><strong>download</strong><br>test2<br>ZIP<br><br><em>test text</em></p>
  </a>
</div>

这是一个常见的叠加问题,之前有人问过。关键是对父对象使用绝对定位和相对定位。将它推到视线之外并为其添加动画,通常使用 top.

在父级上使用相对定位的要点是包含绝对定位的元素。绝对定位元素将相对于最近定位的祖先元素定位自己,如果有 none,那将是 browser/viewport window。除此之外,您可以使用百分比值 top 顶部移动视图的叠加层 in/out 并设置叠加层的 height/width。

我已经用 /**/ 标记了所有 modifications/additions 到 CSS。

* {
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
}

.dldoc p {
  -moz-transition: all 0.3s ease;
  -o-transition: all 0.3s ease;
  -webkit-transition: all 0.3s ease;
  -ms-transition: all 0.3s ease;
  transition: all 0.3s ease;
}

.docwrapper {
  display: flex;
  justify-content: space-between;
  max-width: 100%;
  height: 100%
}

.dldoc {
  position: relative; /**/
  margin: .5em;
  text-decoration: none;
  overflow: hidden;
  min-width: 30vw;
  height: 55vw;
}

.dldoc p {
  position: absolute; /**/
  top: 100%; /**/
  margin: 0; /**/
  height: 100%;
  background: #ff00ff;
  padding: 0 8px;
  text-decoration: none;
  width: auto;
  /* z-index: 10000 */
}

.dldoc strong {
  text-transform: uppercase;
  font-size: 7vw;
  font-weight: bold;
}

.dldoc em,
.dldoc p {
  font-size: 4.5vw;
}

.dldoc:hover>p {
  top: 0; /**/
}

.dlmap {
  padding: 0;
  height: 150px;
  margin-bottom: 1em
}

.dlmap:hover,
.dlmap:active {
  top
}

.dldoc img {
  z-index: 100;
  height: 55vw
}

.dlmap img,
.dldoc img {
  width: 100%
}

.dlmap strong {
  position: absolute;
  top: 0;
  left: 0;
  padding: 5px;
  color: #bbb;
  background: #233
}

.dlmap a {
  text-transform: uppercase;
  left: 0;
  z-index: 1;
  position: absolute;
  top: 150;
  height: 75px;
  padding: 5px;
  background: #233;
  font-size: 1.5em;
  text-decoration: none;
  color: #7d1;
  width: 100%
}

.dlmap a:hover {
  background: #7d1;
  color: #233
}
<div class="docwrapper">
  <a href="#" class="dldoc" id="resumedl"><img src="https://i.imgur.com/eHu2WOp.png">
    <p><strong>download</strong><br/>test1<br/>PDF<br/><br/><em>test text</em></p>
  </a>
  <a href="#" class="dldoc" id="foliodl"><img src="https://i.imgur.com/eHu2WOp.png">
    <p><strong>download</strong><br/>test2<br/>ZIP<br/><br/><em>test text</em></p>
  </a>
</div>