简体   繁体   English

使用文本装饰下划线向文本添加下划线-CSS

[英]Add underline to text with using text-decoration underline - CSS

Following is my HTML/CSS code in which I am trying to add a underline below the text without using native text-decoration: underline property. 以下是我的HTML / CSS代码,其中我尝试在文本下方添加下划线,而不使用本机text-decoration: underline属性。 But somehow this code is not working. 但是以某种方式,此代码无法正常工作。 Any pointers what I am doing wrong here 任何指针我在这里做错了

HTML Code - HTML代码-

<span>
  <a class="test">Add Underline To Text</a>
</span>

CSS Code - CSS代码-

.test {
  position: relative;
  text-decoration: none;
}

.test:hover {
  width: 100%:
}

.test:after {
  content: '';
  position: absolute;
  left: 0;
  bottom: -7px;
  height: 4px;
  background-color: green;
  width: 0;
  transition: width .25s;
}

Codepen Link - https://codepen.io/anon/pen/VEBNyx Codepen链接- https://codepen.io/anon/pen/VEBNyx

You want to add width to :after when you hover the .test link ? 您要在:after .test悬停.test链接:after增加width :after Then you need to write test:hover:after { } . 然后,您需要编写test:hover:after { } Not just test:hover . 不仅仅是test:hover

Even though :after is a pseudo-element you must select it if you want to change it's style/s 即使:after是伪元素,如果要更改其样式,也必须选择它

See below 见下文

 .test { position: relative; text-decoration: none; } .test:hover:after { width: 100%; } .test:after { content: ''; position: absolute; left: 0; bottom: -7px; height: 4px; background-color: green; width: 0; transition: width .25s; } 
 <span> <a class="test">Add Underline To Text</a> </span> 

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

相关问题 开发者在CSS中使用`text-decoration: underline;`创建下划线时是否可以更改为下划线高度? - Is it possible to change to the underline height from when the developer uses `text-decoration: underline;` in CSS to create an underline? CSS:悬停文本装饰:下划线在Safari上不起作用 - CSS :hover text-decoration: underline not working on Safari CSS 文本装饰:下划线自动应用于容器中的所有元素 - CSS text-decoration:underline auto appying to all the elements in the container 在悬停效果上设置文本装饰下划线 - Set text-decoration underline on hover effect 为什么文本装饰:下划线不起作用? - Why does text-decoration: underline not work? 幽灵下划线甚至文本装饰都没有 - Ghost underline even text-decoration is none 使用CSS文本修饰时删除一个像素间隙:下划线 - removing the one pixel clearance when using CSS text-decoration: underline 无法从移动网站的文本中删除“文本装饰:下划线” - Cannot remove `text-decoration:underline` from text in mobile website CSS文本修饰下划线在应用于输入按钮的文本时不起作用 - css text-decoration underline not working when applied to the text of an input button 如何制作文本装饰:用 2px 填充下划线? - How make text-decoration: underline with 2px padding?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM