Responsive youtube video player using css

One of the most frustrating things about video players is keeping the aspect ratio in responsive designs. Luckily there is a workaround using css which retains a ratio. you will need to give the player 100% height and width and add surrounding div container.


<div class="youtube">
    <iframe src="http://www.youtube.com/embed/jICRkfcOOD0" frameborder="0" width="100%" height="100%"></iframe>
</div>


The css will retain 16:9 aspect ratio for the iframe:

.youtube {
    position: relative;
    padding-bottom: 56.25%;
    padding-top: 30px; height: 0; overflow: hidden;
}

.youtube iframe {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}

Works cross browser, cross-device!

Here is a working example:

http://jsfiddle.net/kmturley/8kLnmd0r/

No comments:

Post a Comment