首页 > 解决方案 > 按钮中的文本进入:状态之前

问题描述

我正在使用此codepen中的第三个按钮。

.btn {
    line-height: 50px;
    height: 50px;
    text-align: center;
    width: 250px;
    cursor: pointer;
}    
.btn-three {
        color: #FFF;
        transition: all 0.5s;
        position: relative;
    }
    .btn-three::before {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        z-index: 1;
        background-color: rgba(0,0,0,1);
        transition: all 0.3s;
    }
    .btn-three:hover::before {
        opacity: 0 ;
        transform: scale(0.5,0.5);
    }
    .btn-three::after {
        content: '';
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        z-index: 1;
        opacity: 0;
        transition: all 0.3s;
        border: 1px solid rgba(255,255,255,0.5);
        transform: scale(1.2,1.2);
    }
    .btn-three:hover::after {
        opacity: 1;
        transform: scale(1,1);
    }

每当我将按钮背景的不透明度更改为完全黑色时,文本在 :before 状态下完全消失。

这有什么原因吗?似乎文本与按钮位于同一“层”中,而不是在其顶部

标签: htmlcssbuttonhover

解决方案


我建议将锚链接移到按钮标签之外,并使用 z-index 将文本强制放在前面。

CSS:

.btn-three {
  transition: all 0.5s;
  position: relative;
  line-height: 25px;
  height: 50px;
  align-items: center;
  text-align: center;
  width: 250px;
  cursor: pointer;
}


.btn-three::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 1;
    background-color: rgba(16, 84, 35, 1);
    transition: all 0.3s;
}
.btn-three:hover::before {
    opacity: 0 ;
    transform: scale(0.5,0.5);
}
.btn-three::after {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
    transition: all 0.3s;
    border: 1px solid rgba(0, 0, 0, 0.5);
    transform: scale(1.2,1.2);
    z-index: 2;
}

.three-text {
    position: relative;
    margin-top: -10px;
    z-index: 1;
    display: block;
}

.btn-three:hover::after {
    opacity: 1;
    transform: scale(1,1);
}

.button-wrapper {
    padding-top: 3%;
    text-align: center;
    padding-bottom: 3%;
}

HTML:

<div class="button-wrapper">
  <button class="btn btn-three" />
  <a class="three-text">HERE YOUR TEXT</a>
 </div>

代码笔示例


推荐阅读