html - :after pseudo-element on multi-line text

标签 html css layout pseudo-element

有什么方法可以将 :after 伪元素的样式应用于某些多行文本的每一行?

我的代码如下:

.container {
    width: 50px;
}

.text {
    position: relative;
}

.text:after {
    content: '';
    position: absolute;
    bottom: 4px;
    left: 0;
    width: 100%;
    height: 6px;
    background: linear-gradient(red, yellow);
    z-index: -1;
}
<div class="container">
    <p class="text">This is some multi-line text...</p>
</div>

从示例中可以看出,after 元素已正确应用,但仅应用于文本的最后一行,是否有任何方法可以让此样式出现在每一行上?

谢谢!

最佳答案

不,没有办法得到你想要的伪。

我发现获得这种效果的唯一方法是使用覆盖底层背景图像的背景图像。在此设置从白色到透明的略微倾斜的过渡。当从左向右移动时,它将覆盖/露出下面的图像

.container {
    width: 100px;
}

.text {
    position: relative;
    font-size: 30px;
    line-height: 1.2em;
    background-image: linear-gradient(to left top, transparent 49%, white 51%);
    background-size: 5000% 1.2em;
    background-position: left center;
    transition: background-position 2s;
}

.text:hover {
    background-position: right center;

}

.text:after {
    content: '';
    position: absolute;
    top: 0px;
    left: 0px;
    width: 100%;
    height: 100%;
    background: linear-gradient(to right, red, yellow);
    z-index: -1;
    background-size: 100% 1.2em;
    line-height: 1.2em;
}
<div class="container">
    <p class="text">This is some multi-line text...</p>
</div>

关于html - :after pseudo-element on multi-line text,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50658440/

相关文章:

java - 向组件 Vaadin 添加属性

javascript - 将点击事件添加到动态创建的按钮

php - 通过php获取html5上传的有值(value)的图片

html - Bootstrap 下拉菜单 a :hover not full li:hover

javascript - Array.insert 函数错误作为 Object function Array() { [native code] } has no method 'insert'

javascript - 将 Three.js 场景设置为 jQuery 模态窗口

javascript - 隐藏其他元素后将图像移动到第一个位置

ios - 调整父 View 的大小以使其大小等于其 subview 的大小

css - 使用CSS在鼠标悬停时更改侧面菜单的背景图像

php - 在 Wordpress 中使用字体