cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

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.

            
              <!-- 

This example shows how to use CSS media queries with SVG, and the difference between using a media query on an inline SVG and on an SVG being used as the source of an img element.

Icon designed by: Peter Nowell

 -->



<p>This example shows how to use CSS media queries with SVG. This color swatches icon has a small and large scale version. If the SVG is inline with the HTML, media queries detecting viewport width will use the width of the window, like you'd expect. In this example, the media query has been included at the top of the SVG element.
<br><br>
But when the same SVG (containing a media query) is used as the source of an img element, the media query can't detect the width of the window... instead the SVG's viewport detects the dimensions of its container—the img element. <span>Notice how the "img SVG" only responds to the width of the img—not the width of the window, like the inline SVG does.<span></p>


<p class="caption">Inline SVG:</p>

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="97" height="60" viewBox="0 0 97 60" version="1.1">
	<defs>
		<!--  CSS to toggle between "normal-scale" and "large-scale" versions of the icon:  -->
		<style>
			.normal-scale {
				display: block;
			}
			.large-scale {
				display: none;
			}


			@media screen and (min-width:500px) {
				.normal-scale {
					display: none;
				}
				.large-scale {
					display: block;
				}
			}
			
			.top-panel,
			.mid-panel,
			.back-panel {
				transform-origin: 48px 50px;
			}
			.mid-panel {
				transform: rotate(27deg);;
			}
			.back-panel {
				transform: rotate(53deg);
			}


		</style>
	</defs>
	
	<g class="normal-scale">
		<g class="back-panel">
		<path id="back-panel-fill" d="M40.5 5.01C40.5 3.62 41.63 2.5 42.99 2.5L53.01 2.5C54.38 2.5 55.5 3.62 55.5 5.01L55.5 50C55.5 54.14 52.13 57.5 48 57.5 43.86 57.5 40.5 54.14 40.5 50L40.5 5.01 40.5 5.01Z" fill="#FFFFFF" stroke="#565961"/>
		<rect id="back-swatch-1" fill="#40D68B" x="41" y="6"  width="14" height="7"/>
		<rect id="back-swatch-2" fill="#41D967" x="41" y="14" width="14" height="7"/>
		<rect id="back-swatch-3" fill="#59DB4B" x="41" y="22" width="14" height="7"/>
		<rect id="back-swatch-4" fill="#7DDE45" x="41" y="30" width="14" height="7"/>
		<rect id="back-swatch-5" fill="#A6E647" x="41" y="38" width="14" height="7"/>
	</g>

	<g class="mid-panel">
		<path id="mid-panel-fill" d="M40.5 5.01C40.5 3.62 41.63 2.5 42.99 2.5L53.01 2.5C54.38 2.5 55.5 3.62 55.5 5.01L55.5 50C55.5 54.14 52.13 57.5 48 57.5 43.86 57.5 40.5 54.14 40.5 50L40.5 5.01 40.5 5.01Z" fill="#FFFFFF" stroke="#565961"/>
		<rect id="mid-swatch-1" fill="#0BCDD4" x="41" y="6"  width="14" height="7"/>
		<rect id="mid-swatch-2" fill="#20D6CA" x="41" y="14" width="14" height="7"/>
		<rect id="mid-swatch-3" fill="#23DEBF" x="41" y="22" width="14" height="7"/>
		<rect id="mid-swatch-4" fill="#33E8B2" x="41" y="30" width="14" height="7"/>
		<rect id="mid-swatch-5" fill="#55F2B3" x="41" y="38" width="14" height="7"/>
	</g>

	<g class="top-panel">
		<path id="top-panel" d="M40.5 5.01C40.5 3.62 41.63 2.5 42.99 2.5L53.01 2.5C54.38 2.5 55.5 3.62 55.5 5.01L55.5 50C55.5 54.14 52.13 57.5 48 57.5 43.86 57.5 40.5 54.14 40.5 50L40.5 5.01 40.5 5.01Z" fill="#FFFFFF" stroke="#565961"/>
		<rect id="top-swatch-1" fill="#31A0F7" x="41" y="6"  width="14" height="7"/>
		<rect id="top-swatch-2" fill="#31B2F8" x="41" y="14" width="14" height="7"/>
		<rect id="top-swatch-3" fill="#31C3F8" x="41" y="22" width="14" height="7"/>
		<rect id="top-swatch-4" fill="#31D3F8" x="41" y="30" width="14" height="7"/>
		<rect id="top-swatch-5" fill="#31E4F8" x="41" y="38" width="14" height="7"/>
	</g>

		<circle class="pin" fill="#565961" cx="48" cy="50" r="2"/>
	</g>
	
	<g class="large-scale">
		<g class="back-panel">
		<path id="back-panel-fill" d="M40.5 5.01C40.5 3.62 41.63 2.5 42.99 2.5L53.01 2.5C54.38 2.5 55.5 3.62 55.5 5.01L55.5 50C55.5 54.14 52.13 57.5 48 57.5 43.86 57.5 40.5 54.14 40.5 50L40.5 5.01 40.5 5.01Z" fill="#FFFFFF" stroke="#565961" stroke-width=".5"/>
		<rect id="back-swatch-1" fill="#40D68B" x="40.75" y="6"  width="14.5" height="7.5"/>
		<rect id="back-swatch-2" fill="#41D967" x="40.75" y="14" width="14.5" height="7.5"/>
		<rect id="back-swatch-3" fill="#59DB4B" x="40.75" y="22" width="14.5" height="7.5"/>
		<rect id="back-swatch-4" fill="#7DDE45" x="40.75" y="30" width="14.5" height="7.5"/>
		<rect id="back-swatch-5" fill="#A6E647" x="40.75" y="38" width="14.5" height="7.5"/>
	</g>

	<g class="mid-panel">
		<path id="mid-panel-fill" d="M40.5 5.01C40.5 3.62 41.63 2.5 42.99 2.5L53.01 2.5C54.38 2.5 55.5 3.62 55.5 5.01L55.5 50C55.5 54.14 52.13 57.5 48 57.5 43.86 57.5 40.5 54.14 40.5 50L40.5 5.01 40.5 5.01Z" fill="#FFFFFF" stroke="#565961" stroke-width=".5"/>
		<rect id="mid-swatch-1" fill="#0BCDD4" x="40.75" y="6"  width="14.5" height="7.5"/>
		<rect id="mid-swatch-2" fill="#20D6CA" x="40.75" y="14" width="14.5" height="7.5"/>
		<rect id="mid-swatch-3" fill="#23DEBF" x="40.75" y="22" width="14.5" height="7.5"/>
		<rect id="mid-swatch-4" fill="#33E8B2" x="40.75" y="30" width="14.5" height="7.5"/>
		<rect id="mid-swatch-5" fill="#55F2B3" x="40.75" y="38" width="14.5" height="7.5"/>
	</g>

	<g class="top-panel">
		<path id="top-panel" d="M40.5 5.01C40.5 3.62 41.63 2.5 42.99 2.5L53.01 2.5C54.38 2.5 55.5 3.62 55.5 5.01L55.5 50C55.5 54.14 52.13 57.5 48 57.5 43.86 57.5 40.5 54.14 40.5 50L40.5 5.01 40.5 5.01Z" fill="#FFFFFF" stroke="#565961" stroke-width=".5"/>
		<rect id="top-swatch-1" fill="#31A0F7" x="40.75" y="6"  width="14.5" height="7.5"/>
		<rect id="top-swatch-2" fill="#31B2F8" x="40.75" y="14" width="14.5" height="7.5"/>
		<rect id="top-swatch-3" fill="#31C3F8" x="40.75" y="22" width="14.5" height="7.5"/>
		<rect id="top-swatch-4" fill="#31D3F8" x="40.75" y="30" width="14.5" height="7.5"/>
		<rect id="top-swatch-5" fill="#31E4F8" x="40.75" y="38" width="14.5" height="7.5"/>
	</g>

		<circle class="pin" fill="none" stroke="#565961" stroke-width=".5" cx="48" cy="50" r="1.5"/>
	</g>
	
