按钮中的文本进入:之前状态

问题描述 投票:1回答:2

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

.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状态下完全消失。

任何原因吗?似乎文本与按钮位于同一“层”中,而不是位于其顶部

html css button hover
2个回答
1
投票

我建议将锚链接移至按钮标签之外,并使用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>

Codepen example


0
投票

如果要保持黑色不透明并将文本一直放在最前面,则需要使用z-index将文本“强制”到前面

.btn-three span {
    z-index: 2; 
    display: block;
    position: absolute;
    width: 100%;
    height: 100%;   
}

.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;
}

没有在div上设置跨度,您的文本将始终落后于它,因为::before状态的z索引设置在文本上方。

这将始终使其消失,将z-index设置为-1会将其发送到后面,但将不再使您的块变黑

.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;
}
© www.soinside.com 2019 - 2024. All rights reserved.