0

Like the title said, I want to place a text box over an image when the mouse is hover over certain part of the image.

I was able to do it when the mouse is hover over ANY part of the image but not specific part.

Here is my html:

<h1>World Map</h1>
<div class="map">
  <img src="assets/strangemap.png" usemap="#testing">
  <map name="testing">
    <area shape="rect" coords="100,100,200,200" class="overlay">
  </map>
  <span class="text-content"><span>Strange World</span></span>
</div>

Here is my css:

.map {
  display: inline-block;
  position: relative;
  border-top: 3px solid $gray-lighter;
  border-left: 3px solid $gray-lighter;
  border-bottom: 3px solid $gray-light;
  border-right: 3px solid $gray-light;
}

.overlay {
  cursor: default;
}

span.text-content {
  background: rgba(0,0,0,0.5);
  color: white;
  display: table;
  height: 100px;
  left: 0;
  position: absolute;
  top: 0;
  width: 100px;
  opacity: 0;
  -webkit-transition: opacity 500ms;
  -moz-transition: opacity 500ms;
  -o-transition: opacity 500ms;
  transition: opacity 500ms;
}

span.text-content span {
  display: table-cell;
  text-align: center;
  vertical-align: middle;
}

.overlay:hover span.text-content {
  opacity: 1;
}

Thanks!

1
  • so what part is this then? Cause I am a bit at loss, you now target the overlay. Commented May 15, 2016 at 22:53

1 Answer 1

1

Have you tried this CSS line?

.map map:hover + span { opacity: 1 }

Here's a working example:

.map {
  display: inline-block;
  position: relative;
  border-top: 3px solid $gray-lighter;
  border-left: 3px solid $gray-lighter;
  border-bottom: 3px solid $gray-light;
  border-right: 3px solid $gray-light;
}
.overlay {
  cursor: default;
}
span.text-content {
  background: rgba(0, 0, 0, 0.5);
  color: white;
  display: table;
  height: 100px;
  left: 0;
  position: absolute;
  top: 0;
  width: 100px;
  opacity: 0;
  -webkit-transition: opacity 500ms;
  -moz-transition: opacity 500ms;
  -o-transition: opacity 500ms;
  transition: opacity 500ms;
}
span.text-content span {
  display: table-cell;
  text-align: center;
  vertical-align: middle;
}

img {
  width: 300px;
  height: 300px;
}
.map map:hover + span { opacity: 1 }
<h1>World Map</h1>

<div class="map">
  <img src="assets/strangemap.png" usemap="#testing">
  <map name="testing">
    <area shape="rect" coords="100,100,200,200" class="overlay">
  </map>
  <span class="text-content"><span>Strange World</span></span>
</div>

Sign up to request clarification or add additional context in comments.

4 Comments

@Evilmuffin Glad it helped :)
Here is a follow-up question if you don't mind me asking. Let's say if I want a different text-box "Dessert" pops up when I hover the mouse over 300,300,400,400, is it possible to do it with css or I will have to use something more advance?
You mean a different popup? I think you can do it with css targeting the area with an id or a class
Hmm...so something like... <div class="map"> ` <img src="assets/strangemap.png" usemap="#testing">` <map name="testing"> <area shape="rect" coords="100,100,200,200" class="overlay"> <area shape="rect" coords="300,300,400,400" class="overlay2"> </map> <span class="text-content"><span>Strange World</span></span> <span class="text-content2"><span>Dessert</span></span> </div>` And make the corresponding css for text-content2?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.