输入的 CSS 大小::在与输入大小相同之前

标签 css

<分区>

我正在使用一个带有渐变色背景的提交按钮。我想在悬停时为该颜色设置动画,尽管渐变颜色不是可设置动画的属性。我发现了一个很好的 CSS 技巧,可以使用::before 元素在 anchor 标记上进行此操作,但它似乎不适用于提交按钮。下面是使用 anchor 进行动画处理的示例代码。当 anchor 被提交替换时,为什么同样的代码不起作用?我认为这个问题与提交按钮上::before 元素的高度和宽度有关,但不明白为什么它没有选择按钮的大小。想法?

a {
  background: linear-gradient(to bottom, #3ccf2c 40%, #185400 100%);
  color: white;
  display: inline-block;
  padding: 5px;
  position: relative;
  z-index: 5;
}

a::before {
  background: linear-gradient(to bottom, #2d9b21 40%, #123f00 100%);
  content: '';
  display: block;
  height: 100%;
  left: 0;
  position: absolute;
  transition: opacity 0.4s;
  top: 0;
  opacity: 0;
  width: 100%;
  z-index: -5;
}

a:hover::before {
  opacity: 1;
}
<body>
  <a>submit</a>
</body>

最佳答案

您可以在悬停时为渐变背景制作动画。我为你创建了一个 fiddle ,所以你可以看到:https://codepen.io/mrmathewc/pen/oNNzeJr .

<a id="DemoGradient">Button</a>
#DemoGradient{
    background: -webkit-linear-gradient(#C7D3DC,#5B798E);
    background: -moz-linear-gradient(#C7D3DC,#5B798E);
    background: -o-linear-gradient(#C7D3DC,#5B798E);
    background: linear-gradient(#C7D3DC,#5B798E);

    -webkit-transition: background 1s ease-out;
    -moz-transition: background 1s ease-out;
    -o-transition: background 1s ease-out;
    transition: background 1s ease-out;

    background-size:1px 200px;
    border-radius: 10px;
    border: 1px solid #839DB0;
    cursor:pointer;
    width: 150px;
    height: 100px;

    padding: 10px;
    margin: 10px;
}
#DemoGradient:Hover{
    background-position:100px;
}

希望对您有所帮助。

关于输入的 CSS 大小::在与输入大小相同之前,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58439994/

上一篇:javascript - 如何在 php 中调整 margin session flash?

下一篇:javascript - 有没有办法在 Chrome 中使用 HTML 来更改 Android 中的导航栏颜色?

相关文章:

html - Bootstrap 链接不可点击

java - JSP 在使用 ResourceHandlerRegistry.addResourceHandler(基于 Java 的 webConfig)时找不到 css 文件

javascript - 使用 mousemove 移动背景后的右边距

html - 划掉一段文字并注意容器的最大宽度

javascript - HTML/CSS/JavaScript 未知的菜单方法需要特定的宽度并调整到这个宽度

javascript - 如何在一个函数中更改输入的 css 样式和一些文本?

css - 如何设置不包含特定元素的元素的样式

html - 位置为 :fixed overlaps page content 的页脚

css - LESS,悬停代码可以写成不同的吗?

html - 在图像上放置文本(已尝试此处的其他建议但没有用)