CSS 按钮将线性渐变背景转换为透明背景
CSS button transition linear-gradient background into transparent background
我有一个带有线性渐变背景、橙色边框和一些文本的按钮。当我将鼠标悬停在按钮上时,我希望背景变为透明而不更改按钮的其他属性。
我尝试将不透明度转换为 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;
}
<button class="button">Submit</button>
这可能有帮助:
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;
transition: 2s;
z-index: 1000;
}
button:hover{
background:linear-gradient(transparent, transparent);
}
<button class="button">Submit</button>
所以,我只是将悬停设置为透明的线性渐变,因为你没有固定的颜色。
现在我们可以使用新的 @property
. See the support table.
@property --alpha {
syntax: '<number>';
inherits: false;
initial-value: 1;
}
body {
background-color: darkblue;
}
.button {
background-image: linear-gradient(rgba(255, 0, 0, var(--alpha)), rgba(255, 255, 0, var(--alpha)));
transition: --alpha 1s;
background-color: transparent;
border: solid orange 2px;
border-radius: 10px;
color: white;
padding: 15px 25px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 24px;
}
.button:hover {
--alpha: 0;
}
<button class="button">Submit</button>
为背景使用一个伪元素,你可以很容易地做到这一点:
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;
}
<button class="button">Submit</button>
这是另一个不使用伪元素的想法,您可以依靠更改 background-color
和 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%;
}
<button class="button">Submit</button>
或考虑调整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%;
}
<button class="button">Submit</button>
在 Chrome 中启用实验性 Web 平台功能 (see) 我们可以使用:
CSS.registerProperty({
name: '--alpha',
syntax: '<number>',
initialValue: 1,
inherits: true,
})
body {
background-color: darkblue;
}
.button {
--alpha: 1;
background: linear-gradient(rgba(255,0,0,var(--alpha)), rgba(255,255,0,var(--alpha))) transparent;
border: solid orange 2px;
border-radius: 10px;
color: white;
padding: 15px 25px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 24px;
transition: --alpha 1s linear;
}
.button:hover {
--alpha: 0;
}
<button class="button">Submit</button>
我有一个带有线性渐变背景、橙色边框和一些文本的按钮。当我将鼠标悬停在按钮上时,我希望背景变为透明而不更改按钮的其他属性。
我尝试将不透明度转换为 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;
}
<button class="button">Submit</button>
这可能有帮助:
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;
transition: 2s;
z-index: 1000;
}
button:hover{
background:linear-gradient(transparent, transparent);
}
<button class="button">Submit</button>
现在我们可以使用新的 @property
. See the support table.
@property --alpha {
syntax: '<number>';
inherits: false;
initial-value: 1;
}
body {
background-color: darkblue;
}
.button {
background-image: linear-gradient(rgba(255, 0, 0, var(--alpha)), rgba(255, 255, 0, var(--alpha)));
transition: --alpha 1s;
background-color: transparent;
border: solid orange 2px;
border-radius: 10px;
color: white;
padding: 15px 25px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 24px;
}
.button:hover {
--alpha: 0;
}
<button class="button">Submit</button>
为背景使用一个伪元素,你可以很容易地做到这一点:
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;
}
<button class="button">Submit</button>
这是另一个不使用伪元素的想法,您可以依靠更改 background-color
和 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%;
}
<button class="button">Submit</button>
或考虑调整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%;
}
<button class="button">Submit</button>
在 Chrome 中启用实验性 Web 平台功能 (see) 我们可以使用:
CSS.registerProperty({
name: '--alpha',
syntax: '<number>',
initialValue: 1,
inherits: true,
})
body {
background-color: darkblue;
}
.button {
--alpha: 1;
background: linear-gradient(rgba(255,0,0,var(--alpha)), rgba(255,255,0,var(--alpha))) transparent;
border: solid orange 2px;
border-radius: 10px;
color: white;
padding: 15px 25px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 24px;
transition: --alpha 1s linear;
}
.button:hover {
--alpha: 0;
}
<button class="button">Submit</button>