How would I add a transparent image over an image?

Transparent image.
https://i.imgur.com/DDdKwlk.png

<div class='outer'>
  <div class='tcell'>
    <div class='wrap'>
      <div class='inner'>
        <div class='image' onclick='thevid=document.getElementById(&apos;thevideo&apos;); thevid.style.display=&apos;block&apos;; this.style.display=&apos;none&apos;; document.getElementById(&apos;iframe&apos;).src = document.getElementById(&apos;iframe&apos;).src.replace(&apos;autoplay=0&apos;,&apos;autoplay=1&apos;);'/> </div>

      <div id='thevideo' style='display: none;'>

        <iframe frameborder='0' id='iframe' src='https://www.youtube.com/embed/bU8fLRxB4nI?rel=0&amp;keyboard=1&amp;disablekb=1&amp;vq=medium&amp;showinfo=0&amp;controls=1&amp;autoplay=0&amp;iv_load_policy=3&amp;fs=0&amp;wmode=transparent' />
      </div>
    </div>
  </div>
</div>
html,
body {
  height: 100%;
  background: #000;
  color: #fff;
  padding: 0;
  margin: 0;
}

.outer {
  display: table;
  height: 100%;
  margin: 0 auto;
  width: 100%;
}

.tcell {
  display: table-cell;
  vertical-align: middle;
}

.wrap {
  max-width: 400px;
  position: relative;
  margin: auto;
  border: 0px solid #5CB378;
}

.inner {
  position: relative;
  height: 0;
  padding-bottom: 100%;
  cursor: pointer;
  background-color: black;
}

#thevideo,
.image {
  position: absolute;
  left: 0px;
  top: 0px;
  right: 0px;
  bottom: 0px;
  height: 100%;
  width: 100%;
  background-color: #000000;
  cursor: pointer;
  overflow: hidden;
}

#thevideo iframe {
  width: 100%;
  height: 100%
}

.image {
  background: url('http://i.imgur.com/6cKbjZ7.png');
  background-size: cover;
  border-radius: 50px;
}

One way:

