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

              
                -width=600
div(
data-inline-media 
style='padding: 0.5em 1em; @media screen and (max-width: '+width+'px) { width: auto !important; background: red; color: white } width: '+width+'px;'
) 
	| This block should have #[code width: auto] and  #[strong(style='background: red; color: white') red background] when screen width is less than #{width} pixels
-width=600
.js-inline-media( 
style='padding: 0.5em 1em; @media screen and (max-width: '+width+'px) { width: auto !important; background: red; color: white } width: '+width+'px;'
) 
	| This block should have #[code width: auto] and  #[strong(style='background: red; color: white') red background] when screen width is less than #{width} pixels
              
            
!

CSS

              
                body
	margin: 2rem
	line-height: 1.5
code
	border: 1px solid lightgray
	padding: 0.05em 0.25em
              
            
!

JS

              
                function uuid() {
	return 'xxxx-xxxx'.replace(/[xy]/g, c => {
		let r = Math.random() * 16 | 0,
			v = c == 'x' ? r : (r & 0x3 | 0x8);
		return v.toString(16);
	});
}

function inlineMedia(element) {
  /*
this allows you to use `@media` rules right inside `style` attribute of HTML elements, like this:

```html
<div class="js-inline-media" style="padding: 10rem; @media (max-width: 500px) { padding: 1rem; }"></div>
```

and you will get this:

```html
<div class="js-inline-media" data-inline-media="a9c6-ope4" style="padding: 10rem;"></div>
<style>
@media (max-width: 500px) { 
	[data-inline-media="a9c6-ope4"] {
		padding: 1rem;
	}
}
</style>
```

So, in short:

- you write `@media` rule right inside `style` attribute, **but without selector inside**
- `@media` rule is moved from `style` attribute into `<style>` tag 
- source element now has unique `data-inline-media` attribute value

TODO:

- allow use multiple `@media` blocks for one element

like this:

```
@media (max-width: 500px) { … } @media (max-width: 300) { … }
```

for now, you will get only last `@media`. others will be ignored.
  */
	let $el = $(element)
	let id = uuid()
	$el.attr('data-inline-media', id)
	let regex = /(@media.*)\{(.*)\}/i
	let styleSource = $el.attr('style')
	let styles = styleSource.match(regex)
	if(!styles) return; // if there is no styles you are looking for, IE might be broken
	let newStyle = `${styles[1]}{
		[data-inline-media="${id}"] {
			${styles[2]}
		}
	}`
	let $styleEl = $('<style>')
	$styleEl.text(newStyle)
	$el.after($styleEl)
	$el.attr('style', styleSource.replace(regex, ''))
	// console.debug(newStyle)
}
$(document).ready(() => {
	$('[data-inline-media], .js-inline-media').each((index, element) => {
		inlineMedia(element)
	})
})
              
            
!
999px

Console