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 🙏

© 2024 – Pkg Stats / Ryan Hefner

css-to-html

v0.7.0

Published

Generate HTML documents from CSS.

Downloads

100

Readme

CSS-to-HTML

Unit Tests npm version npm npm bundle size

Generate HTML documents from just CSS.

Give it a try on Cascades

Usage

npm i css-to-html
import { cssToHtml } from 'css-to-html';

// From a CSS string:
const css = 'p { color: purple; }';
const html = await cssToHtml(css);

// Or from a style element:
const css = document.querySelector('style').sheet.cssRules;
const html = await cssToHtml(css);

Example

Input:

h1 {
    content: 'Awesome!';
    color: grey;
}
p > button.rounded {
    content: 'Click here';
    background: #fff;
    border-radius: 8px;
}
p > button.rounded:hover {
    background: #ddd;
}
a img#logo {
    content: 'https://example.com/image';
    display: block;
    width: 1.5em;
    height: 1.5em;
}

Output:

<body>
    <h1>Awesome!</h1>
    <p>
        <button class="rounded">Click here</button>
    </p>
    <a><img src="https://example.com/image" id="logo"></a>
</body>

[!NOTE] cssToHtml always returns an HTMLBodyElement. To get the string representation of the generated HTML, use innerHTML or outerHTML. For example:

const html = await cssToHtml('h1#greeting { content: "Hello!"; }');
console.log( html.innerHTML ); // '<h1 id="greeting">Hello!</h1>'
console.log( html.outerHTML ); // '<body><h1 id="greeting">Hello!</h1></body>'

Options

An options object can be passed as the second argument to cssToHtml() to customize the behavior of the HTML generator. (Values marked with * are default).

| Option | Values | Description | | :----------- | :------------- | :---------- | | duplicates | preserve | Preserve duplicate elements. Eg: button {} button {} Will become: <button></button><button></button>. | | | remove * | Remove duplicate elements. Eg: button {} button {} Will become: <button></button>. | | fill | fill * | Fill the DOM with duplicate elements up to the desired location. Eg: span#fourth:nth-child(4) {} Will become: <span></span><span></span><span></span><span id="fourth"></span>. | | | no-fill | Don't fill. Eg: span#fourth:nth-child(4) {} Will become: <span id="fourth"></span>. | | imports | include | Fetch imported stylesheets and include them in the HTML generation process. | | | style-only * | Ignore @import rules when generating HTML. | | mergeNth | merge * | Elements generated from :nth- selectors will be merged with any similar element occupying the desired location. | | | no-merge | These elements will not be merged. | | sanitize | all * | Sanitize the generated HTML using DOMPurify. | | | imports | Only sanitize the HTML generated from imported stylesheets. | | | off | Don't sanitize the generated HTML. |

import { cssToHtml, type Options } from 'css-to-html';

// An example options object (populated with default values).
const options: Options = {
    duplicates: 'remove',
    fill: 'fill',
    imports: 'style-only',
    mergeNth: 'merge',
    sanitize: 'all'
};

const css = 'p { color: purple; }';
const html = await cssToHtml(css, options);