html - 如何从下到上更改文本的背景颜色

标签 html css

我正在尝试在悬停时使文本颜色从下向上或从上到下变化。

.box {
    width: 200px; height: 100px;
    background-size: 100% 200%;
    background-image: linear-gradient(to bottom, red 50%, green 50%);
    -webkit-transition: background-position 1s;
    -moz-transition: background-position 1s;
    transition: background-position 1s;
}

.box:hover {
    background-position: 0 -100%;
}
<div class="box">Text</div>

上面的代码使框改变颜色而不是文本。我该怎么做才能使文本颜色而不是框。

最佳答案

你必须使用 Background Clip :

.box {
  width: 200px;
  height: 100px;
  background-size: 100% 200%;
  background-image: linear-gradient(to bottom, red 50%, green 50%);
  -webkit-transition: background-position 3s;
  -moz-transition: background-position 3s;
  transition: background-position 3s;
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  font-weight: bold;
  font-size: 50pt;
}

.box:hover {
  background-position: 0 -100%;
}
<div class="box">Text</div>

注意:我把字号和时间调大到3秒,看看效果。

关于html - 如何从下到上更改文本的背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50090739/

相关文章:

html - Div 填充 wrapper 和边距的垂直空间

html - 不会排队

html - CSS悬停效果未完全覆盖所有图像

html - 如何让这两个 div 水平对齐并保持表单组完整?

css - 使社交媒体按钮(Facebook、Twitter 等)响应

html - 如何通过 typescript 获取 img 高度

javascript - 我在使用 Canvas putImageData 从另一个 Canvas /图像(png)复制透明像素时遇到问题

php - AJAX - 上传文件 (HTML5) 和 PHP

html - 需要帮助使 div 出现在屏幕底部,而其余的 div 滚动

html - html 或/和 css 中的图像标题悬停动画