4

I want to place a play icon on top of an image, which redirects to an youtube page. I also want to have hover effects on the image and icon, the image getting a black background with 0.5 opacity, and the icon just change color. But I'm still stuck on getting the icon in the middle of the image. My jsfiddle: https://jsfiddle.net/rgL0ebj7/.

My code:

.module-box {
display: inline-block;
position: relative;
width: 100%;}

.module-dummy {
margin-top: 100%;}

.module-body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;}

.play-button-div {
margin: 0 auto;
position: relative;
width: 100%;}

.play-button-icon i{
width: 100px;
height: 100px;
color: #0080ff;
background-color: #fff;
border-radius: 55px;
border: 2px solid #0080ff;
font-size: 80px;
text-align: center;
position: absolute;
padding-top: 10px;
padding-left: 10px;}

Thank you!

NFDS
  • 99
  • 1
  • 3
  • 12

5 Answers5

8

Check this simple solution without any negative margins:

.play-button-div {
  position: relative;
  display: inline-block;
}
.play-button-icon {
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
}
Naqash Malik
  • 1,707
  • 1
  • 12
  • 14
3

Instead of using negative margin you can use transform: translate(-50%, -50%) if you don't know element size

.play-button-icon {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

Without relative positioning on parent element button will be positioned relative to window. display: inline-block will make div size same as image

.play-button-div {
    margin: 0 auto;
   text-align: center;
   position: relative;
   display: inline-block;
}
Awais Qarni
  • 17,492
  • 24
  • 75
  • 137
Covik
  • 746
  • 6
  • 15
2

I would do it this way.

Ive included your hover states for the image too.

.module-box {
  display: inline-block;
  position: relative;
  width: 100%;
}

.module-dummy {
  margin-top: 100%;
}

.module-body {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
}

.play-button-div {
  margin: 0 auto;
  tex-align: center;
  position: relative;
  display: inline-block;
}

.play-button-div:before {
  content: "\f01d";
  font-family: FontAwesome;
  color: #0080ff;
  background-color: #fff;
  font-size: 80px;
  padding: .05em .2em;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  z-index: 99;
}

.play-button-div img {
  transition: opacity ease .5s;
}

.play-button-div:hover img {
  opacity: .6;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.2/css/font-awesome.min.css" rel="stylesheet"/>
<div class="module-box">
  <div class="module-dummy"></div>
  <div class="module-body">
    <a href=" #">
      <div class="play-button-div">
        <img src="https://www.nasa.gov/sites/default/files/styles/image_card_4x3_ratio/public/thumbnails/image/leisa_christmas_false_color.png?itok=Jxf0IlS4" />
      </div>
    </a>
  </div>
</div>
Aaron
  • 10,187
  • 3
  • 23
  • 39
0

This css will center the icon circle on top of the image, but the arrow icon is not positioned properly. I'll leave that part for you.

.play-button-div {
  position: relative;
  display: inline-block;
}
.play-button-icon {
  position: absolute;
  top: 50%;
  left: 50%;
  margin-left: -52px;
  margin-top: -52px;
}

This method is called negative margin

Juha Tauriainen
  • 1,581
  • 1
  • 12
  • 14
0

Give absolute position and left and top values to span class. It should work.

Use following CSS styles or classes:

.module-box {
    display: inline-block;
    position: relative;
    width: 100%;
}

.module-dummy {
    margin-top: 100%;
}

.module-body {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
}

.play-button-div {
    margin: 0 auto;
   tex-align: center;


}

.play-button-div i{
    width: 100px;
    height: 100px;
    color: #0080ff;
    background-color: #fff;
    border-radius: 50px;
      border: 2px solid #0080ff;
    font-size: 80px;
   text-align: center;
      top:20%;
   left:25%;
   position:absolute;
}
Sagar
  • 642
  • 3
  • 14