{"id":505,"date":"2017-02-17T13:51:12","date_gmt":"2017-02-17T19:51:12","guid":{"rendered":"http:\/\/www.marcblase.com\/blog\/?p=505"},"modified":"2017-11-17T14:01:38","modified_gmt":"2017-11-17T20:01:38","slug":"method-to-crop-youtube-thumbnails-to-169","status":"publish","type":"post","link":"https:\/\/ma.rcbla.se\/blog\/2017\/02\/method-to-crop-youtube-thumbnails-to-169\/","title":{"rendered":"Method to crop YouTube thumbnails to 16:9 with CSS only"},"content":{"rendered":"<p>I have a site displaying mixed thumbnail images based on content created by the client consisting of either a featured image (sized via WordPress), YouTube thumbnails or a proxy image (static size). The featured image and proxy images are 16:9 sized images so there&#8217;s no problem there. YouTube thumbnail images, specifically hqdefault, are sized as 4:3 thus ruining the grid layout on the page I have all this content eventually displaying on. I wanted to find a low bandwidth solution to crop the YouTube thumbnails since the site was already looking to be on the heavy end. Without further adieu, my solution:<\/p>\n<p><code>hqdefault = 4:3 = 0.75 = 75% \/\/ pre-crop images<\/code><\/p>\n<p><code>16:9 = 0.5625 = 56.25% \/\/ cropped images<\/code><\/p>\n<p><code>75% - 56.25% = 18.75% \/\/ difference in size<\/code><\/p>\n<p>Now we take that difference in size, divide it in half and add that to the image as negative margins. Which creates our &#8220;crop&#8221;<\/p>\n<p><code>18.75% \/ 2 = 9.375%<\/code><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n\/\/ HTML\r\n&lt;figure&gt;\r\n  &lt;img src=&quot;https:\/\/img.youtube.com\/vi\/YOUR_VIDEO_ID\/hqdefault.jpg&quot; alt=&quot;&quot;&gt;\r\n&lt;\/figure&gt;\r\n<\/pre>\n<pre class=\"brush: css; title: ; notranslate\" title=\"\">\r\n\/\/ SCSS\r\nfigure {\r\n  overflow:hidden;\r\n\r\n  img {\r\n    margin:-9.375% 0;\r\n    display:block;\r\n    width:100%;\r\n    height:auto;\r\n  }\r\n}\r\n<\/pre>\n<p><a href=\"https:\/\/codepen.io\/marcblase\/pen\/PWvjqp\">Codepen<\/a> example.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have a site displaying mixed thumbnail images based on content created by the client consisting of either a featured image (sized via WordPress), YouTube thumbnails or a proxy image (static size). The featured image and proxy images are 16:9 sized images so there&#8217;s no problem there. YouTube thumbnail images, specifically hqdefault, are sized as [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,12,32,3],"tags":[],"class_list":["post-505","post","type-post","status-publish","format-standard","hentry","category-coding","category-css-coding","category-design","category-discoveries"],"_links":{"self":[{"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/posts\/505","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/comments?post=505"}],"version-history":[{"count":6,"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/posts\/505\/revisions"}],"predecessor-version":[{"id":540,"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/posts\/505\/revisions\/540"}],"wp:attachment":[{"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/media?parent=505"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/categories?post=505"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ma.rcbla.se\/blog\/wp-json\/wp\/v2\/tags?post=505"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}