简体   繁体   English

css - 悬停文本时,下划线中的空格

[英]css - On hover text, empty space in underline

The hover needs to be work on the div, so I put hover rule to the div. 悬停需要在div上工作,所以我将悬停规则放到div上。 There is a bold number and text in it. 其中有一个粗体数字和文字。 If there is no "padding-right:1px" to the bold text, there is no problem, but when using it, the underline has an empty space on hover. 如果粗体文本没有“padding-right:1px”,则没有问题,但使用它时,下划线在悬停时有一个空白区域。 I must use padding-right:1px. 我必须使用padding-right:1px。

Note: Using   注意:使用  instead of "padding-right:1px" makes it too far, so its not a solution. 而不是“填充右:1px”使它太远,所以它不是一个解决方案。

在此输入图像描述

 .member:hover { text-decoration: underline; } 
 <div class="member"> <b style="padding-right:1px">4</b> members </div> 

How can I fix it ? 我该如何解决?

Take this: <b style="letter-spacing: 1px;">4</b> members instead of the padding option. 拿这个: <b style="letter-spacing: 1px;">4</b> members而不是padding选项。 I made a fiddle, so you can see that it works: https://jsfiddle.net/1w31kp4o/ 我做了一个小提琴,所以你可以看到它的工作原理: https//jsfiddle.net/1w31kp4o/

I hope this is what you wanted. 我希望这是你想要的。

Philipps solution is great, and probably the best for your current issue. Philipps解决方案很棒,可能是您当前问题的最佳解决方案。 Here are a few alternative approaches however. 然而,这里有一些替代方法。

Simple approach with border-bottom border-bottom简单方法

 .member:hover { display: inline-block; border-bottom: 1px solid black; } 
 <div class="member"> <b style="padding-right:1px">4</b> members </div> 

fiddle 小提琴


A more extreme approach with :after 一个更极端的方法:after

This one is probably a bit overkill for what you want, but this offers some flexibility if you need that. 这个可能对你想要的东西有点过分,但如果你需要的话,这会提供一些灵活性。 You can for example adjust the distance of the underline by changing the bottom value, as well as other things of course, such as changing the underline thickness, color, etc etc... 例如,您可以通过更改bottom值以及其他内容来调整下划线的距离,例如更改下划线粗细,颜色等等...

 .member { display: inline-block; position: relative; } .member:hover:after { content: ""; width: 100%; height: 1px; background: black; display: block; position: absolute; bottom: 1px; } 
 <div class="member"> <b style="padding-right:1px">4</b> members </div> 

fiddle 小提琴

You can use thin space ( &#8201; ): 您可以使用精简空间( &#8201; ):

Try this fiddle: https://jsfiddle.net/hkb60qnu/ 试试这个小提琴: https//jsfiddle.net/hkb60qnu/

This will make it for you. 这将使你。 It is a small tweak to your approach: 这是对您的方法的一个小调整:

HTML HTML

<div class="member">
  <b>4</b> members
</div>

CSS CSS

.member {
  display: inline-block;  
}

.member:hover {
  text-decoration: underline;
}

Here's a CodePen for you to take a look 这是一个CodePen供您查看

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

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