0

I want an image to change colour (burgundy) when I hover over it, and show text at the same time (which relates to the image). I want the text and background colour to appear neatly over just the image (just covering the image only), but instead I get too much of the screen covered in burgundy (as shown in image below). I have created a jsfiddle which shows the problem I have. The image also needs to be responsive, so when hovering, the 'hover' effect will always just cover the image.

https://jsfiddle.net/cardiffsteve/hnow7yxp/

<div id="Profile-Picture">
  <div class="colleague-image">
    <img width="200" height="200" alt="" src="http://lorempixel.com/100/100/cats/">
  </div>
  <div class="colleague-text">
    <div class="colleague-name">Mr Big </div>
    <div class="colleague-job-title">The Boss </div>
    <div class="colleague-qualifications">Top Cat</div>
  </div>
</div>

#Profile-Picture {
  position: relative;
}

.colleague-text {
  position: absolute;
  top: 10px;
  left: 20px;
  bottom: 0;
  right: 0;
  background: rgba(142, 1, 60, 0.72);
  color: #fff;
  visibility: hidden;
  opacity: 0;
  text-align: center;
}

#Profile-Picture:hover .colleague-text {
  visibility: visible;
  opacity: 1;
}

enter image description here

CardiffSteve
  • 133
  • 6

5 Answers5

2

Give inline-block to #Profile-Picture:

#Profile-Picture {
  position: relative;
  display: inline-block;
}

This will make the relatively positioned container to take the dimensions of its contents.

Preview:

Fiddle: https://jsfiddle.net/x7cv536d/

The other attempt is to make the text both vertically and horizontally aligned middle:

#Profile-Picture {
  position: relative;
  display: inline-block;
}

.colleague-text {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  background: rgba(142, 1, 60, 0.72);
  color: #fff;
  visibility: hidden;
  opacity: 0;
  text-align: center;
}

.colleague-text .center {
  position: absolute;
  top: 50%;
  transform: translate(0, -50%);
  left: 0; right: 0;
  width: 100%;
}

#Profile-Picture:hover .colleague-text {
  visibility: visible;
  opacity: 1;
}

img {
  display: block;
  line-height: 1;
}
<div id="Profile-Picture">
  <div class="colleague-image">
    <img width="200" height="200" alt="" src="http://lorempixel.com/100/100/cats/">
  </div>
  <div class="colleague-text">
    <div class="center">
      <div class="colleague-name">Mr Big </div>
      <div class="colleague-job-title">The Boss </div>
      <div class="colleague-qualifications">Top Cat</div>
    </div>
  </div>
</div>

Preview:

I have used another div to scaffold for vertically centring.. :)

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
1

This worked for me:

#Profile-Picture {
  position: relative;
  display: inline-block;
}
.colleague-text {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  background: rgba(142, 1, 60, 0.72);
  color: #fff;
  visibility: hidden;
  opacity: 0;
  text-align: center;
}

Edit: Set top and left to 0, and add display: inline-block; to #Profile-Picture.

Result: Click to view image

Ray Hogan
  • 319
  • 3
  • 12
1

Just give display: table; to #Profile-Picture will make it as your expected.

Give transition:.6s all; to make hover effect nice.

And it works perfect if image is responsive Or it's size change.

Fiddle

ketan
  • 19,129
  • 42
  • 60
  • 98
0

Move id="Profile-Picture" to the image tag.

Arif Burhan
  • 507
  • 4
  • 12
0

Give your div with id Profile-Picture style display: inline-block.

#Profile-Picture {
  display: inline-block;
  position: relative;
}
.colleague-text {
  position: absolute;
  top: 10px;
  left: 20px;
  bottom: 0;
  right: 0;
  background: rgba(142, 1, 60, 0.72);
  color: #fff;
  visibility: hidden;
  opacity: 0;
  text-align: center;
}
#Profile-Picture:hover .colleague-text {
  visibility: visible;
  opacity: 1;
}
<div id="Profile-Picture">
  <div class="colleague-image">
    <img width="200" height="200" alt="" src="http://lorempixel.com/200/200/cats/">
  </div>
  <div class="colleague-text">
    <div class="colleague-name">Mr Big</div>
    <div class="colleague-job-title">The Boss</div>
    <div class="colleague-qualifications">Top Cat</div>
  </div>
</div>

The reason why your burgundy color overlays more than just the image, is because Profile-Picture takes entire document width, as it has display: block by default.


There is still a small problem at the bottom, though. Add these rules to fix it:
#Profile-Picture {
  line-height: 0;
}
.colleague-text {
  line-height: 1em;
}

The reason is that your burgundy color will overlay div with id Profile-Picture, which is a bit bigger than the the image due to the fact that it also contains whitespaces and not just the image.

Preview after fixes:

enter image description here

Przemek
  • 3,855
  • 2
  • 25
  • 33