Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <!-- Just making this to experiment with Alpine's plugin system -->
<script src="https://unpkg.com/alpinejs@3.x.x/dist/cdn.js" defer></script>

<div class="space-y-2">
  <div class="max-w-screen-2xl h-2 bg-red-100"></div>
  <div class="max-w-screen-xl h-2 bg-blue-200"></div>
  <div class="max-w-screen-lg h-2 bg-green-300"></div>
  <div class="max-w-screen-md h-2 bg-yellow-400"></div>
  <div class="max-w-screen-sm h-2 bg-indigo-500"></div>
</div>
<!-- This is just a demo. And because we don't have direct access to TW breakpoints,
     the demo is using the above list to parse them. If you want to use this functionality
     without requiring them to be parsed, just include an object on x-data
     like x-data="{ twWidths: { sm: '100px', md: '300px' } }" and so on-->

<div class="p-6 bg-gray-100 min-h-screen" x-data="{bg: 'bg-gray-300'}">

  <div 
    class="w-20 h-20"   
    :class="bg"
    x-tw:sm="bg = 'bg-indigo-500'"
    x-tw:md="bg = 'bg-yellow-400'"
    x-tw:lg="bg = 'bg-green-300'"
    x-tw:xl="bg = 'bg-blue-200'"
    x-tw:2xl="bg = 'bg-red-100'"
  ></div>
<!-- CAVEAT: Alpine will process these in order as they appear, so order matters. And they all fire when the condition is met -->
  <p class="mt-6">Resize the browser to the breakpoints and the color will be applied</p>
</div>
              
            
!

CSS

              
                [x-cloak] { display: none!important; }
              
            
!

JS

              
                // This is just a simple implementation done quickly, so it may not cover all scenarios
document.addEventListener('alpine:init', () => {
  Alpine.directive('tw', 
    (el, { value, expression, modifiers }, { Alpine, evaluateLater, evaluate, effect, cleanup }) => {
    
    // First evaluate the special widths option users may pass in
    const widths = evaluate('typeof twWidths === "undefined" ? {} : twWidths')
    
    // Set up the expression to be evaluated later in the listener callback
    const run = evaluateLater(expression)
    
    // This is VERY hacky, but it will attempt to look for possibly breakpoints. 
    // There may be other ways to do this in the future (especially if TW ever makes them CSS vars)
    Object.values(document.querySelectorAll('[class*="max-w-screen-"]')).forEach(node => {
      // Grab the suffix like lg from max-w-screen-lg
      const prefix = Object.values(node.classList)
        .filter((c) => c.includes('screen'))[0]
        .split('-').slice(-1).pop()
      // Add properties that the user didn't pass in
      widths.hasOwnProperty(prefix) || (widths[prefix] = window.getComputedStyle(node).maxWidth)
    })
    
    const mql = window.matchMedia(`(max-width: ${widths[value]})`)
    const handler = () => effect(() => run())
    handler()
    
    
    mql.addEventListener('change', handler)
    // Be sure you clean up your listeners as a best practice
    cleanup(() => mql.removeEventListener('change', handler))
    
  })
})
              
            
!
999px

Console