-6

How do I create an url link prefixed with a small thumbnail-image, such that when I hover on them, BOTH the link color and the thumbnail-image change

Example:
enter image description here

Im now using an image tag that goes with an anchor tag, Im able to change the anchor tag text color on hover, however I dont know how to change the img src accordingly

CSS:

.hoverable-link {
   color: gray;
}
.hoverable-link:hover {
   color: blue;
}

HTML:

<div>
  <img src="thumbnail-1">  //Change to thumbnail-2
  <a href="#" class="hoverable-link">Cool Link</a>
</div>

JsFiddle: https://jsfiddle.net/rbb5ow1v/9/

In conclusion:
[1] How can I change img src when it's on hover
[2] How can I trigger hover-event for both element at the same time

rocketspacer
  • 3,751
  • 3
  • 32
  • 46
  • Use the developer tools in your browser to inspect styles and techniques they used, and try to apply them on your project. – Alessio Cantarella Apr 15 '16 at 08:59
  • 1
    Alessio Cantarella thank you for your advice. Alot have been going on since I've started using stackoverflow and learning web development. I am sorry for the poor quality of the question I displayed. I have updated it with a more readable and easy to understand version, also with an answer of my understanding. Again I am truly sorry – rocketspacer Oct 16 '16 at 05:08

3 Answers3

0

I would give fontello.com a go

Once you have chosen the desired icons set up your tag as follows

 <a href="#"><span class="icon-twitter"></span>example</a>

When you do the CSS you just have to apply a hover state to the anchor and because of fontello it will change that colour too by just using the CSS color attribute.

EDIT:

If you are using a specific twitter icon that you made. Try changing it to an SVG, and change its fill. Same can be applied to the fontello where you can display none and reveal on hovers.

factordog
  • 589
  • 2
  • 21
0

[1] How can I change img src when it's on hover
You can't do this with CSS alone, as src is a DOM attribute not a CSS attribute, to accomplish this some javascript is required with HTML DOM Event system

<body>
<div>
  <img onmouseenter="highlight(this)" onmouseleave="unhighlight(this)" 
       src="thumbnail1">
  <a href="#potato" class="hoverable-link">Some Link</a>
</div>
<script>
  function highlight(image) {
    image.src = "thumbnail2"; //Blue Image
  }
  function unhighlight(image) {
    image.src = "thumbnail1"; //Gray Image
  }
</script>
</body>

JsFiddle: https://jsfiddle.net/f0c7p3tL/2/
List of DOM Events: http://www.w3schools.com/jsref/dom_obj_event.asp

Another approach is to not using the src DOM attribute at all. Instead you can use the background CSS attribute, that way you can utilize the CSS:hover selector
CSS:

#my-thumbnail {
  background: url("/thumbnail1") no-repeat;
  width: 32px;
  height: 32px;
}
#my-thumbnail:hover {
  background: url("/thumbnail2") no-repeat;
}

HTML:

<div>
  <img id="my-thumbnail">
  <a href="#potato" class="hoverable-link">Some Link</a>
</div>

JsFiddle: https://jsfiddle.net/7xoprwky/

[2] How can I trigger hover-event for both element at the same time
Again, two approaches are available here.

First is using javascript and the HTML DOM Events. In this approach, instead of triggering events on either of the child elements, we want them to be triggered on the surrounding <div> parent element. Then, in the event handler, we select the child elements and change their DOM Attribute accordingly

<body>
<div onmouseenter="highlight(this)" onmouseleave="unhighlight(this)">
  <img id="my-thumbnail" src="thumbnail1">
  <a   id="my-anchor" href="#potato">Some Link</a>
</div>
<script>
  var myThumbnail = document.getElementById('my-thumbnail'),
      myAnchor = document.getElementById('my-anchor');

  function highlight() {
    myThumbnail.src = "/thumbnail2";
    myAnchor.style.color = "blue";
    myAnchor.style.fontWeight = "bold";
  }

  function unhighlight() {
    myThumbnail.src = "/thumbnail1";
    myAnchor.style.color = "gray";
    myAnchor.style.fontWeight = "normal";
  }
</script>
</body>

JsFiddle: https://jsfiddle.net/2uthconL/

In the second approach we utilize the CSS selector syntax to highlight our internal element from our surrounding div

CSS:

#my-thumbnail-link {
}
#my-thumbnail-link img { /* Select all img tag inside div */
    background: url("/thumbnail1") no-repeat;
    width: 32px;
    height: 32px;
}
#my-thumbnail-link:hover img { /* Select all img tag inside div when it is hovered */
    background: url("/thumbnail2") no-repeat;
}
#my-thumbnail-link a { /* Select all a tag inside div */
    color: gray;
}
#my-thumbnail-link:hover a { /* Select all a tag inside div when it is hovered */
    color: blue;
    font-weight: bold;
}

HTML:

<div id="my-thumbnail-link" class="vcenter-parent">
  <img class="vcenter-child">
  <a href="#potato" class="vcenter-child">Some Link</a>
</div>

JsFiddle: https://jsfiddle.net/x61dy0mk/2/
More on CSS Selector: http://www.w3schools.com/cssref/css_selectors.asp

If your thumbnail is just a static asset, I recommended the CSS approach over the Javascript HTML DOM one for its readability and maintainability (imagine keeping thousand of event handler)

rocketspacer
  • 3,751
  • 3
  • 32
  • 46
-1

maybe you can try this one:

html

<a href="http://twitter.com/me" class="twitterbird" title="Twitter link"></a>

css for styling

.twitterbird {
 margin-bottom: 10px;
 width: 160px;
 height:160px;
 display:block;
 background:transparent url('twitterbird.png') center top no-repeat;
}

.twitterbird:hover {
   background-image: url('twitterbird_hover.png');
}

this answer is based on this question CSS: image link, change on hover

Update - Just try this one:

html

<ul>
  <li><a id="hoverable" href="#"><i class="home-icon"></i><span class="text">Link 1</span></a></li>
  <li><a id="hoverable" href="#"><i class="tshirt-icon"></i><span class="text">Link 2</span></a></li>
</ul>

css

a {
  color: black;
  text-decoration: none;
}

ul {
  list-style-type: none;
  padding: 0;
  margin: 0;
}

.home-icon {
  background: url("http://s1.postimg.org/gk5fbl6vv/home_black.png") no-repeat;
  width: 32px;
  height: 32px;
  display: inline-block;
  margin-right: 20px;
}

a:hover .home-icon {
    background: url("http://s2.postimg.org/43870q29h/home_green.png") no-repeat;
}

.tshirt-icon {
  background: url("http://s30.postimg.org/61bqc12fh/tshirt_black.png") no-repeat;
  width: 32px;
  height: 32px;
  display: inline-block;
  margin-right: 20px;
}

a:hover .tshirt-icon {
  background: url("http://s17.postimg.org/3x9qzn8sb/tshirt_green.png") no-repeat;
}

a#hoverable:hover {
  color: green;
  font-weight: bold;
}

demo is on this link https://jsfiddle.net/nv4dw8vr/

Community
  • 1
  • 1
Kotomono
  • 68
  • 1
  • 10