8

Is it possible to have a contiguous link, where the text is normally underlined on mouse hover, but in the middle have a section (eg an image) without this underline? This does not work:

<a href="#">one <span style="text-decoration:none;">two</span> <img src="img.png" style="border:0px; text-decoration:none;"> three</a>
Biggles
  • 255
  • 1
  • 4
  • 7

4 Answers4

6

Make a class that hides underlines, and child class that adds them.

.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>

This example code above only underlines links on hover. For always-underlined links, remove :hover.

Chad von Nau
  • 4,316
  • 1
  • 23
  • 34
3
a, a:hover { text-decoration: underline; }
a img, a:hover img { text-decoration: none !important; }
eyelidlessness
  • 62,413
  • 11
  • 90
  • 94
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>

I think it can only be possible using javascript as follows.

LOOK FOLLOWING EXAMPLE

<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. I would like to know if it is possible with css and html only

Salil
  • 46,566
  • 21
  • 122
  • 156
  • This will force the underline to be on all the time. And if I make a special class to handle the hovering, it will break up the contiguous link. – Biggles Jun 04 '10 at 08:00
  • please paste your code after you done it. I would like to know how you handle the hovering. neways Really Good Question :) – Salil Jun 04 '10 at 08:04
  • Salil: Upon further testing I discovered that it didnnot solve my problem. – Biggles Jun 04 '10 at 08:05
  • check my example that uses javascript. – Salil Jun 04 '10 at 09:30
  • Your example works, but only for one "text part", since it uses an id to find the text to underline. Also, it seems a bit complex for such a simple thing. – Biggles Jun 04 '10 at 10:00
1

What I really wanted was to have an image "attached" to links, without it getting underlined on hover. Here is a solution I came up with:

<a href="#" style="background:url('pic.png') no-repeat; background-position: left center; padding-left: 20px;">Link</a>
  • padding-left is for offsetting the text so it does not overlap the image.
  • With background-position you can move the image to make it better aligned with the text.
  • If the image is higher than the text padding-top and padding-bottom can be used to tweak the appearance.

This technique can also be used with CSS sprites like this:

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

I used a similar technique to use CSS sprites instead of ordinary inline images:

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

Hope this helps someone

Biggles
  • 255
  • 1
  • 4
  • 7