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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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>CSS Media Queries</h1>
<p>Test Page for Web Browsers (Screen & Print)</p>
<h2>Screen size</h2>
<dl>
<dt>Width &times; Height:</dt><dd id="size"></dd>
</dl>
<h2>Examples</h2>
<dl>
<dt>Detected with JS <code>matchMedia</code>:</dt><dd><code>▨</code></dd>
<dt>Detected with CSS media queries:</dt><dd><code>▣</code></dd>
</dl>
<h3>#1 Bootstrap 4 grid [px]</h3>
<p>Detect current grid slot, using <code>max-width</code></p>
<table>
<thead>
<tr><th>Match</th><th>Description</th><th>Size</th></tr>
</thead>
<tbody>
<tr id="xs"><td><code>xs</code></td><td>extra small</td><td>0 - 575px</td></tr>
<tr id="sm"><td><code>sm</code></td><td>small</td><td>576px - 767px</td></tr>
<tr id="md"><td><code>md</code></td><td>medium</td><td>768px - 991px</td></tr>
<tr id="lg"><td><code>lg</code></td><td>large</td><td>992px - 1199px</td></tr>
<tr id="xl"><td><code>xl</code></td><td>extra large</td><td>1200px or higher</td></tr>
</tbody>
</table>
<h3>#2 Paper [mm, in]</h3>
<p>Detect largest size that still fits, using <code>min-width</code></p>
<table>
<thead>
<tr><th>Match</th><th>Description</th><th>Size</th></tr>
</thead>
<tbody>
<tr id="a3"><td><code>A3</code></td><td>A3 portrait</td><td>297mm &times; 420mm</td></tr>
<tr id="a4"><td><code>A4</code></td><td>A4 portrait</td><td>210mm &times; 297mm</td></tr>
<tr id="a5"><td><code>A5</code></td><td>A5 portrait</td><td>148mm &times; 210mm</td></tr>
</tbody>
<tbody>
<tr id="letter"><td><code>✉</code></td><td>Letter portrait</td><td>8½in &times; 11in</td></tr>
</tbody>
</table>
<h3>#3 Orientation</h3>
<p>Detect landscape/portrait mode, using <code> orientation</code></p>
<table>
<thead>
<tr><th>Match</th><th>Description</th><th>Hint</th></tr>
</thead>
<tbody>
<tr id="portrait"><td><code>↕</code></td><td>portrait</td><td>width &lt;= height</td></tr>
<tr id="landscape"><td><code>↔</code></td><td>landscape</td><td>width &gt; height</td></tr>
</tbody>
</table>
<div id="px"><b></b><b></b><b></b><b></b><b></b><b></b><b></b><b></b><b></b><b></b><b></b><b></b></div>
<div id="cm"><b></b><b></b><b></b><b></b><b></b><b></b><b></b><b></b><b></b><b></b><b></b><b></b><b></b><b></b><b></b><b></b><b></b><b></b><b></b><b></b><b></b><b></b><b></b><b></b><b></b><b></b><b></b><b></b><b></b><b></b></div>

