『葉狂人』的怕失憶筆記 Front-End

讓你的 youtube 支援 Responsive

CSS 部分:

.video-container {
	position: relative;
	padding-bottom: 56.25%;
	padding-top: 30px; height: 0; overflow: hidden;
}
.video-container iframe,
.video-container object,
.video-container embed {
	position: absolute;
	top: 0;
	left: 0;
	width: 100%;
	height: 100%;
}

HTML 部分:

<div class="video-container">

         <iframe src="http://www.youtube.com/embed/dFVxGRekRSg" frameborder="0" width="560" height="315">iframe>

</div>

文章來源:Responsive Youtube Embed

Responsive Aspect Ratios With Pure CSS

As you can see in the CSS, all we have to do is nest an element with 100% width inside a “responsive” percentage-based-width parent element, and then declare a % for bottom or top padding based on the ratio we want to maintain. To calculate the percentage needed for any aspect ratio we can use the following formula:

B / (A / 100) = C%

So for 16:9 (where 16 is A and 9 is B):

9 / .16 = 56.25 (%)

And that’s all there is to it!

Some Other Common Ratios

For the not so mathematically inclined, here are some of the percentages for common aspect ratios:

75% = 4:3
66.66% = 3:2
62.5% = 8:5

Have fun and happy coding!

『葉狂人』的怕失憶筆記, author
「不要害怕擁有夢想,因為沒夢想的人生是空虛的。」
瑞士腕錶品牌Zenith 總裁 泰利那提夫