Golden Apple Web & Design

Freelance Web Development, Graphic Design & Layouts, Wordpress Specialization

Home 9 CSS 9 Responsive Aspect Ratios With Pure CSS

Responsive Aspect Ratios With Pure CSS

by Justin Bennett | CSS, HTML5 | 16 comments

One of the hurdles I faced when beginning to work with responsive designs was the lack of an obvious way to assign an aspect ratio to a container in CSS. This kind of thing can be handled with an image placeholder or Javascript, but neither is an elegant solution. Fortunately, with a little bit of digging, an answer soon presented itself!

The Box Model

CSS gives us an easy way to give an element a “responsive” width declaration relative to the browser window size or other containing parent element by simply using a percentage. What we don’t have is a way to simply declare a responsive height relative to width, maintaining an aspect ratio. The solution I found was a lot simpler than expected and came from examining the  W3C’s box model recommendations.

Padding to the Rescue

The answer lies in the box model’s padding specs, of all places. We find that when declaring percentages instead of fixed values for padding, the percentage is calculated based on the WIDTH of the element in question, even if we are declaring a vertical value such as padding-top or padding-bottom. To take advantage of this for the purpose of maintaing our height relative to width, all we have to do is a little math!

Let’s look at the CSS:

figure {
	width: 36%;
	margin: 8px auto;
}

div.stretchy-wrapper {
	width: 100%;
	padding-bottom: 56.25%; /* 16:9 */
	position: relative;
	background: black;
}

div.stretchy-wrapper > div {
	position: absolute;
	top: 0; bottom: 0; left: 0; right: 0;
	color: white;
	font-size: 24px;
	text-align: center;
}

And the HTML:

<figure>
	<div class="stretchy-wrapper">
		<div>Relative Aspect Ratio! Try resizing the browser.</div>
	</div>
</figure>

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!

16 Comments

  1. u9r

    this is great idea thanks

    Reply
  2. Armin
  3. Robin
  4. Colin Azeltine
  5. Thomas
  6. Seba
  7. Carlos
  8. nika
  9. Michael Mahemoff

    BTW the padding hack doesn’t work with Flex containers on Firefox unfortunately (which is actually following the flex standard – https://stackoverflow.com/questions/23717953/padding-bottom-top-in-flexbox-layout.)

    The solution is to use object-fit: cover instead. It’s a recent standard to keep aspect ratio fixed.

    Reply
  10. shankar
  11. Boris 'pi' Piwinger
  12. Praveen Giri

    Another way to calculate the ratio is (B*100)/A

    75% = 4:3 = (3*100)/3 =300/3 = 75%
    66.66% = 3:2 = 200/3 = 66.66%
    62.5% = 8:5 = 500/8 = 62.5%

    i think this is easy

    Reply

Trackbacks/Pingbacks

  1. CSS object-fit | Bram.us - […] something like aspect-ratio() in CSS though, because setting aspect-ratios on elements right now is a nasty hack. […]
  2. [Twitter] CSS aspect ratio box sizing, where have you been a… | my little corner - […] CSS aspect ratio box sizing, where have you been all my life? goldenapplewebdesign.com/responsive-asp… […]
  3. Suitsupply Tech Blog Lazy images for responsive website optimization – part 2 of 2 – by Alexey Nekrasov - […] is a CSS trick, how to hold an approximate the same place of the page like image does, but it’s not…
  4. Responsive Css | Home - […] Responsive Aspect Ratios With Pure … – As you can see in the CSS, all we have to do…

Submit a Comment Cancel reply

Your email address will not be published.

哆哆女性网qq签名如何让人看不到旅游网站设计代码起名沫字及其含义网站ui设计的发展4月初8是什么日子英语网名ins风生产洗衣液的起什么名字好信阳网站优化超短个性签名什么seo做好拜丽德股市个性签名周易八字排盘特种兵在都市之诡刃海鲜店铺起名字店算命生辰八字什么命饭店用品属猪什么宝宝起名宜用字北京网站建设有限公司婚介起名周易和易经的区别陈崇伟什么珠宝品牌解梦被蛇咬了自己水芹种植第一厉害的恐龙周公解梦见鞋子杨洁艺术签名444hhh.com新华字典起名10月底出生宝宝起名淀粉肠小王子日销售额涨超10倍罗斯否认插足凯特王妃婚姻不负春光新的一天从800个哈欠开始有个姐真把千机伞做出来了国产伟哥去年销售近13亿充个话费竟沦为间接洗钱工具重庆警方辟谣“男子杀人焚尸”男子给前妻转账 现任妻子起诉要回春分繁花正当时呼北高速交通事故已致14人死亡杨洋拄拐现身医院月嫂回应掌掴婴儿是在赶虫子男孩疑遭霸凌 家长讨说法被踢出群因自嘲式简历走红的教授更新简介网友建议重庆地铁不准乘客携带菜筐清明节放假3天调休1天郑州一火锅店爆改成麻辣烫店19岁小伙救下5人后溺亡 多方发声两大学生合买彩票中奖一人不认账张家界的山上“长”满了韩国人?单亲妈妈陷入热恋 14岁儿子报警#春分立蛋大挑战#青海通报栏杆断裂小学生跌落住进ICU代拍被何赛飞拿着魔杖追着打315晚会后胖东来又人满为患了当地回应沈阳致3死车祸车主疑毒驾武汉大学樱花即将进入盛花期张立群任西安交通大学校长为江西彩礼“减负”的“试婚人”网友洛杉矶偶遇贾玲倪萍分享减重40斤方法男孩8年未见母亲被告知被遗忘小米汽车超级工厂正式揭幕周杰伦一审败诉网易特朗普谈“凯特王妃P图照”考生莫言也上北大硕士复试名单了妈妈回应孩子在校撞护栏坠楼恒大被罚41.75亿到底怎么缴男子持台球杆殴打2名女店员被抓校方回应护栏损坏小学生课间坠楼外国人感慨凌晨的中国很安全火箭最近9战8胜1负王树国3次鞠躬告别西交大师生房客欠租失踪 房东直发愁萧美琴窜访捷克 外交部回应山西省委原副书记商黎光被逮捕阿根廷将发行1万与2万面值的纸币英国王室又一合照被质疑P图男子被猫抓伤后确诊“猫抓病”

哆哆女性网 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化