<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>HTML on MXI</title><link>https://mxii.eu.org/tags/html/</link><description>Recent content in HTML on MXI</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Sun, 04 Dec 2016 23:45:23 +0300</lastBuildDate><atom:link href="https://mxii.eu.org/tags/html/index.xml" rel="self" type="application/rss+xml"/><item><title>HTML aspect ratio box using Custom Element</title><link>https://mxii.eu.org/2016/12/04/html-aspect-ratio-box-using-custom-element/</link><pubDate>Sun, 04 Dec 2016 23:45:23 +0300</pubDate><guid>https://mxii.eu.org/2016/12/04/html-aspect-ratio-box-using-custom-element/</guid><description>&lt;p>There is a standard trick to make HTML block with given proportions:&lt;/p>
&lt;pre>&lt;code>&amp;lt;div id=&amp;quot;containingBlock&amp;quot;&amp;gt;
&amp;lt;div id=&amp;quot;wrapper&amp;quot; style=&amp;quot;position:relative; width:100%; height:0px; padding-bottom:56.25%;&amp;quot;&amp;gt;
&amp;lt;div id=&amp;quot;content&amp;quot; style=&amp;quot;position:absolute; left:0; top:0; width:100%; height:100%&amp;quot;&amp;gt;
...
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code>&lt;/pre>
&lt;p>Since padding is computed according to containing element width, it&amp;rsquo;s possible to use it along with width to get required proportions (16:9 in this example, 56.25% = 100% * 9 / 16). And content block is positioned absolutely to use the space taken by padding.&lt;/p>
&lt;p>After learning about &lt;a href="https://developers.google.com/web/fundamentals/getting-started/primers/customelements">Custom Elements&lt;/a> I decided to see if I can abstract the trick and turn it into something clean and reusable. So it could be used just like this:&lt;/p>
&lt;pre>&lt;code>&amp;lt;ratio-box ratio=&amp;quot;16:9&amp;quot;&amp;gt;
...
&amp;lt;/ratio-box&amp;gt;
&lt;/code>&lt;/pre>
&lt;p>Custom Elements is &lt;a href="http://caniuse.com/#search=custom%20element">quite recent&lt;/a> feature in HTML, with version 0 available only in Chrome and version 1 just coming into play. It also requires JavaScript to work. So it is not practical to use yet, but still an interesting experiment.&lt;/p>
&lt;p>See the code and demo below. In addition, there is also a slightly improved HTML+CSS version.&lt;/p></description></item></channel></rss>