【问题标题】:flexbox element over image background color css图像背景颜色 css 上的 flexbox 元素
【发布时间】:2018-06-29 05:40:05
【问题描述】:

我一直在尝试将我的网站转换为 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>

【问题讨论】:

  • 这其实是一个常见的overlay问题,之前也有人问过。关键是在叠加层上使用绝对定位,在父级上使用相对定位。
  • > 这其实是一个常见的overlay问题,之前也有人问过。关键是在叠加层上使用绝对定位,在父级上使用相对定位。 – 饥饿之星 修复了它!完全忘记了您必须在父母中放置一个亲戚才能绝对像那样工作。谢谢!

标签: html css image flexbox background-color


【解决方案1】:

这是一个常见的叠加问题,之前已被问过。关键是对叠加层使用绝对定位,在父级上使用相对定位。通常使用top 将其推到视野之外并为其设置动画。

在父级上使用相对定位的要点是包含绝对定位的元素。绝对定位元素将相对于最近的定位祖先元素定位自己,如果没有,那将是浏览器/视口窗口。除此之外,您可以使用top top 的百分比值将叠加层移入/移出视图并设置叠加层的高度/宽度。

我已经用/**/ 标记了对 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-11
    • 1970-01-01
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多