Skip to content

Instantly share code, notes, and snippets.

@twkm
Last active August 24, 2016 22:35
Show Gist options
  • Save twkm/0a2bb57763575546940f to your computer and use it in GitHub Desktop.
Save twkm/0a2bb57763575546940f to your computer and use it in GitHub Desktop.
Responsive Video Wrapper

padding-bottom is calculated using the aspect ratio of the video. a 16:9 video has a padding-bottom of 56.25%, while a 4:3 video has a padding-bottom of 75%.

More detailed article at Smashing Magazine

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

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

Another great resource for responsive iframes, with SASS functions https://benmarshall.me/responsive-iframes/

@function ratio($width, $height) {
  return percentage( $height / $width);
}

.videowrapper  {
	    position: relative;
	    padding-bottom: 56.25%;
	    padding-top: 35px;
	    height: 0;
	    overflow: hidden;

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

	    &.16x9 {
	    	padding-bottom: ratio(16, 9);
	    }

	    &.4x3 {
	    	padding-bottom: ratio(4, 3);
	    }
	}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment