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

              
                <div class="page-wrapper">
    <div class="page-layout">
        <main class="demo-section main-content">
            <h1>Main Content</h1>
            <p>This card's layout is now correct because JavaScript is watching its size.</p>
            
            <div class="card-wrapper">
                <article class="card">
                    <img class="card-image" src="https://placehold.co/600x400/22c55e/ffffff?text=Looks+Good" alt="Correctly laid out card">
                    <div class="card-content">
                        <h2 class="card-title">Adaptive Card</h2>
                        <p class="card-description">My layout is based on my container's width.</p>
                        <button class="card-button">Learn More</button>
                    </div>
                </article>
            </div>
        </main>
        
        <aside class="demo-section sidebar">
            <h1>Sidebar</h1>
            <p>This card is also correct. JS sees it's in a narrow container and does not change its layout.</p>
            
             <div class="card-wrapper">
                <article class="card">
                    <img class="card-image" src="https://placehold.co/600x400/22c55e/ffffff?text=Looks+Good" alt="Correctly laid out card">
                    <div class="card-content">
                        <h2 class="card-title">Adaptive Card</h2>
                        <p class="card-description">My layout is based on my container's width.</p>
                        <button class="card-button">Learn More</button>
                    </div>
                </article>
            </div>
        </aside>
    </div>
</div>
              
            
!

CSS

              
                :root {
            --surface-color: #ffffff;
            --background-color: #f1f5f9;
            --primary-color: #4f46e5;
            --text-primary: #1e293b;
            --text-secondary: #64748b;
            --border-color: #e2e8f0;
        }

        body {
            font-family: 'Inter', system-ui, sans-serif;
            background-color: var(--background-color);
            color: var(--text-primary);
            margin: 0;
            padding: 2rem;
        }

        .page-wrapper {
            max-width: 1200px;
            margin: 0 auto;
        }

        .page-layout {
            display: grid;
            grid-template-columns: 1fr;
            gap: 2rem;
        }

        @media (min-width: 960px) {
            .page-layout {
                grid-template-columns: minmax(0, 2.5fr) minmax(0, 1fr);
            }
        }

        .demo-section h1 {
            font-size: 1.5rem;
            font-weight: 700;
            margin-top: 0;
            margin-bottom: 0.25rem;
            color: var(--text-primary);
        }

        .demo-section p {
            font-size: 1rem;
            color: var(--text-secondary);
            margin-top: 0;
        }

        /* 2. Enhanced Card Component Styles */
        .card-wrapper {
             margin-top: 1.5rem;
        }
        
        .card {
            display: grid;
            grid-template-columns: 1fr; /* Default is single column */
            background-color: var(--surface-color);
            border-radius: 0.75rem;
            border: 1px solid var(--border-color);
            box-shadow: 0 1px 3px rgba(0,0,0,0.05), 0 1px 2px rgba(0,0,0,0.02);
            overflow: hidden;
        }

        .card-image {
            width: 100%;
            height: auto;
            display: block;
            aspect-ratio: 16 / 9;
            object-fit: cover;
        }

        .card-content {
            padding: 1.5rem;
        }

        .card-title {
            font-size: 1.25rem;
            font-weight: 700;
            margin: 0 0 0.5rem 0;
        }
        
        .card-description {
            color: var(--text-secondary);
            line-height: 1.6;
            margin: 0 0 1.5rem 0;
        }

        .card-button {
            display: inline-block;
            background-color: var(--primary-color);
            color: white;
            border: none;
            padding: 0.75rem 1.5rem;
            border-radius: 0.5rem;
            font-weight: 500;
            text-align: center;
            cursor: pointer;
            transition: background-color 0.2s ease;
        }
        
        .card-button:hover {
            background-color: #4338ca;
        }


        /* 3. THE JAVASCRIPT-POWERED STYLES */
        /* This CSS rule is only applied when JavaScript adds the '.is-horizontal' class.
           The media query from Demo 1 has been REMOVED. */
        .card.is-horizontal {
            grid-template-columns: 140px 1fr; /* Horizontal layout */
            align-items: center;
            gap: 0;
        }

        .card.is-horizontal .card-image {
            height: 100%;
        }

        .card.is-horizontal .card-content {
            padding: 1.5rem;
        }
              
            
!

JS

              
                 // 4. THE RESIZEOBSERVER HACK
    const observer = new ResizeObserver(entries => {
      // Loop over all the elements being observed.
      for (const entry of entries) {
        // entry.target is the .card-wrapper div
        // entry.target.firstElementChild is the .card article
        const card = entry.target.firstElementChild;
        if (card) {
          // If the container (.card-wrapper) is wider than 450px, add the class.
          // Otherwise, remove it.
          card.classList.toggle('is-horizontal', entry.contentRect.width > 450);
        }
      }
    });

    // Find all the card containers on the page and start observing them.
    document.querySelectorAll('.card-wrapper').forEach(wrapper => {
      observer.observe(wrapper);
    });
              
            
!
999px

Console