html - Overlap::after 和::before 与 CSS 中的文本

标签 html css

目标:在纯 CSS 中制作悬停按钮的漂亮效果,这将使用 ::after::before 伪元素。看看这个<强>jsFiddle 例子看看,我想达到什么目的。

代码:按钮将有一些样式,还有一个 background-color,在这个例子中是关闭的。

.button {
    display: inline-block;
    position: relative;
    height: 35px;
    line-height: 35px;
    padding: 0 15px;
    /*background-color: white;*/
    text-decoration: none;
    color: black;
}

问题:我想使用background-color,当我启用它时,我看不到伪元素。是这样的,因为这些伪元素有z-index: -1;,把它们放在了背景后面。当我将 z-index 更改为 01 时,文本不可见。

我不能做什么:我不能在按钮内添加新元素(如 span),因为这是一个已经在运行的网站,客户决定改变按钮的行为,所以我来了。这个网站上有很多按钮,这就是为什么我想用伪元素找到解决方案的原因,因为试图找到每个按钮并更改它们是不合适的。

最佳答案

如果我理解你的话,这就是你要找的:

.button {
    display: inline-block;
    position: relative;
    height: 35px;
    line-height: 35px;
    padding: 0 15px;
    /*background-color: white;*/
    text-decoration: none;
    color: black;
	border:1px solid;
}
a.button:before {
    content: " ";
    display: block;
    z-index: -1;
    position: absolute;
    height: 0%;
    top: 0;
    right: 0;
    left: 0;
    background: #ddd;
	transition: height 0.2s ease;
}

a.button:hover:before {
	height:100%;
}
<a href="#" class="button">TEST</a>

关于html - Overlap::after 和::before 与 CSS 中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37965872/

相关文章:

html - 保持 Div 居中

javascript - 轮播文本对齐

javascript - getElementById Javascript 不工作?

javascript - html刷新页面除了ajax外不再发送表单数据

javascript - 奇怪的搜索表单行为

html - 如何使用 Bootstrap 制作全宽响应式 div 部分?

javascript - 您可以使用 CSS 来引用某个对象的父项或子项吗?

javascript - 尽管使用了 jQuery,datepicker 仍然无法工作

css - 如何选择H1标签?

javascript - 是否可以通过拖动更改背景位置?