.image:after{
  content:"";
  position:absolute;
  left:0;
  top:0;
  right:0;
  bottom:0;
  background:url(https://i.imgur.com/DDdKwlk.png) no-repeat 0 0;
  background-size:cover;
  }
1 Like

That worked, thanks!

How come you left [ content:"";] blank?

Is something supposed to go there?

Well, if I remove it then the transparent image won’t show.

The ‘content’ property creates a pseudo element and without it there is no content (box generated) that you can style or apply images to. It works hand in hand with :before or :after pseudo elements.

Its like a blank span has been placed on the page.

<span></span>

You don’t need any content inside the span if you are going to size it and apply a background image. The same applies to the content property.

4 Likes

I did that here on blogger and the whole thing doesn’t reduce it’s size now. The aspect ratio stays the same and doesn’t change.

http://testpage34567.blogspot.com/

Is there something I can do to add in to fix it so that the aspect ratio changes?

It works on jsfiddle, but not working on blogger.

I have another question, if I reduce the size of the transparent image, how do I then center that in the middle blue box?

Also, when I do this the aspect ratio of the transparent image doesn’t change it’s size. Is there a way to fix that?

.image:after{
  content:"";
  position:absolute;
  width: 257px;
  height: 257px;
  left:0;
  top:0;
  right:0;
  bottom:0;
  background:url(https://i.imgur.com/HDbLxjZ.png) no-repeat 0 0;
  background-size:cover;
  }

Adding margin: auto Worked.

.image:after{
  content:"";
  margin: auto;
  position:absolute;
  width: 100px;
  height: 100px;
  left:0;
  top:0;
  right:0;
  bottom:0;
  background:url(https://i.imgur.com/DDdKwlk.png) no-repeat 0 0;
  background-size:cover;
  }

It isn’t broken, so there is nothing to fix.

Changing the width and height to a percent value will cause it to resize as the parent box resizes.

Is there a reason why I’m required to set height and width of the transparent image? And if I don’t, it scales the full length of the image?

257px of 400 comes out to 64.25%

1 Like

ronpat helped me with this.

Good Question!

{background-size:cover} tells the image to resize to fill the full area of its container. By applying a percentage width that is smaller than the width of the parent container the area covered by the image is reduced. BTW, height: on the container was a “magic number” that happened to work. My bad. In practice, you should not be using {background-size:cover} or {margin:auto} or the width and height properties.

Although it seemed to work, it was bad code. Therefore…

 
I would like to make a significant change to the code for the pseudo-element in my modified fiddle post.

The following is more efficient and logical IF one understands how the background properties work. With that understanding in mind, I would like to recommend that you avoid using the background shorthand command. Instead, I recommend that you write out each property so you can see what is happening. The reason for avoiding the shorthand command is to avoid causing problems if you are not aware of the defaults that it applies and to help you learn the properties.

I have made some comments beside these properties to help explain the reason they are used. You can find more information on the web or by asking here. For you I recommend
http://www.w3schools.com/cssref/default.asp
Click on a property in the left column to go to the page that describes it.
I strongly suggest that you take advantage of their “Play It” examples to see how properties behave.

My corrected code:

.image:after{
   content:"";    /* content is required with :before or :after to create the pseudo-element */
   position:absolute;   /* the pseudo-element is being set to the same size as an ancestoral container with position:relative */
   left:0;
   top:0;
   right:0;
   bottom:0;
   background-image:url(https://i.imgur.com/HDbLxjZ.png);  /* Noted as a memory aid. The arrow image is 257x257 px */
   background-repeat:no-repeat;  /* The background image should "not repeat".  The default is "repeat". */
   background-size:64.25% auto;  /* The size of the arrow and circle image is this percentage of the width of its parent container. */
   background-position:50% 50%;  /* The correct way to center the circle and arrow background-image over the blue area is to position it using {background-position:50% 50%}; not by using {margin:auto} in combination with {background-position:cover} and fixed or percent dimensions applied to the image. */
}
2 Likes

I was using margin:auto to center the button in the middle. But I’m going to look at the code now.

So, any image that’s larger than 400px,

background size should be 100%, which fits to size, unless you want it smaller than that.

So, essentially, {background-size:cover} is the same thing as doing 100% for any image that’s larger than the background image area. The only difference here is, this deals with only larger images and doesn’t blow up smaller images to fit to size.

In this case, we are assuming that the small image is already the desired size that it needs to be to fit over the max sized video box (you calculated the width of the image to be 64.25% of the width of the video box), so we assign that percent width as the background-size width it so it can scale down smaller as the video box scales smaller.

I was just saying that if it’s larger than 400px,

You can scale it down to:

24.25%

I mean, I could always resize the transparent image if I wanted to.

Using this:

If the total size of the transparent image with the circle and arrow were the same width as the video container, then no percentage adjustment would be needed to make it fit at the correct size over the video box.

Try this CSS with the attached image:

/* Example with wider image */
.image:after {
   content:"";    /* content is required with :before or :after to create the pseudo-element */
   position:absolute;   /* the pseudo-element is being set to the same size as an ancestoral container with position:relative */
   left:0;
   top:0;
   right:0;
   bottom:0;
   background-image:url("dotted-circle-w-arrow-400w.png");  /* Noted as a memory aid. The arrow image is 400x257 px */
   background-repeat:no-repeat;  /* The background image should "not repeat".  The default is "repeat". */
   background-position:50% 50%;  /* The correct way to center the circle and arrow background-image over the blue area is to position it using {background-position:50% 50%}; not by using {margin:auto} in combination with {background-position:cover} and fixed or percent dimensions applied to the image. */
   background-size:contain;
}

Note that I am using {background-size:contain} and that the image is not being resized until the page narrows and the video box narrows.

<observation>
After the vid plays, should the blue overlay reappear over the video box?

It doesn’t work


<div class='outer'>
  <div class='tcell'>
    <div class='wrap'>
      <div class='inner'>
        <div class='image' onclick='thevid=document.getElementById(&apos;thevideo&apos;); thevid.style.display=&apos;block&apos;; this.style.display=&apos;none&apos;; document.getElementById(&apos;iframe&apos;).src = document.getElementById(&apos;iframe&apos;).src.replace(&apos;autoplay=0&apos;,&apos;autoplay=1&apos;);'
        /> </div>

      <div id='thevideo' style='display: none;'>

        <iframe frameborder='0' id='iframe' src='https://www.youtube.com/embed/bU8fLRxB4nI?rel=0&amp;keyboard=1&amp;disablekb=1&amp;vq=medium&amp;showinfo=0&amp;controls=1&amp;autoplay=0&amp;iv_load_policy=3&amp;fs=0&amp;wmode=transparent' />
      </div>
    </div>
  </div>
</div>
html,
 body {
   height: 100%;
   background: #000;
   color: #fff;
   padding: 0;
   margin: 0;
 }
 
 .outer {
   display: table;
   height: 100%;
   margin: 0 auto;
   width: 100%;
 }
 
 .tcell {
   display: table-cell;
   vertical-align: middle;
 }
 
 .wrap {
   max-width: 400px;
   position: relative;
   margin: auto;
   border: 0px solid #5CB378;
 }
 
 .inner {
   position: relative;
   height: 0;
   padding-bottom: 100%;
   cursor: pointer;
   background-color: black;
 }
 
 #thevideo,
 .image {
   position: absolute;
   left: 0px;
   top: 0px;
   right: 0px;
   bottom: 0px;
   height: 100%;
   width: 100%;
   background-color: #000000;
   cursor: pointer;
   overflow: hidden;
 }
 
 #thevideo iframe {
   width: 100%;
   height: 100%
 }
 
 .image {
   background: url('http://i.imgur.com/6cKbjZ7.png');
   background-size: cover;
   border-radius: 50px;
 }
 
 .image:after {
   content: "";
   position: absolute;
   left: 0;
   top: 0;
   right: 0;
   bottom: 0;
   background-image: url("dotted-circle-w-arrow-400w.png");
   background-repeat: no-repeat;
   background-position: 50% 50%;
   background-size: contain;
 }

I replaced
dotted-circle-w-arrow-400w.png

with