{"id":3136,"date":"2024-02-15T13:51:43","date_gmt":"2024-02-15T18:51:43","guid":{"rendered":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-sp24\/?p=3136"},"modified":"2024-02-15T13:57:50","modified_gmt":"2024-02-15T18:57:50","slug":"responsive-images-and-video","status":"publish","type":"post","link":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-sp24\/responsive-images-and-video\/","title":{"rendered":"Responsive Images and Video"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Images are by default inline elements that are a certain size and will take up that space on your web site.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For responsive web design we need our images to be able to shrink in size if the container they are in is smaller than the image. For example:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The following needs to be in place for that to happen:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>The image must be inside a container.<\/li>\n\n\n\n<li>CSS is applied to the container to give it a responsive width (ie it\u2019s part of a CSS grid with an fr unit, or the container has a % width)<\/li>\n\n\n\n<li>CSS is applied to the image to allow it to change size to shrink if needed.<\/li>\n<\/ol>\n\n\n\n<h4 class=\"wp-block-heading\">1: Put the Image inside a container<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">This is relatively simple. Just remember to use a block element like div, section, paragraph, figure. Also it should have a class applied to the container (not required but easier to style later).<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;htmlmixed&quot;,&quot;mime&quot;:&quot;text\/html&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;hasCustomCSS&quot;:false,&quot;customCSS&quot;:&quot;&quot;,&quot;otterConditions&quot;:[],&quot;language&quot;:&quot;HTML&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;html&quot;}\">&lt;div class=&quot;imageContainer&quot;&gt;\n   &lt;img src=&quot;images\/myimage.jpeg&quot; alt=&quot;image of something cool&quot;&gt;\n&lt;\/div&gt;\n\n&lt;figure class=&quot;imageContainer&quot;&gt;\n    &lt;img src=&quot;images\/myimage.jpeg&quot; alt=&quot;image of something cool&quot;&gt;\n  &lt;figcaption&gt;Image of something cool by Awesome Person&lt;\/figcaption&gt;\n&lt;\/figure&gt;<\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">2: Apply Responsive Width to the Container<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">This one is hard to show an example. It depends on how you\u2019re doing the layout for your page. If you\u2019re using CSS grid then just make sure you use responsive units, like fr, for the widths of your columns.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here\u2019s an example three column grid with fr widths for the columns.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;css&quot;,&quot;mime&quot;:&quot;text\/css&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;hasCustomCSS&quot;:false,&quot;customCSS&quot;:&quot;&quot;,&quot;otterConditions&quot;:[],&quot;language&quot;:&quot;CSS&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;css&quot;}\">.container{\n    display: grid;\n    grid-template-columns: 1fr 1fr 1fr;\n    grid-gap: 1rem;\n}<\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">3: Apply CSS to the Image to Allow it to Change Size<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Notice we did not say anything about growing. Generally, you&nbsp;<strong>DO NOT<\/strong>&nbsp;want the image to grow to fill its container. It could become pixellated and not look good. At first, we will load in images that are large enough to fill the space we need. Another post will show a more advanced responsive method that can load different sized images.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;css&quot;,&quot;mime&quot;:&quot;text\/css&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;hasCustomCSS&quot;:false,&quot;customCSS&quot;:&quot;&quot;,&quot;otterConditions&quot;:[],&quot;language&quot;:&quot;CSS&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;css&quot;}\">\/* Basic Responsive Images and Video *\/\nimg,video{\n  width: auto; \n  height: auto;\n  max-width: 100%; \n}\n<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Setting the width and height to auto allows it to change size. Setting the max-width to 100% means it won\u2019t grow larger than its original size.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can also just copy the CSS from my&nbsp;<a href=\"https:\/\/gist.github.com\/profstein\/ff67cb749716f2d6a5d95c98350930cb\" target=\"_blank\" rel=\"noreferrer noopener\">Base Responsive CSS<\/a>&nbsp;file which has this style and the responsive video embed styles that I show below.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is an example of an image inside a container that is smaller than the image where no CSS has bee applied to the image:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"720\" height=\"320\" src=\"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-sp24\/wp-content\/uploads\/sites\/3596\/2024\/02\/responsive-image-outside-border.png\" alt=\"700 by 300 image that is wider than a square red box it is meant to be in.\" class=\"wp-image-3144\" srcset=\"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-sp24\/wp-content\/uploads\/sites\/3596\/2024\/02\/responsive-image-outside-border.png 720w, https:\/\/openlab.bmcc.cuny.edu\/mmp-350-sp24\/wp-content\/uploads\/sites\/3596\/2024\/02\/responsive-image-outside-border-300x133.png 300w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">And here is the same image with the CSS applied. The width of the image has changed so that it stays inside of the containing element.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"700\" height=\"153\" src=\"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-sp24\/wp-content\/uploads\/sites\/3596\/2024\/02\/responsive-image-in-border.png\" alt=\"700 by 300 image that is contained in a square red box it is meant to be in.\" class=\"wp-image-3145\" srcset=\"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-sp24\/wp-content\/uploads\/sites\/3596\/2024\/02\/responsive-image-in-border.png 700w, https:\/\/openlab.bmcc.cuny.edu\/mmp-350-sp24\/wp-content\/uploads\/sites\/3596\/2024\/02\/responsive-image-in-border-300x66.png 300w\" sizes=\"auto, (max-width: 700px) 100vw, 700px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">To see this in action&nbsp;<a href=\"https:\/\/codepen.io\/profstein\/pen\/mqMBoV\" target=\"_blank\" rel=\"noreferrer noopener\">view this CodePen Example<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">RESPONSIVE VIDEO<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">This is away to embed a video element that will resize responsively with your page. Actually, it allows you to put whatever you want in a box with a specific aspect ratio, but the most common use is for video which is either a 4:3 or 16:9 aspect ratio.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For this to work you need to add in HTML and CSS to your project.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">HTML to Add<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Create an HTML wrapper element. This is usually a div.<\/li>\n\n\n\n<li>Give that wrapping element two classes\n<ol class=\"wp-block-list\">\n<li>videoWrapper class<\/li>\n\n\n\n<li>One of the ratio classes: <strong>ratio-16-9<\/strong> or <strong>ratio-4-3<\/strong>.\n<ul class=\"wp-block-list\">\n<li>Side note: you can make your own ratio by dividing the height by the width (opposite of ratio) and making the result the padding-bottom percent. If you want 21:9 ratio then 9\u00f721 = .428571 so:<code>\u00a0.videoWrapper.ratio-21-9{ padding-bottom: 42.8571% }<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<\/li>\n\n\n\n<li>Copy iframe HTML from the site that has the video (YouTube or Vimeo). On YouTube click Share > Embed, then click on the iFrame HTML and copy it.\n<ul class=\"wp-block-list\">\n<li><img loading=\"lazy\" decoding=\"async\" width=\"150\" height=\"148\" class=\"wp-image-3137\" style=\"width: 150px;\" src=\"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-sp24\/wp-content\/uploads\/sites\/3596\/2024\/02\/screenshot-youtube-embed-code-iframe.png\" alt=\"screenshot of YouTube's embed video modal window showing iframe code.\" srcset=\"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-sp24\/wp-content\/uploads\/sites\/3596\/2024\/02\/screenshot-youtube-embed-code-iframe.png 424w, https:\/\/openlab.bmcc.cuny.edu\/mmp-350-sp24\/wp-content\/uploads\/sites\/3596\/2024\/02\/screenshot-youtube-embed-code-iframe-300x295.png 300w\" sizes=\"auto, (max-width: 150px) 100vw, 150px\" \/><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Example HTML. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You will need to replace the iframe with an iframe from YouTube or whatever streaming site you&#8217;re using (eg Vimeo).<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;htmlmixed&quot;,&quot;mime&quot;:&quot;text\/html&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;hasCustomCSS&quot;:false,&quot;customCSS&quot;:&quot;&quot;,&quot;otterConditions&quot;:[],&quot;language&quot;:&quot;HTML&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;html&quot;}\">&lt;div class=&quot;videoWrapper ratio-16-9&quot;&gt;\n\t&lt;!-- iframe below is Copy &amp; Pasted from YouTube replace with your own --&gt;\n\t&lt;iframe width=&quot;853&quot; height=&quot;480&quot; src=&quot;https:\/\/www.youtube.com\/embed\/IiJzbXzOdHQ&quot; frameborder=&quot;0&quot; allowfullscreen&gt;&lt;\/iframe&gt;\n  \n&lt;\/div&gt;<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">CSS to Add<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This part is straightforward. You can just copy and paste the CSS into your own CSS code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The CSS to copy is below.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;css&quot;,&quot;mime&quot;:&quot;text\/css&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;hasCustomCSS&quot;:false,&quot;customCSS&quot;:&quot;&quot;,&quot;otterConditions&quot;:[],&quot;language&quot;:&quot;CSS&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;css&quot;}\">.videoWrapper {\n\tposition: relative;\n\tpadding-top: 25px;\n\theight: 0;\n}\n\n.videoWrapper.ratio-16-9{\n    padding-bottom: 56.25%; \/* 16:9 *\/\n}\n\n.videoWrapper.ratio-4-3{\n    padding-bottom: 75%; \/* 4:3 *\/\n}\n\n.videoWrapper iframe {\n\tposition: absolute;\n\ttop: 0;\n\tleft: 0;\n\twidth: 100%;\n\theight: 100%;\n}<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Full Responsive Video Example<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Here is a Codepen with a working example: <a href=\"https:\/\/codepen.io\/profstein\/pen\/mrMLRj\" target=\"_blank\" rel=\"noreferrer noopener\">Responsive Video Embed (codepen.io)<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Images are by default inline elements that are a certain size and will take up that space on your web site. For responsive web design we need our images to&hellip;&nbsp;<a href=\"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-sp24\/responsive-images-and-video\/\" rel=\"bookmark\">Read More &raquo;<span class=\"screen-reader-text\">Responsive Images and Video<\/span><\/a><\/p>\n","protected":false},"author":16,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"portfolio_post_id":0,"portfolio_citation":"","portfolio_annotation":"","neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","_themeisle_gutenberg_block_has_review":false,"openlab_post_visibility":"","footnotes":""},"categories":[15,14],"tags":[27,29,26],"class_list":["post-3136","post","type-post","status-publish","format-standard","hentry","category-css","category-html","tag-image","tag-media","tag-responsive"],"_links":{"self":[{"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-sp24\/wp-json\/wp\/v2\/posts\/3136","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-sp24\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-sp24\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-sp24\/wp-json\/wp\/v2\/users\/16"}],"replies":[{"embeddable":true,"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-sp24\/wp-json\/wp\/v2\/comments?post=3136"}],"version-history":[{"count":4,"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-sp24\/wp-json\/wp\/v2\/posts\/3136\/revisions"}],"predecessor-version":[{"id":3147,"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-sp24\/wp-json\/wp\/v2\/posts\/3136\/revisions\/3147"}],"wp:attachment":[{"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-sp24\/wp-json\/wp\/v2\/media?parent=3136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-sp24\/wp-json\/wp\/v2\/categories?post=3136"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/openlab.bmcc.cuny.edu\/mmp-350-sp24\/wp-json\/wp\/v2\/tags?post=3136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}