【发布时间】:2021-09-06 04:38:25
【问题描述】:
我想在不更改 HTML 脚本的情况下更改“Elzero”中第一个字母“E”的颜色,但是当我使用 div::first-letter {color: #f44336} 时,除非我删除 :before 伪元素,否则颜色不会改变。为什么?
我尝试添加 !important : div::first-letter {color: #f44336 !important;} 但它没有改变。
这是代码:
div::first-letter {
color: #f44336;
}
div {
width: 600px;
margin: 0 auto;
background-color: #eee;
text-align: center;
padding: 0px;
padding-top: 25px;
padding-bottom: 25px;
font-size: 48px;
font-weight: 600;
letter-spacing: 7px;
position: relative;
border-right: 10px solid #673ab7;
border-left: 10px solid #f44336;
}
div::before {
content: "";
position: absolute;
top: 0px;
left: 0;
width: 100%;
height: 10px;
background-image: linear-gradient( to right, #f44336 0%, #f44336 20%, #2196f3 20%, #2196f3 40%, #4caf50 40%, #4caf50 60%, #e91e63 60%, #e91e63 80%, #673ab7 80%, #673ab7 100%);
}
div::after {
content: "";
position: absolute;
bottom: 0px;
left: 0;
width: 100%;
height: 10px;
background-image: linear-gradient( to right, #f44336 0%, #f44336 20%, #2196f3 20%, #2196f3 40%, #4caf50 40%, #4caf50 60%, #e91e63 60%, #e91e63 80%, #673ab7 80%, #673ab7 100%);
}
<div>Hello</div>
【问题讨论】:
-
您的代码似乎有效。
-
Firefox 不会将颜色应用于第一个字母,因为 :before 伪。删除它并使用 div 内的渐变并通过 background-size + no-repeat 调整大小。
标签: html css css-selectors pseudo-element