npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

rectpackr-layout

v1.0.1

Published

A web component that creates layouts by packing HTML elements as rectangles using a best-fit 2D strip-packing algorithm. Automatically measures dimensions and handles mixed content.

Readme

Rectpackr Layout

NPM Version Coverage Status CodePen Demos

A web component that creates layouts by treating your HTML elements as rectangles and packing them using a best-fit 2D strip-packing algorithm.

⚙️ Why a Packing Algorithm for Web Layouts?

Web browsers naturally manage elements as rectangles. rectpackr-layout leverages this by applying a best-fit strip-packing algorithm — the same approach used in industrial optimization problems — to web layout creation.

🤖 Layouts Through Automated Measurement

The algorithm intelligently works with whatever dimensional information is available:

How It Works:

  • Automatically measures element dimensions through browser APIs
  • Uses width as the primary constraint for predictable flow
  • Adapts to any height — whether fixed, aspect-ratio based, or content-determined
  • Handles mixed content seamlessly without manual configuration

You Can:

  • Set explicit widths for pixel-perfect control
  • Use percentage-based or responsive widths
  • Let elements determine their own natural sizes
  • Mix and match approaches within the same layout

What This Enables:

  • Truly flexible layouts that work with your existing CSS approach
  • Zero-configuration setups for rapid prototyping
  • Production-ready precision when you need exact control
  • Best of both worlds — automation when you want it, control when you need it

📥 Installation

Install the package via your preferred package manager:

npm

npm install rectpackr-layout

yarn

yarn add rectpackr-layout

pnpm

pnpm install rectpackr-layout

Then import it in your JavaScript:

// In your main.js or component file
import 'rectpackr-layout';

Or directly in your HTML:

<script type="module">
  import 'rectpackr-layout';
</script>

Using a CDN (No Build Step Needed)

Include it directly in your HTML via CDN:

unpkg

<script type="module" src="https://unpkg.com/rectpackr-layout"></script>

jsDelivr

<script
  type="module"
  src="https://cdn.jsdelivr.net/npm/rectpackr-layout"
></script>

esm.sh

<script type="module" src="https://esm.sh/rectpackr-layout"></script>

Once installed, use the web component anywhere in your HTML:

<rectpackr-layout>
  <div>Your content here</div>
</rectpackr-layout>

📖 API Reference

Attributes

positioning

Defines the CSS method used to position items.

  • transform (Default): Uses transform: translate(x, y)
  • offset: Uses CSS inset property for precise positioning

💡 Performance Note: The default transform value typically offers better performance through hardware acceleration. Use offset only when child elements already use transform for other purposes (animation etc.).

x-direction

Controls the horizontal packing direction.

  • ltr (Default): Left-to-right packing
  • rtl: Right-to-left packing

y-direction

Controls the vertical packing direction.

  • ttb (Default): Top-to-bottom packing
  • btt: Bottom-to-top packing

A Note on Visual Order & Accessibility

The x-direction and y-direction attributes control visual placement, which may create a difference between the visual arrangement and the underlying DOM order.

  • DOM Order is Preserved: The underlying HTML structure remains unchanged
  • Visual Order is Algorithm-Determined: Item placement follows the packing logic and your direction settings

🚀 Usage Examples

Fluid, Responsive Layout

<rectpackr-layout>
  <div>Card 1</div>
  <div>Card 2</div>
  <div>Card 3</div>
</rectpackr-layout>

<style>
  rectpackr-layout {
    container-type: inline-size;
    display: block;
  }

  rectpackr-layout > * {
    /* Fluid width based on container queries */
    width: 100%;
  }

  @container (min-width: 400px) {
    rectpackr-layout > * {
      width: 50%;
    }
  }

  @container (min-width: 800px) {
    rectpackr-layout > * {
      width: 33.33%;
    }
  }
</style>

Dynamic Content Handling

<rectpackr-layout id="dynamic-layout">
  <!-- Content can be added/removed dynamically -->
</rectpackr-layout>

<script>
  // The layout automatically adjusts to content changes
  document.getElementById('dynamic-layout').appendChild(newElement);
</script>

🎯 Live Demos

Consistent Width Gallery

See predictable masonry-style layouts with equal-width elements

View on CodePen

Mixed Dimension Gallery

Explore optimal packing of variably-sized elements and aspect ratios

View on CodePen

Interactive Playground

Experiment with real-time controls and dynamic content manipulation

View on CodePen

✅ Browser Support

Modern browsers with Web Components support.

🔧 Issues and Support

If you encounter any issues or have questions, please open an issue.

📄 License

This project is licensed under the MIT License.