简体   繁体   English

使用过渡更改 hr 数据内容的颜色

[英]Change color of hr data-content with transition

I have the follow code in HTML and CSS:我在 HTML 和 CSS 中有以下代码:

 hr.hr-text { position: relative; border: none; height: 18px; background: rgb(51, 51, 51); -webkit-transition: color 1s; /* For Safari 3.0 to 6.0 */ transition: color 1s display 2s; /* For modern browsers */ } hr.hr-text::before { content: attr(data-content); display: inline-block; background: #fff; font-weight: bold; font-size: 1.30rem; color: rgb(59, 59, 59); padding: 0.2rem 2rem; position: absolute; top: 50%; left: 50%; text-align: center; transform: translate(-50%, -50%); } hr.hr-text:hover::after{ content: attr(data-content); display: inline-block; background: #fff; font-weight: bold; font-size: 1.30rem; color: #FF7550; padding: 0.2rem 2rem; position: absolute; top: 50%; left: 50%; text-align: center; transform: translate(-50%, -50%); }
 <div> <hr data-content="Publicaciones más visitadas" class="hr-text"> </div>

I would like that the change of "data-content" color has a transition of a few seconds.我希望“数据内容”颜色的变化有几秒钟的过渡。 And if it's posible change the border color with a transition too, same like "data-content".如果它也可以通过过渡改变边框颜色,就像“数据内容”一样。 Like you can see I try using "-webkit-transition" and "transition" but this doesn't work.如您所见,我尝试使用“-webkit-transition”和“transition”,但这不起作用。 Actually the change of color is instantly, looks good with I would like some more smoth and soft, that is because I want to use a transition.实际上颜色的变化是瞬间的,看起来不错,我想要更平滑一些,那是因为我想使用过渡。 Thanks for help.感谢帮助。

Not sure why you are using before and after to change the color.不知道为什么要在更改颜色之前和之后使用。 Just set hover with the before只需将 hover 与之前设置

 hr.hr-text { position: relative; border: none; height: 18px; background-color: rgb(51, 51, 51); transition: background-color 1s; } hr.hr-text:hover { background-color: #FF7550; transition: background-color 1s; } hr.hr-text::before { content: attr(data-content); display: inline-block; background: #fff; font-weight: bold; font-size: 1.30rem; color: rgb(59, 59, 59); padding: 0.2rem 2rem; position: absolute; top: 50%; left: 50%; text-align: center; transform: translate(-50%, -50%); transition: color 1s; } hr.hr-text:hover::before { color: #FF7550; transition: color 1s; }
 <div> <hr data-content="Publicaciones más visitadas" class="hr-text"> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM