GithubHelp home page GithubHelp logo

kafkaforks / computed-style-to-inline-style Goto Github PK

View Code? Open in Web Editor NEW

This project forked from lukehorvat/computed-style-to-inline-style

0.0 1.0 0.0 9 KB

Convert a HTML element's computed CSS to inline CSS.

License: MIT License

JavaScript 100.00%

computed-style-to-inline-style's Introduction

computed-style-to-inline-style NPM version Build status

Convert a HTML element's computed CSS to inline CSS.

Uses Window.getComputedStyle internally.

Installation

Install the package with NPM:

$ npm install computed-style-to-inline-style

Usage

The package exposes a function that accepts a HTML element as an argument. When called, it will iterate through every computed style property of the specified element and redefine it as an inline style. An optional boolean argument can be provided to determine whether the function recursively processes child elements as well (defaults to false).

Example:

var computedToInline = require("computed-style-to-inline-style");
computedToInline(document.body, true);

Why?

Consider a scenario where you're rendering an SVG element in a HTML document, with its pretty styling defined in external stylesheets:

<svg viewBox="0 0 800 600">...</svg>

You then add a link to let users download the SVG as a file:

<a href="data:image/svg+xml;base64,..." download="example.svg">Download</a>

To your dismay, you find that none of the SVG's pretty styling is preserved when downloaded to disk! This is because the file lacks the original context in which the SVG element was rendered in the browser and has no reference to those nice external stylesheets you wrote.

As a solution, you use this package to redefine the SVG styling inline, guaranteeing that the element is saved to file with all of its fancy CSS information embedded within:

var computedToInline = require("computed-style-to-inline-style");
var svg = getSvgElement();
computedToInline(svg, true);

computed-style-to-inline-style's People

Contributors

lukehorvat avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.