</svg>




<p class="caption">Img with SVG src:</p>

<img alt="icon" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/44800/1-dynamic-icon.svg" />



<div class="scale-button">Change the img width to </div>
            
          
!
            
              svg,
img {
	display: block;
	margin: 10px 0;
	border: 1px lightgray solid;
}

body {
	max-width: 650px;
	padding: 0 10px;
	margin: 50px auto 100px auto;
}



/* Rotate Middle & Back Panels */
.top-panel,
.mid-panel,
.back-panel {
	transform-origin: 48px 50px;
}
.mid-panel {
	transform: rotate(27deg);;
}
.back-panel {
	transform: rotate(53deg);
}





/* Captions, Zoom Button, Zooming */
body {
	font-family: Avenir, sans-serif;
	color: #404248;
	-webkit-font-smoothing: antialiased;
	-moz-osx-font-smoothing: grayscale;
}
.caption {
	margin: 30px 0 0 0;
}
p span {
	font-weight: 600;
}
img {
	margin-bottom: 320px;
}
.scaled img {
	width: 500px;
	margin-bottom: 0;
}
.scale-button {
	position: fixed;
	bottom: 20px;
	background: white;
	cursor: pointer;
	padding: 5px 15px;
	border: 1px solid #3279FD;
	border-radius: 4px;
	color: #3074F3;
}
.scale-button:after {
	content: "500px";
}
.scaled .scale-button:after {
	content: "original size";
}



            
          
!
            
              // Event Listener for Zoom Button
$('.scale-button').click( changeSize );

// Function to zoom in or out
function changeSize() {
	if( $('html').hasClass('scaled') ) {
		$('html').removeClass('scaled');
	}else{
		$('html').addClass('scaled');
	};
};
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console