Evilmuffin
Evilmuffin

Reputation: 234

Place text over image when hover certain part of the image using css

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!

Upvotes: 0

Views: 2303

Answers (1)

I.G. Pascual
I.G. Pascual

Reputation: 5965

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>

Upvotes: 1

Related Questions