Nic*_*ick 7 html css linear-gradients css3
我有一个带有线性渐变背景,橙色边框和一些文字的按钮.当我将鼠标悬停在按钮上时,我希望背景变为透明而不更改按钮的其他属性.
我试图将不透明度转换为0,但显然,这将隐藏边框和文本.我也尝试过转换背景,但它不起作用,因为我没有要转换到的端点,因为它需要透明.
body {
background-color: darkblue;
}
.button {
background-image: linear-gradient(red,yellow);
border: solid orange 2px;
border-radius: 10px;
color: white;
padding: 15px 25px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 24px;
}Run Code Online (Sandbox Code Playgroud)
<button class="button">Submit</button>Run Code Online (Sandbox Code Playgroud)
使用伪元素作为背景,您可以轻松地做到这一点:
body {
background-color: darkblue;
}
.button {
border: solid orange 2px;
border-radius: 10px;
color: white;
padding: 15px 25px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 24px;
position: relative;
overflow: hidden;
z-index:0;
background:transparent;
}
.button::before {
content: "";
position: absolute;
z-index:-1;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-image: linear-gradient(red, yellow);
transition:1s;
}
.button:hover::before {
opacity:0;
}Run Code Online (Sandbox Code Playgroud)
<button class="button">Submit</button>Run Code Online (Sandbox Code Playgroud)
这是不使用伪元素的另一种想法,您可以依赖于更改background-colorand background-size。诀窍是使一种渐变颜色保持透明,这样我们就可以看到background-color(您可以将其渐变为透明)。然后增加background-size来隐藏底色,我们只会看到透明色。
body {
background-color: darkblue;
}
.button {
border: solid orange 2px;
background-image: linear-gradient(transparent, yellow);
background-size:100% 100%;
background-color:red;
border-radius: 10px;
color: white;
padding: 15px 25px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 24px;
transition:1s;
}
.button:hover {
background-color:transparent;
background-size:100% 500%;
}Run Code Online (Sandbox Code Playgroud)
<button class="button">Submit</button>Run Code Online (Sandbox Code Playgroud)
或考虑进行调整background-size以进行另一种过渡:
body {
background-color: darkblue;
}
.button {
border: solid orange 2px;
background:
linear-gradient(red, yellow),
transparent;
background-size:100% 100%;
background-position:left; /*change this to change the way the transtion happen*/
background-repeat:no-repeat;
border-radius: 10px;
color: white;
padding: 15px 25px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 24px;
transition:1s;
}
.button:hover {
background-size:0% 100%;
}Run Code Online (Sandbox Code Playgroud)
<button class="button">Submit</button>Run Code Online (Sandbox Code Playgroud)