仅灰度按钮背景但不是文本
Grayscale only button background but not text
我有几个具有不同背景图像的按钮。
我想实现在默认情况下对它们使用灰度滤镜,并在悬停时移除灰度滤镜。
我已经有了这个,但问题是按钮的文本也变灰了,我想避免。
我不知道如何只在按钮背景而不是文本上应用灰度滤镜。
现在我有一个主要的 css class 用于按钮
.box {
-webkit-filter: grayscale(100%);
filter: grayscale(100%);
position:relative;
color: red;
cursor:pointer;
text-align: center;
width: 160px;
height: 160px;
}
.box:hover {
-webkit-filter: grayscale(0%);
filter: grayscale(0%);
color: blue;
}
我为 html 代码中的每个按钮应用了背景
<button onclick="buttonFunction()" button class="button box" style="background: url(background1.jpg); background-size: 100%;" >Gray button text:( </button>
知道如何仅添加灰度滤镜按钮背景并使按钮文本保持彩色吗?
谢谢
正如 Temani Afif 还指出的那样,伪元素是您的选择,尽管我想添加一些不同的定位方式,我认为这可能更精确,更容易适应响应式设计。
关键部分是使 .box 具有相同的高度和行高,这也将是我们的 :after 内容的高度。然后绝对定位(0-0-0-0)会自动居中
.box {
position: relative;
color: #ff0000;
text-align: center;
width: 300px;
height: 300px;
line-height:300px;
}
.box:before {
content: ' ';
position: absolute;
top: 0;
bottom: 0;
right: 0;
left: 0;
filter: grayscale(100%);
background: url(https://upload.wikimedia.org/wikipedia/commons/thumb/e/e9/16777216colors.png/220px-16777216colors.png);
background-size: cover;
}
.box:hover:before {
filter: grayscale(0%);
}
.box:after {
content: 'Text Text Text';
position: absolute;
left:0;
bottom:0;
right:0;
top:0;
display: inline-block;
vertical-align: middle;
}
<button type="button" class="box">Button</button>
将 filter
属性 应用于元素将影响该元素的 children。您将无法取消设置/重置 children 的过滤器 属性。
如果您不想/不能使用其他答案中的 pseudoelement
方法,那么您可以更改 HTML 以创建您想要的效果。
将图像添加为按钮中的 img
元素,并将文本放置在 span 中。然后,您可以将 filter
应用于 img
。
.box {
cursor: pointer;
width: 160px;
height: 160px;
position: relative;
padding: 0;
display: inline-flex;
justify-content: center;
align-items: center;
}
.box img {
-webkit-filter: grayscale(100%);
filter: grayscale(100%);
position: absolute;
max-width: 100%;
left: 0;
right: 0;
top: 0;
bottom: 0;
}
.box:hover img {
-webkit-filter: grayscale(0%);
filter: grayscale(0%);
}
.text {
color: red;
z-index: 1;
}
.box:hover .text {
color: blue;
}
<button onclick="buttonFunction()" button class="button box"><img src="https://unsplash.it/200x200"><span class="text">Gray button text:(</span></button>
我有几个具有不同背景图像的按钮。 我想实现在默认情况下对它们使用灰度滤镜,并在悬停时移除灰度滤镜。
我已经有了这个,但问题是按钮的文本也变灰了,我想避免。 我不知道如何只在按钮背景而不是文本上应用灰度滤镜。
现在我有一个主要的 css class 用于按钮
.box {
-webkit-filter: grayscale(100%);
filter: grayscale(100%);
position:relative;
color: red;
cursor:pointer;
text-align: center;
width: 160px;
height: 160px;
}
.box:hover {
-webkit-filter: grayscale(0%);
filter: grayscale(0%);
color: blue;
}
我为 html 代码中的每个按钮应用了背景
<button onclick="buttonFunction()" button class="button box" style="background: url(background1.jpg); background-size: 100%;" >Gray button text:( </button>
知道如何仅添加灰度滤镜按钮背景并使按钮文本保持彩色吗?
谢谢
正如 Temani Afif 还指出的那样,伪元素是您的选择,尽管我想添加一些不同的定位方式,我认为这可能更精确,更容易适应响应式设计。
关键部分是使 .box 具有相同的高度和行高,这也将是我们的 :after 内容的高度。然后绝对定位(0-0-0-0)会自动居中
.box {
position: relative;
color: #ff0000;
text-align: center;
width: 300px;
height: 300px;
line-height:300px;
}
.box:before {
content: ' ';
position: absolute;
top: 0;
bottom: 0;
right: 0;
left: 0;
filter: grayscale(100%);
background: url(https://upload.wikimedia.org/wikipedia/commons/thumb/e/e9/16777216colors.png/220px-16777216colors.png);
background-size: cover;
}
.box:hover:before {
filter: grayscale(0%);
}
.box:after {
content: 'Text Text Text';
position: absolute;
left:0;
bottom:0;
right:0;
top:0;
display: inline-block;
vertical-align: middle;
}
<button type="button" class="box">Button</button>
将 filter
属性 应用于元素将影响该元素的 children。您将无法取消设置/重置 children 的过滤器 属性。
如果您不想/不能使用其他答案中的 pseudoelement
方法,那么您可以更改 HTML 以创建您想要的效果。
将图像添加为按钮中的 img
元素,并将文本放置在 span 中。然后,您可以将 filter
应用于 img
。
.box {
cursor: pointer;
width: 160px;
height: 160px;
position: relative;
padding: 0;
display: inline-flex;
justify-content: center;
align-items: center;
}
.box img {
-webkit-filter: grayscale(100%);
filter: grayscale(100%);
position: absolute;
max-width: 100%;
left: 0;
right: 0;
top: 0;
bottom: 0;
}
.box:hover img {
-webkit-filter: grayscale(0%);
filter: grayscale(0%);
}
.text {
color: red;
z-index: 1;
}
.box:hover .text {
color: blue;
}
<button onclick="buttonFunction()" button class="button box"><img src="https://unsplash.it/200x200"><span class="text">Gray button text:(</span></button>