How to overlay a play button over a YouTube thumbnail image

北城以北 提交于 2019-12-02 17:31:22
<div class="video">
    <img src="thumbnail..." />
    <a href="#">link to video</a>
</div>

css

.video { position: relative; }

.video a {
   position: absolute;
   display: block;
   background: url(url_to_play_button_image.png);
   height: 40px;
   width: 40px;
   top: 20px;
   left: 20px;
}

I would do something like that. In any case, I don't think it's a good idea to post process the images to add the play button. What if you need to change the button design for instance?


If you want to center the button, a nice way to do it is to set top and left to 50%, and then adding negative margins equal to half of the size of the button:

.video a {
   position: absolute;
   display: block;
   background: url(url_to_play_button_image.png);
   height: 40px;
   width: 40px;
   top: 50%;
   left: 50%;
   margin: -20px 0 0 -20px;
}

One way to do it that provides a lot of flexibility without extra HTML markup (contrarily to most solutions suggested when people ask about image overlays) is using the :after CSS selector. Assuming there's a div of the "video" class around each image, something like:

.video:after {
    content: "";
    position: absolute;
    top: 0;
    width: 150px;
    height: 120px;
    z-index: 100;
    background: transparent url(play.png) no-repeat center;
    pointer-events: none;
}

Edit the values for width, height, and z-index as needed depending on the rest of your stylesheet. This works well without interfering with other code you might have, such as a div used to hold a caption.

Add a hover selector for extra snazz:

.video:hover:after { 
    content: ""; 
    position: absolute; 
    top: 0; 
    width: 150px; 
    height: 120px; 
    z-index: 100; 
    background: transparent url(play_highlight.png) no-repeat center; 
    pointer-events: none; 
}

You can also use a service

http://halgatewood.com/youtube/

Like this: http://halgatewood.com/youtube/i/youtube_id.jpg (redirects to amazon)

For example: http://halgatewood.com/youtube/i/aIgY20n3n0Y.jpg (redirects to amazon)

Edit:

the service was taken down, guess too many people bombarded him :) now it is on github

https://halgatewood.com/easily-add-play-buttons-to-youtube-video-thumbnails/ (link broken) https://github.com/halgatewood/youtube-thumbnail-enhancer

Here is another service that does the same thing (as suggested by muktesh patel):

http://www.giikers.com/iwc

Short, clean, fully responsive, without extra HTML:

<a class="youtube" href="http://youtube.com/watch?v={video id here}">
    <img src="http://i3.ytimg.com/vi/{video id here}/default.jpg" alt="{ video title here }" />
</a>

CSS:

.youtube {
    position: relative;
    display: inline-block;
}
.youtube:before {
    content: "";
    position: absolute;
    width: 100%;
    height: 100%;
    z-index: 10;
    background: transparent url({play icon url here}) center center no-repeat;
}

PS: You may even make play icon size responsive (depending on the size of the thumbnail) by adding background-size to the .youtube:before styles.

For example:

.youtube:before {
    background-size: 30%;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!