How can I make an img and text link both link to the same place and both become active on hover independent of which one i am hovering over?

StackOverflow https://stackoverflow.com/questions/1404616

  •  05-07-2019
  •  | 
  •  

Question

I would like to be able to have hovering over the image invoke the hover property on the text link too that is next to it.

I have a image preview of a video and the video name written next to it as text string. I'd like to make it so i can hover on the picture and make the text still change color like a hover effect without having to make the image and text all one image , for obvious search and usability reasons; keeping text as text etc.

UPDATE: I also want to make sure the text link that is now in the "span" can not just move over using "margin-left" but can be raised up. By default in the code i have it just drops to the level of the bottom of the 60x60px image. I want it higher up so it looks nice but "margin-bottom" doesn't help that. i want it in the "Center height" of the 60x60 img... not at the bottom of it where it goes by default.

ANy ideas? thanks!

p.s. I'm open to the java script and jquery ideas some posted here but i am a complete newbie at that and will need extra explanation. I prefer CSS.

my code now is:

my html is:

<div id="example">
  <a href="#">
    <img src="image/source_60x60px.jpg">
    <span class="video_text">Image Text</span>
  </a>
</div> 

my css is:

div#example         { float:left; width:358px; height:60px; margin-top:20px; }   
div#example a       { color:#B9B9B9; width:100%;}  
div#example a:hover { color:#67a; text-decoration:none;}  
span.videotext      { margin-left:80px;} 
Was it helpful?

Solution

You could do this with CSS, or with Javascript. Without knowing your markup, and how your elements are laid out, it's not possible to determine which option is best.

CSS Option

a:hover span { color:red; }
<a href="1.html">
  <img src="1.jpg" />
  <span>Title: One</span>
</a>

In that example, we have our text and image both within a link. We are then modifying the color of the text on the :hover event.

Javascript (jQuery)

div.hovered p { color:red; }

<div class="preview">
  <img src="1.jpg" />
  <p>Title: One</p>
</div>

$("div.preview img").hover(
  function () { $(this).parent().addClass("hovered"); },
  function () { $(this).parent().removeClass("hovered"); }
);

This example showcases a solution that simply adds a class to the parent container, which in turn modifies the representation of the title. This event is controlled by hovering the image within the container.

Update:

Positioning your text isn't that difficult. The quick-and-easy method would be to position the parent container with relative, and the text with absolute:

<div class="container">
  <a href="#">
    <img src="1.jpg" />
    <span class="title">Hello World</span>
  </a>
</div>

div.container            { position:relative; width:150px; }
div.container span.title { position:absolute; top:0; left:50px; width:100px; }

That's a quick example. You can modify the values to your needs.

OTHER TIPS

What about

<style>
a 
{ 
    color: #000000; 
}
a:hover 
{ 
    color: #a9a9a9; 
}
</style>

<a href="#"><img src="imagepath" alt="Image Title" />&nbsp;Image Text</a>

Based on Jonathan's answer, but for javascript if you don't use jQuery:

<div onmousemove="hover('text1',true)" onmouseout="hover('text1',false)" >
  <img src="1.jpg" />
  <p id="text1">Title: One</p>
</div>

<script type="text/javascript">
function hover(elemID, on){
  element = document.getElementById(elemID);  
  if(on){
    element.style.color='red';
  }
  else
  {
    element.style.color='black';
  }
}
</script>

My apologies. I mis-read your question. Here is another stab at it:

HTML

<div class="video">
    <a href="" title="title">
        <img alt="title" src="path/to/image" />
        Video Title
    </a>
</div>

CSS

    .video {
        line-height: 75px;
        margin: 10px;
        padding: 10px;
        width: 200px;   
    }
    .video img {
        float: left;
        margin-right: 15px; 
    }
    .video a:hover {
        color: #hexcol; 
    }
    .video:after {
        clear: both;
        content: ".";
        display: block;
        height: 0;
        visibility: hidden; 
    }

Obviously, there are some styles in here that you don't need but I used them to test in a browser. If you want your text to vertically align with your video thumbnail, you will need to set the line-height property to the same height as the thumbnail image. That will push the text to the middle of the video.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top