简体   繁体   English

下划线悬停效果会占用整个空间,但只应占用文本空间

[英]Underline hover effect is taking the entire space, but it should take only the text space

I'm actually trying to add an underline effect on the text when it is hovered 我实际上是在悬停时在文本上添加下划线效果

But when I hover on the p element the underline is taking actually the entire space, what I want is the underline should be displayed only within the text not the complete space which is occupied by the text 但是当我将鼠标悬停在p元素上时,下划线实际上占据了整个空间,我想要的是下划线应仅在文本内显示,而不是文本所占据的完整空间。

To do that I actually added a span element and gave the hover effect to the span, but still it is working the same, 为此,我实际上添加了一个span元素并为该span赋予了悬停效果,但仍然可以正常工作,

Please check the effect I have added the code below, please help, Thanks in advance :) 请检查效果,我已经在下面添加了代码,请帮助,在此先感谢:)

 .underline-effect span:after{ content: ''; position: absolute; left: 0; display: inline-block; height: 1em; width: 100%; border-bottom: 2px solid; margin-top: 10px; opacity: 0; -webkit-transition: opacity 0.35s, -webkit-transform 0.35s; transition: opacity 0.35s, transform 0.35s; -webkit-transform: scale(0,1); transform: scale(0,1); } .underline-effect span:hover:after{ opacity: 1; -webkit-transform: scale(1); transform: scale(1); } 
 <p class="underline-effect"> <span>Home</span> </p> 

You need to add position: relative to your span element. 您需要添加position: relative对于span元素。

 .underline-effect span{ position: relative; } .underline-effect span:after{ content: ''; position: absolute; left: 0; display: inline-block; height: 1em; width: 100%; border-bottom: 2px solid; margin-top: 10px; opacity: 0; -webkit-transition: opacity 0.35s, -webkit-transform 0.35s; transition: opacity 0.35s, transform 0.35s; -webkit-transform: scale(0,1); transform: scale(0,1); } .underline-effect span:hover:after{ opacity: 1; -webkit-transform: scale(1); transform: scale(1); } 
 <p class="underline-effect"> <span>Home</span> </p> 

Your span needs to have a position: relative; 您的span需要有一个position: relative; that the span's pseudo-element can relate to: 跨度的伪元素可能与以下内容有关

 .underline-effect span { position: relative; } .underline-effect span:after { content: ''; position: absolute; top: 20px; left: 0; width: 100%; border-bottom: 2px solid; opacity: 0; -webkit-transition: opacity 0.35s, -webkit-transform 0.35s; transition: opacity 0.35s, transform 0.35s; -webkit-transform: scale(0, 1); transform: scale(0, 1); } .underline-effect span:hover:after { opacity: 1; -webkit-transform: scale(1); transform: scale(1); } 
 <p class="underline-effect"> <span>Home</span> </p> 

The code is doing what you told it to do. 该代码正在执行您要求的操作。 you set the width of the parent element to 100% and that is why your animation hover affect is taking up 100%. 您将父元素的宽度设置为100%,这就是为什么动画悬停效果占据100%的原因。

    .underline-effect span:after{
    content: '';
  position: absolute;
  left: 0;
  display: inline-block;
  height: 1em;
  width: 60px;/*changed from 100%*/
  border-bottom: 2px solid;
  margin-top: 10px;
  opacity: 0;
    -webkit-transition: opacity 0.35s, -webkit-transform 0.35s;
    transition: opacity 0.35s, transform 0.35s;
    -webkit-transform: scale(0,1);
    transform: scale(0,1);
}
.underline-effect span:hover:after{
    opacity: 1;
    -webkit-transform: scale(1);
    transform: scale(1);
}

see this fiddle 看到这个小提琴

EDIT: This will work but the above answer that putting position:relative on your span is a better solution imho 编辑:这将起作用,但上面的回答是:将position:relative放在您的span是更好的解决方案imho

Span needs a relative position, but another way, you can give border-bottom to span, and padding or line-height that, and when you hover span, border-bottom is shown. 跨度需要相对位置,但是另一种方式是,您可以给边界跨度设置跨度,并给其填充或线高,并且当您跨度跨度时,将显示边界跨度。 I think this way is better. 我认为这样更好。

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

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