Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
		<h1>Responsive Images Demo</h1>

		<p>As part of making your websites more 'responsive friendly', there are a couple of proposals for responsive images that were developed by the <a href="https://responsiveimages.org/" target="_blank">Responsive Images Community Group</a>. There is the &lt;picture&gt; element which allows for 'art direction' of an image by using multiple sources, as well as the "srcset" and "sizes" attributes which allow us to offer browsers a variety of image resolutions to choose from.</p>

		<p>It is a good idea to take a look at <a href="http://caniuse.com/#search=srcset" target="_blank">how supported "srcset"</a> and <a href="http://caniuse.com/#search=picture" target="_blank">the &lt;picture&gt; element</a> are before use.</p>





		<section id="srcset">

			<h2>"srcset" Example</h2>



			<!--
				The srcset attribute allows us to define a comma separated list of different files and widths at which they should be loaded. In this case we are telling the browser to pick the image that makes sense for the display size that it has been loaded for:

					'img/Puppies_cropped-400.jpg 400w' is providing the file, and the pixel width of the original image. In this case 400 pixels. We use the 'w' (width) descriptor to define the 'true width' of the image.

				The more complicated part of this is the sizes attribute, where we are helping the browser understand how the image may be 'scaled' at different sizes. This part breaks our separation of styling (CSS) and content (HTML) somewhat, but allows for much more efficient and appropriate selection of images by the browser when needed. In this case we provide a comma-separated list of media queries that define the size the image will be at in those different queries:

					'(min-width:75rem) 50vw' is stating that at 75rem and larger, this image will be 50% of the viewport width. This lines up with our main.css file which switches to a two column layout at 75rem.

					The '100vw' value at the end of the list is the 'default' for this image.

				The img element below has been broken onto separate lines for readability. It is not required that you do this.
			-->
			<div class="grid two-col">
				<img 
					src="https://andrewh.ca/teaches/web_design_and_development/tutorials/05/explanation/img/Puppies_cropped-400.jpg" 
					alt="A collection of Nova Scotia Duck-tolling Retriever puppies"
					srcset=
						"https://andrewh.ca/teaches/web_design_and_development/tutorials/05/explanation/img/Puppies_cropped-400.jpg 400w, 
						https://andrewh.ca/teaches/web_design_and_development/tutorials/05/explanation/img/Puppies_cropped-1000.jpg 1000w, 
						https://andrewh.ca/teaches/web_design_and_development/tutorials/05/explanation/img/Puppies_cropped-1600.jpg 1600w"
					sizes=
						"(min-width: 75rem) 50vw,
						100vw">
				<div>
					<!-- A blank column for the purposes of our tutorial -->
				</div>
			</div>


		</section>




		<section id="picture">

			<h2>&lt;picture&gt; Example</h2>



			<!-- 
				Because the picture element defines a number of sources to work with, we need to open and close the element (unlike our img element).
			-->
			<picture>

				<!-- 
					Below the source element defines a series of images to load using the srcset attribute that we have seen before. In addition to this we use the media attribute - which behaves like our CSS media queries - to define a width at which the source attribute should be come active.
				-->
				<source srcset="https://andrewh.ca/teaches/web_design_and_development/tutorials/05/explanation/img/Puppies-1000.jpg 1000w, https://andrewh.ca/teaches/web_design_and_development/tutorials/05/explanation/img/Puppies-2000.jpg 2000w, https://andrewh.ca/teaches/web_design_and_development/tutorials/05/explanation/img/Puppies-3000.jpg 3000w" media="(min-width: 60rem)">

				<!-- 
					We can embed the img element, with srcset, AND original src attribute to allow for backwards compatibility. In this case the img element also acts as the 'default' if the media conditions in the source element above are not met.

					Also note that we are NOT defining the height and width of the img element below. This is on purpose because the source element above may end up loading a differently sized image, rendering the height and width attributes incorrect.
				-->
				<img src="https://andrewh.ca/teaches/web_design_and_development/tutorials/05/explanation/img/Puppies_cropped-400.jpg"  alt="A collection of Nova Scotia Duck-tolling Retriever puppies" srcset="https://andrewh.ca/teaches/web_design_and_development/tutorials/05/explanation/img/Puppies_cropped-400.jpg 400w, https://andrewh.ca/teaches/web_design_and_development/tutorials/05/explanation/img/Puppies_cropped-1000.jpg 1000w, https://andrewh.ca/teaches/web_design_and_development/tutorials/05/explanation/img/Puppies_cropped-1600.jpg 1600w" sizes= "(min-width: 75rem) 50vw, 100vw">

			</picture>


	</section>


              
            
!

CSS

              
                * {
	box-sizing: border-box;
}


body {
	margin: 0;
	padding: 1rem;
	font-family: Verdana, sans-serif;
}

p {
	max-width: 36rem;
}

section {
	margin-top: 6rem;
}

img {
	max-width: 100%;
	height: auto;
}

.grid {
	display: grid;
}


@media (min-width: 75rem) {

	.grid.two-col {
		grid-template-columns: 1fr 1fr;
	}

}
              
            
!

JS

              
                
              
            
!
999px

Console