CSS:跳过部分链接的下划线

问题描述 投票:6回答:4

是否可以有一个连续的链接,该文本通常在鼠标悬停时用下划线标出,但是中间有一个没有该下划线的部分(例如图像)?这不起作用:

<a href="#">one <span style="text-decoration:none;">two</span> <img src="img.png" style="border:0px; text-decoration:none;"> three</a>
html css image hyperlink underline
4个回答
3
投票
a, a:hover { text-decoration: underline; }
a img, a:hover img { text-decoration: none !important; }

2
投票
<a href="#" style="text-decoration:none;">
     <span style="text-decoration:underline;">one</span>  
    two
    <img src="img.png" style="border:0px; text-decoration:none;"> three
</a>

我认为只能使用如下所示的javascript。

下面的例子

<html>
<head></head>
  <style>
    a{
       text-decoration:none;
     }

    a:hover
     {
       text-decoration:none;
     }

    .sample
     {
        text-decoration:underline;
     }

    .sample_none
     {
        text-decoration:none;
     }
   </style>
   <script type="text/javascript">
      function show_underline(){
        document.getElementById('sample').className= 'sample';
      }

      function hide_underline(){
        document.getElementById('sample').className= 'sample_none';
      }
   </script>
    <a href="#" onmouseover="show_underline();" onmouseout="hide_underline();"> <span id="sample" class="sample_none">two</span>  
    <img src="img.png" style="border:0px;"> three
    </a>


</body>
</html>

P.S。我想知道是否可以仅使用CSS和html


1
投票

我真正想要的是将图像“附加”到链接上,而不会在悬停时加下划线。这是我想出的解决方案:

<a href="#" style="background:url('pic.png') no-repeat; background-position: left center; padding-left: 20px;">Link</a>
  • padding-left用于偏移文本,因此它不会与图片。
  • 使用背景位置您可以移动图像以使其更好与文本对齐。
  • 如果图像高于文字padding-toppadding-bottom可以用来调整外观。

此技术也可以与CSS sprites一起使用,如下所示:

<a href="#" style="background:url('sprites.png') no-repeat; background-position: -16px -16px; padding-left: 32px;">Link</a>

[我使用类似的技术来使用CSS Sprite代替普通的嵌入式图像:

<span style="background:url('sprites.png') no-repeat; background-position: -16px -16px; padding-left: 32px;"></span>

希望这可以帮助某人


1
投票

创建一个隐藏下划线的类,并添加一个下划线的子类。

.underline-some {
  text-decoration: none;
}

.underline-some:hover .text {
  text-decoration: underline;
}
<a href="#" class="underline-some">
  <span class="text">Boyz</span> 💈💈
  <span class="text">Men</span>
</a>

上面的示例代码仅强调悬停链接。对于始终带有下划线的链接,请删除:hover

© www.soinside.com 2019 - 2024. All rights reserved.