css – :hover:before text-decoration没有没有效果?

前端之家收集整理的这篇文章主要介绍了css – :hover:before text-decoration没有没有效果?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
作为标题,我使用.icon- *添加图标。在超链接添加图标时:
  1. <a href="#" class="icon-email icon-large">Email me!</a>

由content属性插入的内容显示悬停时的下划线文本装饰。我想禁用文本装饰只为内容之前:

  1. [class^="icon-"]:before,[class*=" icon-"]:before {
  2. font-family: 'IcoMoon';
  3. font-style: normal;
  4. speak: none;
  5. }
  6. .icon-mail:before {
  7. content: "\37";
  8. }
  9. [class^="icon-large-"]:before,[class*=" icon-large"]:before {
  10. font-size: 48px;
  11. line-height: 48px;
  12. }
  13. a[class^="icon-"]:before,a[class*=" icon-"]:before {
  14. margin-right: 5px;
  15. vertical-align: middle;
  16. }

我试过这个,但它不工作(装饰仍然可见):

  1. a[class^="icon-"]:hover:before,a[class*=" icon-"]:hover:before {
  2. text-decoration: none;
  3. color: white;
  4. }

解决方法

作为其生成元素的 the :before pseudo-element is rendered as a descendant box(更具体地,就在第一子内容框之前),它遵守 the same rules its normal descendant boxes do with respect to text-decoration

The ‘text-decoration’ property on descendant elements cannot have any effect on the decoration of the ancestor.

有关详细信息,请参阅以下答案:

> CSS text-decoration property cannot be overridden by child element
> How do I get this CSS text-decoration override to work?

没有任何好的方法围绕这…唯一的替代品,立即想到的是:

>将文本包裹在其自己的span元素中,然后将text-decoration应用于该span,as shown by skip405.缺点是,当然是额外的标记
>使用inline块背景图像而不是图标字体中的内联文本与您的:before伪元素(我也已更正与您的类选择器的不一致):

  1. [class^="icon-"]:before,[class*=" icon-"]:before {
  2. display: inline-block;
  3. width: 1em;
  4. height: 1em;
  5. background-size: contain;
  6. content: "";
  7. }
  8. .icon-email:before {
  9. background-image: url(icon-mail.svg);
  10. background-repeat: no-repeat;
  11. }
  12. .icon-large:before {
  13. width: 48px;
  14. height: 48px;
  15. }
  16. a[class^="icon-"]:before,a[class*=" icon-"]:before {
  17. margin-right: 5px;
  18. vertical-align: middle;
  19. }

这个优于skip405的解决方案是,你不必修改HTML,但鉴于它使用SVG vector background imagesbackground-size,它将无法在IE8中工作。

如果你需要IE8支持,那么你必须回到位图图像:

  1. .icon-email:before {
  2. background-image: url(icon-mail.png);
  3. }
  4. .icon-email.icon-large:before {
  5. background-image: url(icon-mail-large.png);
  6. }

猜你在找的CSS相关文章