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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@osmn-byhn/css-formatter

v1.0.2

Published

Format CSS with smart selectors

Readme

CSS Inliner

Bidirectional CSS-HTML converter with smart selector generation

Convert between CSS and inline styles effortlessly. Perfect for email templates, production optimization, and development workflows.

npm version License: MIT

🚀 Features

Forward Conversion (CSS → Inline)

  • Full CSS selector support - element, class, descendant, compound selectors
  • Preserve responsive styles - @media, @import, @keyframes, @font-face
  • Keep pseudo-classes - :hover, :focus, :active in <style> tags
  • Smart style merging - respects CSS specificity and existing inline styles

Reverse Conversion (Inline → CSS)

  • Smart selector generation - uses existing classes, creates nested selectors
  • Style deduplication - groups identical styles under combined selectors
  • Minimal HTML changes - prioritizes existing structure over auto-classes
  • Two output modes - internal <style> tag or external CSS file

📦 Installation

npm install css-inliner
# or
pnpm add css-inliner
# or
yarn add css-inliner

🎯 Quick Start

Forward: CSS to Inline Styles

import { inlineCSS } from 'css-inliner';

const html = `
<style>
  .header { color: blue; font-size: 24px; }
  .header:hover { color: darkblue; }
</style>
<div class="header">Hello World</div>
`;

const result = await inlineCSS(html);
console.log(result);
// Output:
// <style>.header:hover { color: darkblue; }</style>
// <div class="header" style="color:blue;font-size:24px">Hello World</div>

Reverse: Inline Styles to CSS (Internal)

import { reverseCSSInternal } from 'css-inliner';

const html = `
<div class="header" style="color:blue;font-size:24px">Hello</div>
<div class="header" style="color:blue;font-size:24px">World</div>
`;

const result = await reverseCSSInternal(html);
console.log(result);
// Output:
// <style>
// .header { color:blue; font-size:24px; }
// </style>
// <div class="header">Hello</div>
// <div class="header">World</div>

Reverse: Inline Styles to External CSS

import { reverseCSSExternal } from 'css-inliner';
import { writeFileSync } from 'fs';

const html = `
<div class="nav" style="display:flex;gap:20px">Navigation</div>
`;

const { html: htmlOutput, css: cssOutput } = await reverseCSSExternal(html);

writeFileSync('index.html', htmlOutput);
writeFileSync('styles.css', cssOutput);

// index.html:
// <link rel="stylesheet" href="styles.css">
// <div class="nav">Navigation</div>

// styles.css:
// .nav { display:flex; gap:20px; }

📚 API Reference

inlineCSS(input: string): Promise<string>

Converts CSS rules to inline styles.

Parameters:

  • input - HTML string or URL

Returns: HTML string with inlined CSS

Features:

  • Inlines all CSS rules into element style attributes
  • Preserves @media, @import, @keyframes, @font-face in <style> tag
  • Keeps pseudo-classes (:hover, :focus) in <style> tag
  • Respects CSS specificity
  • Merges with existing inline styles

reverseCSSInternal(input: string): Promise<string>

Extracts inline styles to internal CSS (<style> tag).

Parameters:

  • input - HTML string or URL

Returns: HTML string with CSS in <style> tag

Smart Selector Strategy:

  1. Use existing classes - .header, .nav-list
  2. Create nested selectors - .parent .child, .nav a
  3. Use element selectors - body, html, h1
  4. Auto-generate only when needed - .auto-style-1, .auto-style-2

Features:

  • Deduplicates identical styles
  • Preserves existing CSS (@media, :hover, etc.)
  • Minimal HTML modifications
  • Clean, semantic CSS output

reverseCSSExternal(input: string): Promise<{html: string, css: string}>

Extracts inline styles to external CSS file.

Parameters:

  • input - HTML string or URL

Returns: Object with html and css strings

Use Case: Production websites that benefit from browser caching

const { html, css } = await reverseCSSExternal(inlinedHTML);
// html: contains <link rel="stylesheet" href="styles.css">
// css: all extracted CSS rules

🎨 Use Cases

Email Templates

// Convert CSS to inline for email clients
const emailHTML = await inlineCSS(template);
sendEmail(emailHTML);

Production Optimization

// Extract inline CSS to cacheable file
const { html, css } = await reverseCSSExternal(buildOutput);
writeFileSync('index.html', html);
writeFileSync('styles.css', css);

Development Workflow

// Convert messy inline styles to readable CSS
const readable = await reverseCSSInternal(legacyHTML);

Round-Trip Conversion

// Chain conversions for different outputs
const inlined = await inlineCSS(original);
const withStyleTag = await reverseCSSInternal(inlined);
const { html, css } = await reverseCSSExternal(withStyleTag);

🔧 Advanced Examples

Responsive Design

const html = `
<style>
  .container { max-width: 1200px; }
  @media (max-width: 768px) {
    .container { max-width: 100%; }
  }
</style>
<div class="container">Content</div>
`;

const result = await inlineCSS(html);
// @media queries are preserved in <style> tag
// Regular styles are inlined

Font Imports

const html = `
<style>
  @import url('https://fonts.googleapis.com/css2?family=Roboto');
  body { font-family: 'Roboto', sans-serif; }
</style>
<body>Text</body>
`;

const result = await inlineCSS(html);
// @import is preserved
// font-family is inlined with proper quote escaping

Nested Selectors

const html = `
<div class="nav">
  <a style="color:blue;text-decoration:none">Link 1</a>
  <a style="color:blue;text-decoration:none">Link 2</a>
</div>
`;

const result = await reverseCSSInternal(html);
// Output:
// <style>
// .nav a { color:blue; text-decoration:none; }
// </style>
// <div class="nav">
//   <a>Link 1</a>
//   <a>Link 2</a>
// </div>

📊 Performance

  • Fast parsing - Uses optimized CSS and HTML parsers
  • Minimal overhead - Efficient DOM traversal and style matching
  • Memory efficient - Streaming-friendly architecture

🧪 Testing

# Run tests
pnpm test

# Watch mode
pnpm test:watch

# Coverage
pnpm test:coverage

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

📄 License

MIT © Osman Beyhan

🙏 Acknowledgments

Built with:


Made with ❤️ for developers who work with HTML and CSS