GithubHelp home page GithubHelp logo

append-styles's Introduction

append-styles

This module will prepend style tags to the document head. The style tag will get the given id and you can specify that the style tag should be placed before or after another tag with a given id. This is useful if you create multiple dependent modules that needs to injects CSS a specified order. If you don't need that capability use insert-css instead.

Example

You have two modules A and B, where B dependents on A. Then you inject your styles in the following way:

Module A:

var appendStyles = require('append-styles');

appendStyles({
  css: 'body { background:blue; }',
  id: 'module-a',
  before: 'module-b'
});

appendStyles({
  css: 'h1 { color:papayawhip; }',
  id: 'module-a',
  before: 'module-b'
});

Module B:

var appendStyles = require('append-styles');

appendStyles({
  css: 'body { background:red; }',
  id: 'module-b',
  after: 'module-a'
});

appendStyles({
  css: 'h1 { color:black; }',
  id: 'module-b',
  after: 'module-a'
});

No matter what order you load the modules in, you will get the following styles injected:

<html>
  <head>
    <style data-append-styles="module-a">body { background:blue; }</style>
    <style data-append-styles="module-a">h1 { color:papayawhip; }</style>
    <style data-append-styles="module-b">body { background:red; }</style>
    <style data-append-styles="module-b">h1 { color:black; }</style>
    ...
  </head>
  ...
<html>

API

var styleElement = appendStyles(css, options);

  • options.css - the css to be appended.
  • options.id - the append styles data id of the script tag to append css to. The element will be created if it does not exist.
  • options.before - the append styles data id of a script tag that this script tag should be created before.
  • options.after - the append styles data id of a script tag that this script tag should be created after.

Notice that you have to specify either a before and after.

append-styles's People

Contributors

csk157 avatar sunesimonsen avatar

Stargazers

 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.