简体   繁体   English

如何清除文本装饰:之前在a.class css上悬停?

[英]How to clear text-decoration of :before in a.class css on hover?

I use Font Awesome to put an icon before my hyperlink. 我使用Font Awesome在我的超链接前放置一个图标。 I have a css for my hyperlink: 我的超链接有一个CSS:

a.cancel {
  font-family: 'Open Sans', sans-serif;
  font-size: 14px;
  font-weight: 600;
  font-style: normal;
  font-stretch: normal;
  line-height: 1.71;
  letter-spacing: normal;
  color: #eb1700;
  text-transform: uppercase;
  text-decoration: none;
}

a.cancel:before {
  font-family: "Font Awesome 5 Pro"; 
  content: "\f057";
  padding: 0 4px 0px 0px;
  font-weight: 300;
}

a.cancel:hover {
  text-decoration: underline;
}

a.cancel:hover:before {
  text-decoration: none;
  color: blue;
}

Unfortunately, the text-decoration isn't removed under my icon when hovered. 不幸的是,悬停时我的图标下没有删除文字装饰。 How can I fix this? 我怎样才能解决这个问题?

There are many ways to fix that, but here is how I usually proceed: 有很多方法可以解决这个问题,但以下是我通常的做法:

Set display: inline-block to the link, then float: left to its pseudo-element. 设置display: inline-block到链接,然后float: left到它的伪元素。

 a.cancel { font-family: 'Open Sans', sans-serif; font-size: 14px; font-weight: 600; font-style: normal; font-stretch: normal; line-height: 1.71; letter-spacing: normal; color: #eb1700; text-transform: uppercase; text-decoration: none; display: inline-block; /* + */ } a.cancel:before { font-family: "Font Awesome 5 Pro"; content: "\\f057"; padding: 0 4px 0px 0px; font-weight: 300; float: left; /* + */ } a.cancel:hover { text-decoration: underline; } a.cancel:hover:before { text-decoration: none; /* You can remove this line */ color: blue; } 
 <a href="#" class="cancel">hello</a> 

I'm guessing a bit on your HTML here. 我在这里猜你的HTML。 Assuming there is no additional HTML in your A tag, the short answer is: you can't have the text-decoration apply to only part of the A tag via CSS. 假设您的A标签中没有其他HTML,简短的答案是:您不能通过CSS将文本修饰仅应用于A标签的一部分。 It applies to all of the A tag or not. 它适用于所有A标签或不适用。 The :hover is triggering on the A tag. :悬停在A标签上触发。 The :before isn't truly a separate DOM element. :之前并不是真正独立的DOM元素。 The A:hover is triggering regardless of if you're over the :before portion or the regular portion, if that makes sense. A:悬停是触发,无论你是否超过:前一部分或常规部分,如果这是有道理的。

However, if don't mind adding a little extra HTML inside your A tag, you can have the underline apply only to that element: 但是,如果不介意在A标记中添加一些额外的HTML,则可以将下划线仅应用于该元素:

<a class="cancel" href="http://example.com"><i>http://example.com</i></a>

a.cancel:hover i {
  text-decoration: underline;
}

The above will tell it to put the underline only in the internal tag, not the whole A tag. 以上将告诉它只将下划线放在内部标记中,而不是整个A标记。 You can use whatever inline tag you want, such as span, etc. This may not be what you want, but without some sort of separation, you can't have the A:hover apply only to the text and not to the :before. 你可以使用你想要的任何内联标签,例如span等。这可能不是你想要的,但没有某种分离,你不能有A:hover仅适用于文本而不适用于:之前。 It's all one DOM element. 这是一个DOM元素。 If you inspect it in Chrome (and others), you'll see the ::before inside the A container. 如果你在Chrome(和其他人)中检查它,你会在A容器看到::之前。

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

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