<style>
html{font-size:9pt;line-height:1.25}
body{display:flex;flex-direction:column;margin:0;padding:0}
form{order:-2;position:sticky;z-index:1;top:0;margin-bottom:2em;padding:2px 4px;border-bottom:1px solid;background-color:#ddd;font-family:-apple-system,system-ui,BlinkMacSystemFont,'Segoe UI',Roboto,'Helvetica Neue',Arial,sans-serif;font-size:12px}
form div{margin:6px 12px;text-align:right}
select{width:6rem;margin-left:8px;padding:2px}
button{position:fixed;right:16px;bottom:16px;padding:4px 16px;box-shadow:0 2px 8px rgba(60,0,0,.25);font-weight:600}
@media (min-width:768px){form{display:flex}button{bottom:auto;top:6px;box-shadow:none}}
@media print{.screen{display:none!important}}
</style>
<form action="." method="get" class="screen">
<div>
<label for="assign-media">Apply styles with <code>media</code> attribute:</label>
<select id="assign-media">
<option value="">(none)</option>
<option value="screen">screen</option>
<option value="print">print</option>
<option value="illegal">illegal</option>
<option value="all">all</option>
</select>
</div>
<div>
<label for="assign-margin">Apply margin with <code>@page</code> directive:</label>
<select id="assign-margin">
<option value="">(none)</option>
<option value="0">0</option>
<option value="25mm">20mm</option>
<option value="1in">1in</option>
</select>
</div>
<button type="button">Print</button>
</form>

              
            
!

CSS

              
                html{box-sizing:border-box}
*,*::before,*::after{box-sizing:inherit}
*{margin:0;padding:0}
h1,h1+p,h2,h3,h3+p{border-left:2.25rem solid #ddd;padding-left:.5rem;font-weight:700;line-height:1}
h1,h2,h3{margin:2.5rem 0 1rem}
h1+p,h3+p{margin:-1rem 0 1rem}
h3+p{font-weight:inherit}
dt{position:absolute}
dd{position:relative;max-width:17em;color:red;text-align:right}
dl:last-of-type dd:last-of-type{color:inherit}
table,thead,tbody{display:block}
table{padding-bottom:.5em;border-bottom:thin dashed}
tr{display:flex}
th,td{width:8em}
th{font-style:italic;font-weight:inherit;text-align:left}
tbody{margin-top:.5em;border-top:thin dashed}
tbody tr{--css:none;--m:solid;margin-top:1em;border-bottom:thin solid;border-right:thin var(--m) red}
table:not(:first-of-type) tr{--m:none}
td{line-height:1.25em;vertical-align:bottom}
td code{display:block;position:relative;width:2rem;font-size:150%;text-transform:uppercase}
td code::before{content:var(--css);position:absolute;top:0;left:3ch}
td code::after{content:var(--js);position:absolute;top:0;left:4.5ch;color:red}
#letter code,#portrait code,#landscape code{text-align:center}

#xs{max-width:575px}
#sm{max-width:767px}
#md{max-width:991px}
#lg{max-width:1199px}
#xl{--css:'▣';--m:none}
@media (max-width:1199px){#lg{--css:'▣';--m:none}#lg~tr{--css:none;--m:none}}
@media (max-width:991px){#md{--css:'▣';--m:none}#md~tr{--css:none;--m:none}}
@media (max-width:767px){#sm{--css:'▣';--m:none}#sm~tr{--css:none;--m:none}}
@media (max-width:575px){#xs{--css:'▣';--m:none}#xs~tr{--css:none;--m:none}}

#a5{max-width:148mm}
#a4{max-width:210mm}
#a3{max-width:297mm}
@media (min-width:148mm){#a5{--css:'▣';--m:solid}}
@media (min-width:210mm){#a4{--css:'▣';--m:solid}#a4~tr{--css:none;--m:solid}}
@media (min-width:297mm){#a3{--css:'▣';--m:solid}#a3~tr{--css:none;--m:solid}}

#letter{max-width:8.5in}
@media (min-width:8.5in){#letter{--css:'▣';--m:solid}}

#landscape{--css:'▣'}
@media media print and (orientation:portrait){#portrait{--css:'▣'}#portrait~tr{--css:none}}

[data-grid='xs'] #xs code::after,
[data-grid='sm'] #sm code::after,
[data-grid='md'] #md code::after,
[data-grid='lg'] #lg code::after,
[data-grid='xl'] #xl code::after,
[data-din='a3'] #a3 code::after,
[data-din='a4'] #a4 code::after,
[data-din='a5'] #a5 code::after,
[data-letter='true'] #letter code::after,
[data-orientation='portrait'] #portrait code::after,
[data-orientation='landscape'] #landscape code::after{--js:'▨'}

#px,#cm{order:-1;counter-reset:ruler;overflow:hidden;font-size:7pt;line-height:1;white-space:nowrap}
b{counter-increment:ruler;display:inline-block;height:6mm;padding:1pt;border-right:thin solid;font-weight:inherit;text-align:right}
b::before{content:var(--css);float:left}
#px b{width:100px}
#px b::after{content:counter(ruler) '00px'}
#cm b{width:1cm}
#cm b::after{content:counter(ruler) 'cm'}
@media (min-width:100px){#px b:nth-child(1)::before{--css:'▣'}}
@media (min-width:200px){#px b:nth-child(2)::before{--css:'▣'}}
@media (min-width:300px){#px b:nth-child(3)::before{--css:'▣'}}
@media (min-width:400px){#px b:nth-child(4)::before{--css:'▣'}}
@media (min-width:500px){#px b:nth-child(5)::before{--css:'▣'}}
@media (min-width:600px){#px b:nth-child(6)::before{--css:'▣'}}
@media (min-width:700px){#px b:nth-child(7)::before{--css:'▣'}}
@media (min-width:800px){#px b:nth-child(8)::before{--css:'▣'}}
@media (min-width:900px){#px b:nth-child(9)::before{--css:'▣'}}
@media (min-width:1000px){#px b:nth-child(10)::before{--css:'▣'}}
@media (min-width:1100px){#px b:nth-child(11)::before{--css:'▣'}}
@media (min-width:1200px){#px b:nth-child(12)::before{--css:'▣'}}
@media (min-width:1cm){#cm b:nth-child(1)::before{--css:'▣'}}
@media (min-width:2cm){#cm b:nth-child(2)::before{--css:'▣'}}
@media (min-width:3cm){#cm b:nth-child(3)::before{--css:'▣'}}
@media (min-width:4cm){#cm b:nth-child(4)::before{--css:'▣'}}
@media (min-width:5cm){#cm b:nth-child(5)::before{--css:'▣'}}
@media (min-width:6cm){#cm b:nth-child(6)::before{--css:'▣'}}
@media (min-width:7cm){#cm b:nth-child(7)::before{--css:'▣'}}
@media (min-width:8cm){#cm b:nth-child(8)::before{--css:'▣'}}
@media (min-width:9cm){#cm b:nth-child(9)::before{--css:'▣'}}
@media (min-width:10cm){#cm b:nth-child(10)::before{--css:'▣'}}
@media (min-width:11cm){#cm b:nth-child(11)::before{--css:'▣'}}
@media (min-width:12cm){#cm b:nth-child(12)::before{--css:'▣'}}
@media (min-width:13cm){#cm b:nth-child(13)::before{--css:'▣'}}
@media (min-width:14cm){#cm b:nth-child(14)::before{--css:'▣'}}
@media (min-width:15cm){#cm b:nth-child(15)::before{--css:'▣'}}
@media (min-width:16cm){#cm b:nth-child(16)::before{--css:'▣'}}
@media (min-width:17cm){#cm b:nth-child(17)::before{--css:'▣'}}
@media (min-width:18cm){#cm b:nth-child(18)::before{--css:'▣'}}
@media (min-width:19cm){#cm b:nth-child(19)::before{--css:'▣'}}
@media (min-width:20cm){#cm b:nth-child(20)::before{--css:'▣'}}
@media (min-width:21cm){#cm b:nth-child(21)::before{--css:'▣'}}
@media (min-width:22cm){#cm b:nth-child(22)::before{--css:'▣'}}
@media (min-width:23cm){#cm b:nth-child(23)::before{--css:'▣'}}
@media (min-width:24cm){#cm b:nth-child(24)::before{--css:'▣'}}
@media (min-width:25cm){#cm b:nth-child(25)::before{--css:'▣'}}
@media (min-width:26cm){#cm b:nth-child(26)::before{--css:'▣'}}
@media (min-width:27cm){#cm b:nth-child(27)::before{--css:'▣'}}
@media (min-width:28cm){#cm b:nth-child(28)::before{--css:'▣'}}
@media (min-width:29cm){#cm b:nth-child(29)::before{--css:'▣'}}
@media (min-width:30cm){#cm b:nth-child(30)::before{--css:'▣'}}

              
            
!

JS

              
                (function (w, d) {

	// report screen width (pixel)
	const log = function () {
		d.getElementById('size').innerHTML = `${w.innerWidth}px &times; ${w.innerHeight}px`;
	};
	w.addEventListener('load', log);
	w.addEventListener('resize', log);

	// detect breakpoints with JS
	const detect = function() {
		let detected;
		// BS4 grid size detection
		const maxPxWidths = {'lg': 1199, 'md': 991, 'sm': 767, 'xs': 575};
		detected = 'xl';
		Object.keys(maxPxWidths).forEach(function (size) {
			if (w.matchMedia(`(max-width: ${maxPxWidths[size]}px)`).matches) {
				detected = size;
			}
		});
		d.querySelector('html').setAttribute('data-grid', detected);
		// DIN paper size detection
		const minMmWidths = {'a5': 148, 'a4': 210, 'a3': 297};
		detected = 'none';
		Object.keys(minMmWidths).forEach(function (size) {
			if (w.matchMedia(`(min-width: ${minMmWidths[size]}mm)`).matches) {
				detected = size;
			}
		});
		d.querySelector('html').setAttribute('data-din', detected);
		// US paper size detection
		detected = w.matchMedia('(min-width: 8.5in)').matches ? 'true' : 'false';
		d.querySelector('html').setAttribute('data-letter', detected);
		// orientation detection
		detected = w.matchMedia('(orientation: portrait)').matches ? 'portrait' : 'landscape';
		d.querySelector('html').setAttribute('data-orientation', detected);
	};
	w.addEventListener('load', detect);
	w.addEventListener('resize', detect);

	// adjust stylesheet media attribute
	const assignMedia = function (event) {
		const mediaAttribute = typeof event === 'string' ? event : event.target.value;
		const stylesheet = d.querySelector('head style:not(.page)');
		if (mediaAttribute) {
			stylesheet.setAttribute('media', mediaAttribute);
		} else {
			stylesheet.removeAttribute('media');
		}
	};
	assignMedia(d.getElementById('assign-media').value);
	d.getElementById('assign-media').addEventListener('change', assignMedia);

	// adjust page margins for print
	const assignMargin = function (event) {
		const marginSize = typeof event === 'string' ? event : event.target.value;
		const stylesheet = d.querySelector('.page');
		if (stylesheet) {
			stylesheet.parentNode.removeChild(stylesheet);
		}
		if (marginSize) {
			d.querySelector('head').insertAdjacentHTML('beforeend', `<style class="page">@page{margin:${marginSize}}</style>`);
		}
	};
	assignMargin(d.getElementById('assign-margin').value);
	d.getElementById('assign-margin').addEventListener('change', assignMargin);

	// call system print dialog for current frame
	d.querySelector('button').addEventListener('click', function (event) {
		w.print();
	});

}(window, document));

              
            
!
999px

Console