views:

310

answers:

4

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;}
+2  A: 

What about

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

<a href="#"><img src="imagepath" alt="Image Title" />&nbsp;Image Text</a>
rahul
this does what i'm wanting ( ie the text link changes color on hover over the image as well as just the text link ) but the picture is a 60x60px img and the text no matter what ends up right at the bottom right corner of the picture. I want to make the text about 40 pixels over to the right of the img and half way down the 60x60px so it lines up nicely. The "span idea" presented by Jonathan Sampson below somewhat helps... but still ends up a bit short. I will explain there. thanks though!
Jonn
@John, try to use the property `line-height` to adjust the text's vertical position. If you use a line-height equal to the image's height then it should be vertically centered.
David Thomas
+2  A: 

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.

Jonathan Sampson
As i told "phoenix" above - your answer is similar to theirs but closer but still not perfect. this does what i'm wanting ( ie the text link changes color on hover over the image as well as just the text link ) but the picture is a 60x60px img and the text no matter what ends up right at the bottom right corner of the picture. I want to make the text about 40 pixels over to the right of the img and half way down the 60x60px so it lines up nicely. The "span idea" presented by you works so i can make the text link move over with a margin-left but it still is at the bottom line next to the img
Jonn
continued: Having it move over with the margin-left is helpful but i still need it to move up to look right margin-bottom does nothing. ANy ideas? I'm not sure how to use the jquery really as i'm really a newb so i prefer css if i can. 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;}
Jonn
@John: I've updated my solution to include information of positioning your titles.
Jonathan Sampson
Jonathan - YOU ARE THE MAN! That worked great! I never really had a firm grasp on various types of positioning. I've only had to use them in limited circumstances and never had a real use for absolute positioning in my limited experience. I also never really understood what relative positioning was based on its description, but now that i used absolute with its parent item as the "key item" of sorts set to relative... it makes so much more sense! thanks so much!
Jonn
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>
awe
A: 

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.

Lark
nope, that doesn't work. The image is not part of the link the way you set it up. also: it didn't help anything with the text link problem; ie it being stuck at the foot level of the image next to it. It still can only be moved using 'margin-left" and can't be raised up. thanks though.
Jonn
Adapt the above to something akin to `.video img:hover, .video img:hover + a {/* styles */}` I'm not sure how apply `:hover` to the `img` if the user `:hovers` over the `<a>`, though.
David Thomas