在flexbox悬停期间,我的文本隐藏在背景后面

时间:2017-04-27 00:10:38

标签: html css background flexbox z-index

我设置了一个flexbox代码(图片1),用于在悬停时进行弯曲,并在其上显示彩色叠加层和文本。问题是文本显示在彩色叠加层下面,我希望它在顶部(图2)。我尝试更改z-index,但这只会出于某种原因隐藏叠加层。我的代码如下。

* {
  box-sizing: border-box;
}

body {
  margin: 0;
}

.absolute-bg {
  z-index: -1;
  position: absolute;
  top: 0;
  left: 0;
  z-index: 0;
  height: 100%;
  width: 100%;
  background-position: 50%;
  background-repeat: no-repeat;
  background-size: cover;
  overflow: hidden;
}

#Text {
  display: none;
  z-index: 2;
}

.home-mast__container>*:hover #Text {
  background-color: black;
  margin-bottom: -90%;
  display: flex;
  text-align: right;
  color: white;
  margin-top: 50%;
  z-index: 1;
  font-size: 80px;
  justify-content: center
}

.home-mast {
  height: 100vh;
}

.home-mast__container {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  height: 100%;
}

.home-mast__container>* {
  position: relative;
  -webkit-box-flex: 1;
  -ms-flex-positive: 1;
  flex-grow: 1;
  padding: 1em;
  -webkit-transition: flex-grow 0.6s cubic-bezier(0.4, 0, 0.2, 1) 0.2s, - - webkit-box-flex 0.6s cubic-bezier(0.4, 0, 0.2, 1) 0.2s;
  transition: flex-grow 0.6s cubic-bezier(0.4, 0, 0.2, 1) 0.2s, -webkit- box-flex 0.6s cubic-bezier(0.4, 0, 0.2, 1) 0.2s;
  transition: flex-grow 0.6s cubic-bezier(0.4, 0, 0.2, 1) 0.2s;
  transition: flex-grow 0.6s cubic-bezier(0.4, 0, 0.2, 1) 0.2s, -webkit- box-flex 0.6s cubic-bezier(0.4, 0, 0.2, 1) 0.2s, -ms-flex-positive 0.6s cubic-bezier(0.4, 0, 0.2, 1) 0.2s;
}

.home-mast__container>*:hover {
  -webkit-box-flex: 3;
  -ms-flex-positive: 3;
  flex-grow: 3;
}

.home-mast__container>*:nth-child(1):after {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  z-index: 0;
  height: 100%;
  width: 100%;
  color: white;
  background-color: #ff2400;
  opacity: 0;
  -webkit-transition: opacity 0.4s ease-in-out;
  transition: opacity 0.4s ease-in-out;
}

.home-mast__container>*:nth-child(1):hover:after {
  opacity: 0.65;
  -webkit-transition-delay: 0.6s;
  transition-delay: 0.6s;
}
<div class="home-mast__container">
  <a href="http://23348472.nhd.weebly.com/ebenezer-tucker.html">
    <div>
      <div class="absolute-bg" style="background-image:url('http://23348472.nhd.weebly.com/uploads/9/9/7/3/99738320/eb-tuck_1_orig.png');">
        <h2 id="Text">Ebenezer Tucker</h2>
      </div>
    </div>
  </a>

1 个答案:

答案 0 :(得分:1)

我制作了容纳文字flex的容器。无需使h1成为弹性体。容器将使其居中或移动。见下面的代码

* {
  box-sizing: border-box;
}

 body {
 margin: 0;

}

 .absolute-bg {
z-index: -1;
position: absolute;
top: 0;
left: 0;
z-index: 0;
height: 100%;
width: 100%;
background-position: 50%;
background-repeat: no-repeat;
background-size: cover;
overflow: hidden;
   display: flex;
    align-items: center;
    justify-content: center;
}


#Text{
display: none;
z-index: 2;

}

.home-mast__container > *:hover #Text{
background-color: black;
text-align: right;
color: white;
z-index: 1;
font-size: 80px;
display: block
}

.home-mast {
height: 100vh;

}

.home-mast__container {
 display: -webkit-box;
 display: -ms-flexbox;
 display: flex;
 height: 100%;

}
.home-mast__container > * {
 position: relative;
  -webkit-box-flex: 1;
  -ms-flex-positive: 1;
      flex-grow: 1;
 padding: 1em;
   -webkit-transition: flex-grow 0.6s cubic-bezier(0.4, 0, 0.2, 1) 0.2s,                                -    - webkit-box-flex 0.6s cubic-bezier(0.4, 0, 0.2, 1) 0.2s;
   transition: flex-grow 0.6s cubic-bezier(0.4, 0, 0.2, 1) 0.2s, -webkit-       box-flex 0.6s cubic-bezier(0.4, 0, 0.2, 1) 0.2s;
  transition: flex-grow 0.6s cubic-bezier(0.4, 0, 0.2, 1) 0.2s;
  transition: flex-grow 0.6s cubic-bezier(0.4, 0, 0.2, 1) 0.2s, -webkit-     box-flex 0.6s cubic-bezier(0.4, 0, 0.2, 1) 0.2s, -ms-flex-positive 0.6s    cubic-bezier(0.4, 0, 0.2, 1) 0.2s;
 }
 .home-mast__container > *:hover {
   -webkit-box-flex: 3;
    -ms-flex-positive: 3;
      flex-grow: 3;


 }
.home-mast__container > *:nth-child(1):after {
 content: "";
 position: absolute;
 top: 0;
 left: 0;
 z-index: 0;
  height: 100%;
  width: 100%;
 color: white;
 background-color: #ff2400;
 opacity: 0;
 -webkit-transition: opacity 0.4s ease-in-out;
  transition: opacity 0.4s ease-in-out;
  }

 .home-mast__container > *:nth-child(1):hover:after {
  opacity: 0.65;
  -webkit-transition-delay: 0.6s;
      transition-delay: 0.6s;
   }
<div class="home-mast">
<div class="home-mast__container">
 <a href="http://23348472.nhd.weebly.com/ebenezer-tucker.html">
  <div>
   <div  class="absolute-bg" style="background-image:url('http://23348472.nhd.weebly.com/uploads/9/9/7/3/99738320/eb-tuck_1_orig.png');">
     <h2 id="Text">Ebenezer Tucker</h2>
    </div>
   </div>
  </a>
  </div>
</div>